Link against dynamic lib (DEF-2732)

I have an android library, libabc.so, how can I link it and bundle to apk?

If defold support it, the manual in native extensions should be updated.

We don’t support this yet. It’s assigned ticket DEF-2732.
We can hopefully start working on that in the beginning of 2019.

I’m working with a third party library which has dynamic .so, Right now, How can I use it?Can I use dlopen?

Yes, you should be able to use dlopen.

Another thing that is tricky, is that when bundling/building, the dynamic libraries doesn’t end up next to the executable, so you’ll need to workaround that as well, by copying the libraries manually.

I wonder if there are other gotchas, so I’m going to pong @dapetcu21, to see if he has some good info how to work with shared libraries currently.

1 Like

Well, it depends. If your library also has a Java side, it’s a bit trickier. I had to create a Java file with a function in which I call System.loadLibrary("something"), then calling that function with JNI. Calling System.loadLibrary directly in JNI didn’t work.

Otherwise, if you add the library in my_extension/res/armv7-android/lib/ameabi-v7a/libsomething.so, then call dlopen("libsomething.so"), it should technically work. If you only use the library through Java (JNI), just do the System.loadLibrary("something") as mentioned above without the dlopen().

I’m not 100% sure why, but in the FMOD extension I had to call, System.mapLibraryName("something") in JNI to get the path to libsomething.so, though from what I remember, just specifying the name and not the full path should correctly resolve to it.

Check out FMODBridge::linkLibraries() in here: https://github.com/dapetcu21/defold-fmod/blob/master/bridge/src/fmod_dynamic_loading.cpp

After calling dlopen(), you’ll have to call dlsym() to get the address for each function you have to call from the library. I made myself a macro to help with this: https://github.com/dapetcu21/defold-fmod/blob/master/bridge/src/fmod_bridge.hpp#L134

3 Likes

OK,I’ll have a try.