Home > Net >  Error when trying to access nativeLibraryDir
Error when trying to access nativeLibraryDir

Time:10-22

i'm trying to access getPackageManager.getApplicationInfo in jni.

const char* getNativeLibPath(JNIEnv* env, jobject thiz, const char* libraryName, const char* packageName) {
    jclass contextClass = env->GetObjectClass(thiz);
    jmethodID getPackageManager = env->GetMethodID(contextClass, "getPackageManager", "()Landroid/content/pm/PackageManager;");
    jobject instantiatePackageManager = env->CallObjectMethod(thiz, getPackageManager);
    jclass packageManagerClass = env->GetObjectClass(instantiatePackageManager);
    jmethodID getApplicationInfo = env->GetMethodID(packageManagerClass, "getApplicationInfo", "(Ljava/lang/String;I)Landroid/content/pm/ApplicationInfo;");
    jobject instantiateApplicationInfo = env->CallObjectMethod(thiz, getApplicationInfo, packageName, 0);
    jclass applicationInfoClass = env->GetObjectClass(instantiateApplicationInfo);
    jfieldID nativeLibraryDir = env->GetFieldID(applicationInfoClass, "nativeLibraryDir", "Ljava/lang/String;");
    auto string = (jstring) env->GetObjectField(instantiateApplicationInfo, nativeLibraryDir);
    const char* returnValue = env->GetStringUTFChars(string, nullptr);
    std::string appendedResult = std::string(returnValue)   std::string("/")   std::string(libraryName);
    return appendedResult.c_str();
}

This is my code for it. However for some reason i'm getting this error: JNI ERROR (app bug): accessed stale WeakGlobal 0x74eecd21ff (index 1324143135 in a table of size 38) JNI DETECTED ERROR IN APPLICATION: use of deleted weak global reference 0x74eecd21ff Any help is appreciated!

CodePudding user response:

Your code has at least three problems:

  1. You call getApplicationInfo with a const char * which expects a Java string:
jobject instantiateApplicationInfo = env->CallObjectMethod(instantiatePackageManager, getApplicationInfo, env->NewStringUTF(packageName), 0);
  1. You need to call env->ReleaseStringUTF(returnValue) to release the string on the Java side

  2. You cannot return a const char * like that. Either return the std::string directly, or allocate memory with new char[] and let the caller free it.

  • Related