David  Tsaturyan David Tsaturyan - 15 days ago 6
C++ Question

Couldn't resolve JNIfunction: Android ndk

I want to get the list of installed apps in Android by calling Android methods from C++ code. The first method found (

getPackageManager()
), but the
getInstalledApplications()
method every time is nil. What is wrong here?

JNIEXPORT jobject JNICALL Java_com_example_davidt_ndkexample_TestNdk_getListOfInstalledApps
(JNIEnv* env, jobject obj, jobject activity){
jclass contextWrapperClass = env->FindClass("android/content/ContextWrapper");
jmethodID getPackageManagerId = env->GetMethodID(contextWrapperClass,"getPackageManager","()Landroid/content/pm/PackageManager;");
if (getPackageManagerId == 0) {
fprintf(stderr, "packageManager() not found");
}

jobject pm = env->CallObjectMethod(activity, getPackageManagerId);

jclass PackageManagerClass = env->GetObjectClass(pm);
jmethodID getInstalledApplicationsId = env->GetMethodID(PackageManagerClass,"getInstalledApplications","(I)[Landroid/content/pm/ApplicationInfo;");
if (getInstalledApplicationsId == 0) {
fprintf(stderr, "getInstalledApplications() not found");
}

return env->CallObjectMethod(pm, getInstalledApplicationsId);
}

Answer

Looks like your signature descriptor is wrong. I've used the javap tool to check it

javap -s -bootclasspath <Your SDK Installation Path>/platforms/android-25/android.jar android.content.pm.PackageManager

and for getInstalledApplications it returns

public abstract java.util.List<android.content.pm.ApplicationInfo> getInstalledApplications(int);
    descriptor: (I)Ljava/util/List;

so, in your case it has to be

jmethodID getInstalledApplicationsId = env->GetMethodID(PackageManagerClass,"getInstalledApplications","(I)Ljava/util/List");
Comments