Issue
I have a static method in a Java class called getCurrValue()
package com.my;
public class MyClass {
public static long value = 5L;
public static long getCurrValue(){
return value;
}
}
and I have a method in native c/c++ code called useValue(JNIEnv* env)
.
When I try to use the Java static method getCurrValue()
to get the value
in native c/c++ code , I always get 0L
as result - why?
void useValue(JNIEnv* env) {
jclass clazz = env->FindClass("com/my/MyClass");
jlong result = -1L;
jmethodID get_curr_value_method_id = env->GetStaticMethodID(clazz,"getCurrValue","()J");
result = env->CallStaticLongMethod(clazz,get_curr_value_method_id);
// the result is 0L
}
Solution
I tested this code in a native method and on my Nexus 9 with Android 6.0 and it's working perfectly.
jstring Java_it_stefanocappa_ndkexample_Example_stringFromJNI( JNIEnv* env, jobject thiz ) {
jclass clazz = (*env)->GetObjectClass(env, thiz);
jmethodID staticMethodId1;
staticMethodId1 = (*env)->GetStaticMethodID(env, clazz, "getCurrValue", "()J");
jlong staticMethodResult2;
staticMethodResult2 = (jlong) (*env)->CallStaticLongMethod(env, clazz, staticMethodId1);
return (*env)->NewStringUTF(env, "Hello from JNI);
}
If you are not satisfied about my answer, please write and i'll post the entire fully-working example ;)
Update as suggested:
I updated a project on Github that i realized some months ago.
Repository: https://github.com/Ks89/NdkExample_AndroidStudio
Look "example.c" lines 155-166.
Obviously, in this example there are also other features.
Answered By - Stefano Cappa
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.