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?
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
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?
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?