Re: How to access Package Name from JNI/NDK .. ?

2,628 views
Skip to first unread message

learner

unread,
Jul 11, 2012, 4:39:21 AM7/11/12
to andro...@googlegroups.com
Pass your activity as a input parameter to the JNI native function. And then call the below APIs
 
static jboolean nativeGetPackageName(JNIEnv* env, jobject this, 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);

}

 
 

On Tuesday, 26 June 2012 16:31:49 UTC+5:30, ibisum wrote:
Hi,

I would like to replace my use of hard-coded package names in findClass() method calls from within my JNI library, with non hard-coded paths .. "somehow".

The trouble is: how do I get the package name for the currently running app which is using my library, from within the JNI?

foo64

unread,
May 8, 2013, 5:57:40 PM5/8/13
to andro...@googlegroups.com
I don't think this will work, because with a signature like that you'd need to call JNIEnv::RegisterNatives( jclass, ... ) so it would be callable from Java. Notice the first parameter which is a class reference, which you need to get from somewhere (most likely hardcoded). If you want to avoid calling JNIEnv::RegisterNatives( ) then you'll need to declare your function like this:

JNIEXPORT jboolean JNICALL Java_com_example_package_ClassName_nativeGetPackageName(JNIEnv* env, jobject this, jobject activity)
{
    ...
}

In both cases, you need to hardcode the package and class reference.

I wish I knew a solution for this though. 
Reply all
Reply to author
Forward
0 new messages