Skip to content

Instantly share code, notes, and snippets.

@douzifly
Created April 19, 2014 14:43
Show Gist options
  • Save douzifly/11086408 to your computer and use it in GitHub Desktop.
Save douzifly/11086408 to your computer and use it in GitHub Desktop.
android get package name from jni
static jstring getpkg(JNIEnv* env, jobject thiz, jobject activity) {
jclass android_content_Context =env->GetObjectClass(activity);
jmethodID midGetPackageName = env->GetMethodID(android_content_Context,"getPackageName", "()Ljava/lang/String;");
jstring packageName= (jstring)env->CallObjectMethod(activity, midGetPackageName);
return packageName;
}
@fnc12
Copy link

fnc12 commented Jun 13, 2016

Good code. But do you know a way of performing this without activity handle?

@z1ad
Copy link

z1ad commented Jan 10, 2018

for reference, a way to do it without activity parameter:

JNIEXPORT jstring JNICALL
Java_com_example_test_Test_getPackageName(JNIEnv *env, jobject obj)
{
    jclass cls = (*env)->GetObjectClass(env, obj);
    jmethodID mid = (*env)->GetMethodID(env, cls, "getPackageManager", "()Landroid/content/pm/PackageManager;");
    jobject packageManager = (*env)->CallObjectMethod(env, obj, mid);

    // this.getPackageName()
    mid = (*env)->GetMethodID(env, cls, "getPackageName", "()Ljava/lang/String;");
    jstring packageName = (jstring) (*env)->CallObjectMethod(env, obj, mid);

   return packageName;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment