# Kotlin Native plugin loading mechanism

**URL:** <https://discuss.kotlinlang.org/t/kotlin-native-plugin-loading-mechanism/15645>\
**Category:** Native\
**Created:** [December 29, 2019, 3:39pm UTC](https://discuss.kotlinlang.org/t/kotlin-native-plugin-loading-mechanism/15645 "2019-12-29T15:39:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aknauf](https://avatars.discourse-cdn.com/v4/letter/a/e99b99/32.png) [@aknauf](https://discuss.kotlinlang.org/u/aknauf)\
**Post date:** [December 29, 2019, 3:39pm UTC](https://discuss.kotlinlang.org/t/kotlin-native-plugin-loading-mechanism/15645/1 "2019-12-29T15:39:50Z")

</div>

Is there a way of implementing a plugin mechanism in Kotlin Native? I’m aware that there is no Kotlin Native equivalent of Java’s Class.forName(). I have considered implementing something using C-interop and dlopen()/dlsym(), but that is limited to types with C mappings and I want plugin authors to be able to write multi-platform plugins in Kotlin Common.

Is it possible to dynamically load a Kotlin Native library at runtime, without resorting to C-interop?

---

<div class="post-metadata">

**Author:** ![dickensas](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dickensas](https://discuss.kotlinlang.org/u/dickensas)\
**Post date:** [January 5, 2020, 4:38am UTC](https://discuss.kotlinlang.org/t/kotlin-native-plugin-loading-mechanism/15645/2 "2020-01-05T04:38:50Z")

</div>

dlopen in combination with -fPIC should be working, as you told, you already doing it

**platform.posix** in kotlin native already have **dlfcn.h** you don’t have to cinterop it yourself, you can use dlopen to load .so files or similar files using those functions

**platform.windows** in kotlin native already have **windows.h** you don’t have to cinterop it yourself, you can use **LoadLibrary** function to load the .dll files

similarly  
**platform.android** in kotlin native already have **dlext.h**  
**platform.posix** in kotlin native will work for **IOS** and **OSX** also

You can refer all these cinterop here [Kotlin Native GitHub](https://github.com/JetBrains/kotlin-native/tree/master/platformLibs/src/platform)

---

<div class="post-metadata">

**Author:** ![aknauf](https://avatars.discourse-cdn.com/v4/letter/a/e99b99/32.png) [@aknauf](https://discuss.kotlinlang.org/u/aknauf)\
**Post date:** [January 28, 2020, 9:00am UTC](https://discuss.kotlinlang.org/t/kotlin-native-plugin-loading-mechanism/15645/3 "2020-01-28T09:00:48Z")

</div>

Thanks for your reply and sorry for the delayed response on this. I’m aware of dlopen() and LoadLibrary(), however, as I understand it, the Kotlin binding that lets us use them is a C binding, meaning that I would be limited to treating these as C functions and not as Kotlin classes.

What I really want to do is dynamically load a Kotlin Library that implements a Kotlin interface that I have defined. Is there a way to do this?

---

<div class="post-metadata">

**Author:** ![HughG](https://avatars.discourse-cdn.com/v4/letter/h/35a633/32.png) [@HughG](https://discuss.kotlinlang.org/u/HughG)\
**Post date:** [January 29, 2020, 8:12am UTC](https://discuss.kotlinlang.org/t/kotlin-native-plugin-loading-mechanism/15645/4 "2020-01-29T08:12:07Z")

</div>

At least on Windows, loading a DLL with `LoadLibrary` will call some predefined (by Windows) DLL init function (can’t remember the name), and that could then set up Kotlin-level runtime classes, objects etc. so that the loading app can access them. It could b me just as if you had dynamically linked against the library at compile time, except you’d need some reflection-based(?) way to access those runtime things. If that’s possible, I imagine/hope that Kotlin Native would generate that for you already, but maybe you’ve already discovered that it doesn’t. Or maybe you just need to find out what that last be of reflection magic is?

---

<div class="post-metadata">

**Author:** ![dickensas](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dickensas](https://discuss.kotlinlang.org/u/dickensas)\
**Post date:** [February 3, 2020, 2:42am UTC](https://discuss.kotlinlang.org/t/kotlin-native-plugin-loading-mechanism/15645/5 "2020-02-03T02:42:20Z")

</div>

you need to use some “internal” libraries to operate “.klib” file

Refer -  
org.jetbrains.kotlin.library.metadata.KlibMetadataProtoBuf  
org.jetbrains.kotlin.konan.library.KonanLibrary  
org.jetbrains.kotlin.konan.library.resolverByName  
org.jetbrains.kotlin.library.metadata.parseModuleHeader

but as per the books this not documented to be a solution

The only document I see it  
[https://kotlinlang.org/docs/reference/native/libraries.html](https://kotlinlang.org/docs/reference/native/libraries.html)
