I can use System.loadLibrary(lib);
In android java to load my shared library and use it. The problem with this current approach is that I can actually have two instances of my android application running.
Having two instances of my application with one shared library doesn't. I need two instances of my shared library. The good new is that from java I can find out if I am running either instance A or instance B. Sometimes I'll be running in instance A but still have to load instance B; doing this totally messes up the first instance.
What i've done so far is find out which instance I am running in and pass that to JNI then inside my .c file I load the .so with dlopen, now I have a handle to my shared library.
The java side:
if(preview)
{
for (String lib : getLibraries()) {
nativeInitPreview(lib);
}
}
else
{
for (String lib : getLibraries()) {
nativeInitLive(lib);
}
}
the c code side:
static void *mainHandleLive = NULL;
static void *SDLHandleLive = NULL;
JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeInitLive(JNIEnv* env, jobject instance, jstring lib)
{
const char *libString = (*env)->GetStringUTFChars(env, lib, NULL);
__android_log_print(ANDROID_LOG_INFO, "SDL", "Java_org_libsdl_app_SDLActivity_nativeInitLive %s", libString);
(*env)->ReleaseStringUTFChars(env, lib, libString);
mainHandleLive = dlopen("/data/app-lib/org.libsdl.app-1/libmain.so", RTLD_NOW | RTLD_LOCAL);
if (mainHandleLive == 0)
{
__android_log_print(ANDROID_LOG_INFO, "SDL", "/data/app-lib/org.libsdl.app-1/libmain.so failed to open");
}
SDLHandleLive = dlopen("/data/app-lib/org.libsdl.app-1/libSDL2.so", RTLD_NOW | RTLD_LOCAL);
if (SDLHandleLive == 0)
{
__android_log_print(ANDROID_LOG_INFO, "SDL", "/data/app-lib/org.libsdl.app-1/libSDL2.so failed to open");
}
}
I have two questions, the initial post that lead me to this idea used a function prototype like this:
dlmopen(LM_ID_NEWLM, "path/to/lib", RTLD_NOW | RTLD_LOCAL);
on android compiling with LM_ID_NEWLM
throws an LM_ID_NEWLM
undeclared (first use in this function).
So I tried to use RTLD_LOCAL based on this question
I don't currently get any errors when compiling this project.
My question now is, how do I actually use these libs once they have been loaded from c code?