1
I have these two code snippets, the two of them display the same result, so I’d like to know, what’s the difference between them.
1
jclass cls = (*env)->FindClass(env, "android/content/ContextWrapper");
jmethodID mid = (*env)->GetMethodID(env, cls, "getPackageName", "()Ljava/lang/String;");
jstring packageNamei = (jstring)(*env)->CallObjectMethod(env, context, mid);
const char *nativeString = (*env)->GetStringUTFChars(env, packageNamei, 0);
__android_log_print(ANDROID_LOG_INFO, "--packageName--jni--", "%s", nativeString);
2
jclass context_clazz = (*env)->GetObjectClass(env, context);
jmethodID methodID_pack = (*env)->GetMethodID(env, context_clazz,
"getPackageName", "()Ljava/lang/String;");
jstring application_package = (*env)->CallObjectMethod(env, context, methodID_pack);
const char *str = (*env)->GetStringUTFChars(env, application_package, 0);
__android_log_print(ANDROID_LOG_DEBUG, "JNI", "PackageName: %s\n", str);
I can use any of them, or some is more recommended?
Why do you want to do this in JNI?
– Victor Stafusa
Is to get the package name of the android app, could I do otherwise? I’m getting the
context
ofMainActivity.java
– Florida
If you have a context can get the Package name through
context.getPackageName();
– ramaral
In the library @ramaral?
– Florida
What do you mean library? Only if you are programming in java.
– ramaral
The focus is the C library, get the pakage name only in C, compiled library, know how to get in java, the code I’m using requires the
context
, which I could only achieve throughMainActivity.java
maybe this other one of mine question help you understand better.– Florida