What is the difference between these two means of obtaining the Packagename?

Asked

Viewed 61 times

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?

  • Is to get the package name of the android app, could I do otherwise? I’m getting the context of MainActivity.java

  • If you have a context can get the Package name through context.getPackageName();

  • In the library @ramaral?

  • What do you mean library? Only if you are programming in java.

  • 1

    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 through MainActivity.java maybe this other one of mine question help you understand better.

Show 1 more comment

1 answer

1


In case 1

FindClass will take the class reference from her name, that is, for you to get the class reference you must know her name and her full signature. Class example String:

Name String

Full subscription java.lang.String - but the points should be replaced by bars "java/lang/String"

In case 2 GetObjectClass take the class reference from an object, that is, you do not need to know the class name, the object itself contains that information and the GetObjectClass take the reference of the class to which the object was instantiated. If the class, to which this object belongs, does not have the method you are looking for, probably GetObjectClass will generate an exception NoSuchMethodError. The second argument from GetObjectClass is the reference of the object:

jclass GetObjectClass(JNIEnv *env, jobject obj);

Specifically for the code snippet of your second case, the function GetObjectClass can be used in the case of several classes that have the same method, such as classes that inherit a method from the same parent class. Thus it does not matter which class the instantiated object belongs to because all classes inherit the same method.

Their use will depend on the purpose of their native function.

Below link with more information about the class operations functions: JNI Functions

From this information you can judge which one will serve best for your need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.