Question

I am trying to read the ANDROID_ID String from the NDK.

Edit : Initial problem on the bottom, here is where i am now :

I removed the static and changed the Jni code to :

jstring JNICALL GEN_FUNCNAME(MyClass_nGetID)(JNIEnv *env,jobject obj
{

jclass  activity = env->GetObjectClass(obj);

jmethodID  mid_getContentResolver =env->GetMethodID(activity,"getContentResolver","()Landroid/content/ContentResolver;");

jobject contentObj =  env->CallObjectMethod(obj, mid_getContentResolver);

if(contentObj == NULL)
    return env->NewStringUTF("method 1 null");

jclass secClass=env->FindClass("android/provider/Settings$Secure");

if(secClass == NULL)
    return env->NewStringUTF("class 2 null");

jmethodID secMid = env->GetStaticMethodID(secClass,"getString","(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;");

if(secMid == NULL)
    return env->NewStringUTF("method 2 null");

jstring jandroid_id = (jstring) env->CallStaticObjectMethod(secClass,secMid,contentObj,"android_id");

if(jandroid_id == NULL)
    return env->NewStringUTF("android id null");

Now the first stage seems to work but I get the following error on CallStaticObjectMethod :

W/dalvikvm(2698): Invalid indirect reference 0x7509bd4c in decodeIndirectRef
I/dalvikvm(2698):   at android.provider.Settings$Secure.getString(Settings.java:-1)
I/dalvikvm(2698):   at mypackage.myclass.nGetID(Native Method)

Thanks for your help so far ! :)

<-------------------------- Initial Issue ------------------------------------>

I can successfully read it in Java using :

final String android_id = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID); 

I get a JNI warning and a VM crash on the function CallObjectMethod in the JNI.

Is there something I forgot to do ?

The JNI_WARNING :

W/dalvikvm(14911): JNI WARNING: can't call Landroid/content/Context;.getContentResolver on instance of Ljava/lang/Class; (CallObjectMethodV)

The VM crash log :

E/dalvikvm(14911): VM aborting
A/libc(14911): Fatal signal 6 (SIGABRT) at 0x00003a3f (code=-6), thread 14911 

My JNI function :

jstring GEN_FUNCNAME(MyClass_nGetID)(JNIEnv *env,jobject obj)

{
jclass  act = env->GetObjectClass(obj);
jclass  clsontentResolver = env->FindClass("android/content/Context");


jmethodID  mid_getContentResolver = env->GetMethodID(clsontentResolver,"getContentResolver","()Landroid/content/ContentResolver;");

jobject contentObj = env->CallObjectMethod(act, mid_getContentResolver);

jclass secClass=env->FindClass("android/provider/Settings$Secure");

jmethodID secMid=env->GetStaticMethodID(secClass,"getString","(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;");

jstring jandroid_id= (jstring) env->CallStaticObjectMethod(secClass,secMid,contentObj,"android_id");

return jandroid_id;

}

My Java Main class :

public class MyClass extends Activity {

    TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView)findViewById(R.id.text);

        text.setText(nGetID());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    static { System.loadLibrary("mylib"); }
    public final static native String nGetID();

}

Many thanks !

Was it helpful?

Solution

(Edit)

To pass strings back to Java you need to use NewStringUTF. Like this:

jstring jStringParam = getEnv()->NewStringUTF("android_id");
jstring jandroid_id = (jstring) env->CallStaticObjectMethod(secClass,secMid,contentObj,jStringParam);
// Remember to release when done!
getEnv()->DeleteLocalRef(jStringParam);

(Initial issue)

jobject obj not attached to an instance

public final static native String nGetID(); is defined as static, meaning that it won't be "attached" to any object instance.

While in your JNI function The line jclass act = env->GetObjectClass(obj); - you pass in jobject obj.

Try declaring nGetID() without static, that should do the trick. Is there any reason why you want it static?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top