I wanted to check whether specific set of packages are installed on the device in c++ ndk code as I don't wanted to check in Java code, so after research I had written code comparing code in java. My code checks packageinfo and if exception is thrown it treats it as package not found. Here is my code snippet
jboolean checkpackages(JNIEnv *env, jobject context, char packagePaths[][MAX_STRING_SIZE], int size) {
jstring packageName;
jobject packageManagerObj;
jobject packageInfoObj;
jclass contextClass = env->GetObjectClass(context);
jmethodID getPackageNameMid = env->GetMethodID(contextClass, "getPackageName",
"()Ljava/lang/String;");
jmethodID getPackageManager = env->GetMethodID(contextClass, "getPackageManager",
"()Landroid/content/pm/PackageManager;");
jclass packageManagerClass = env->FindClass("android/content/pm/PackageManager");
jmethodID getPackageInfo = env->GetMethodID(packageManagerClass, "getPackageInfo",
"(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
jclass packageInfoClass = env->FindClass("android/content/pm/PackageInfo");
jfieldID versionCodeFid = env->GetFieldID(packageInfoClass, "versionCode", "I");
packageManagerObj = env->CallObjectMethod(context, getPackageManager);
for (int i = 0; i < size; i++) {
const char *path = packagePaths[i];
//packageName = env->NewStringUTF(path);
jstring packageNamet = env->NewStringUTF("com.mypackgage");
packageInfoObj = env->CallObjectMethod(packageManagerObj, getPackageInfo, packageNamet, 0x0);
int versionCode = env->GetIntField(packageInfoObj, versionCodeFid);
if(env->ExceptionCheck()) {
env->ExceptionClear();
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: Package manager: Exception");
} else {
return true;
}
}
return false;
}
Here even after putting exception check my code crashes with pending exception
JNI DETECTED ERROR IN APPLICATION: JNI GetIntField called with pending exception android.content.pm.PackageManager$NameNotFoundException
My code works if I put existing package on device.I'm using Android studio 4.0.1 and ndk 21.3. Please help me in how can I handle this or any other way we can do.
Aucun commentaire:
Enregistrer un commentaire