I'm trying to understand the lifecycle stages of an Android app with a native lib.
The native library is loaded like this in the MainActivity:
static {
System.loadLibrary("lifecycleevents");
}
The definition of JNI_onLoad and JNI_onUnLoad are as follows:
extern "C" JNIEXPORT jint JNICALL
JNI_OnLoad (JavaVM * pJavaVM, [[maybe_unused]] void * pReserved) {
JNIEnv * env;
if (pJavaVM->GetEnv ((void **) &env, JNI_VERSION_1_6) != JNI_OK)
return JNI_ERR;
__android_log_print (ANDROID_LOG_VERBOSE, (const char *) TAG, "JNI_OnLoad");
return env->GetVersion ();
}
extern "C" JNIEXPORT void JNICALL
JNI_OnUnload (JavaVM * pJavaVM, [[maybe_unused]] void * pReserved) {
__android_log_print (ANDROID_LOG_VERBOSE, (const char *) TAG, "JNI_OnUnload");
}
I'm able to trace JNI_onLoad but JNI_onUnLoad never hits. According to the documentation, it will be called when the classloader of the native lib is garbage collected. This will happen at some point after MainActivity's onDestroy but before the app terminates, right? So, why is JNI_onUnLoad not called?
Also, in the documentation, JNI_onUnLoad_L will be called when the classloader of the native lib L is garbage collected. Since my native lib name is lifecycleevents, I renamed the JNI_onUnLoad method to JNI_onUnLoad_lifecycleevents, but it still doesn't get called.
I'm confused. Are both the methods valid? Requesting help from anyone who is familiar with this topic.