Instantiate Java class from JNI - android

I need to create an instance of a Java class in my native code. To do it, I am using the following C code:
jobject Java_com_mypackage__myClass_myMethod(JNIEnv* env, jobject thiz, jint index){
int fd = pDevs[index].ufds.fd; // fd = open(....); it's a input/eventX file.
jclass class = (*env)->FindClass(env,"com/mypackage/ClassName");
jmethodID mid = (*env)->GetMethodID(env,class,"<init>","(Ljava/lang/String;)V");
return (*env)->NewObject(env,class,mid,(*env)->NewStringUTF(env, pDevs[index].device_path));
}
But when I invoke myMethod, I keep getting fatal signal 11 (SIGSEGV). Is the code wrong?

You should use logging/debbuger to find place where segmentation fault happenned. The easiest way is to use android logging system as described
here
jclass class = (*env)->FindClass(env,"com/mypackage/ClassName");
if(class == null)
{
__android_log_print(ANDROID_LOG_VERBOSE, "TAG", "class is null");
}
For example if ClassName is an inner class of some activity you should use com/mypackage/ActivityName#ClassName instead of com/mypackage/ClassName. But I can only guess before you provide your logs.

Related

How to set IntField from native jni to Java Android 10

After update my project to support Android 10, a crash produced in the JNI level related to non-SDK interface restrictions in Android 10 :
JNI DETECTED ERROR IN APPLICATION: JNI SetIntField called with pending exception java.lang.NoSuchFieldError: no "I" field "value" in class "Ljava/lang/Integer;" or its superclasses
which come from this part of code :
jclass clazz = (*env)->GetObjectClass(env, outputObj);
jfieldID mi = (*env)->GetFieldID(env, clazz, "value", "I");
(*env)->SetIntField(env, outputObj, mi, pListLen);
To fix that i replace it by :
jclass clazz = (*env)->GetObjectClass(env, outputObj);
jmethodID intValueMethod = (*env)->GetMethodID(env, clazz, "intValue", "()I");
jint result = (*env)->CallIntMethod(env, outputObj,intValueMethod,pListLen);
After that my application doesn't crash and find the integer value correctly, but i want to set the result integer on Java code using the same method SetIntField.
Could you please give me a way or method to set the result on the Java part.
As changing the intger field is not a good practice on JNI part, so i changed the way instead changing it by reflection method
jclass clazz = (*env)->GetObjectClass(env, outputBufLen);
jmethodID value_of = (*env)->GetStaticMethodID(env,clazz, "valueOf", "(I)Ljava/lang/Integer;");
jobject result = (*env)->CallStaticObjectMethod(env,clazz, value_of, pOutBufLen);
I set the result under an int Array and i return it :
int result = (*env)->CallStaticObjectMethod(env,clazz, value_of, pOutBufLen);
jintArray resultArray = (*env)->NewIntArray(env, 2);
jint fill[2];
fill[0] = result;
fill[1] = (jint) pOutBufLen;
(*env)->SetIntArrayRegion(env, result, 0, 2, fill);
return (resultArray);

Invalid indirect reference 0x416f68a0 in decodeIndirectRef

I've seen plenty of questions about exactly the same error, but none of them seems to be trying to do this simple thing and still fail.
I have in my class header, as private members:
static JNIEnv* env;
static jclass copterServiceClass;
static jmethodID mavlinkMsgMethod;
Then in the source for that class:
JNIEnv* JU_Calls::env = 0;
jclass JU_Calls::copterServiceClass = 0;
jmethodID JU_Calls::mavlinkMsgMethod = 0;
bool JU_Calls::setupJNICalls() {
if (cached_jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "Unable to get Java Env from cached JavaVM");
return -1;
}
jclass dataClass = env->FindClass("eu/deye/copterdroidair/copterdroidair/Services/CopterService");
copterServiceClass = (jclass) env->NewGlobalRef(dataClass);
mavlinkMsgMethod = env->GetMethodID(copterServiceClass, "MavlinkMsg", "(Ljava/lang/String;)V");
jobject javaObjectRef = env->NewObject(copterServiceClass, mavlinkMsgMethod);
jstring msg = env->NewStringUTF("aaaa");
env->CallVoidMethod(javaObjectRef, mavlinkMsgMethod, msg);
return true;
}
Note: cached_jvm is assigned at JNI_OnLoad.
It fails with the aforementioned error when executing NewObject.
Invalid indirect reference 0x416f68a0 in decodeIndirectRef
I tried passing NULL to the CallVoidMethod as the message, as seen on other's questions, but as it's logical, the problem is before, so useless.
Hope you can help me, as always, great SO masters ;)
EDIT: While I think Bangyno answer is the right one, what I've eventually done to solve the problem as quickest as possible, was to declare the Java Methods that would be called from C++ as static. That way I don't have to call the constructor, and everything makes a lot more sense. Because the Java class to which I was calling, was an Android Service, thus calling the constructor was erroneous for sure.
Apart from declaring the Java method static, the resulting C++ code results as follows:
jclass copterServiceClass = env->FindClass("eu/deye/copterdroidair/copterdroidair/Services/CopterService");
jmethodID mavlinkMsgMethod = env->GetStaticMethodID(copterServiceClass, "MavlinkMsg", "(ILjava/lang/String;)V");
jstring msg = env->NewStringUTF(str);
env->CallStaticVoidMethod(copterServiceClass, mavlinkMsgMethod, severity, msg);
env->DeleteLocalRef(msg);
Is very important not to forget the last line, because otherwise it will fill your JNI table and crash.
Here I want to discuss is the error message:
Invalid indirect reference 0x416f68a0 in decodeIndirectRef
It means you give the wrong argument, the second one "mavlinkMsgMethod".
In my experience, if you change "mavlinkMsgMethod" to a number such as "5", the 0x416f68a0 will change to 0x5.
The right way to use newObject is use to invoke the constructor. It should look like this, just a sample:
jclass dataClass = env->FindClass("eu/deye/copterdroidair/copterdroidair/Services/CopterService");
mavlinkMsgMethod = env->GetMethodID(copterServiceClass, "<init>", "(Ljava/lang/String;)V");
jstring str = env->NewStringUTF("testing");
jobject javaObjectRef = env->NewObject(copterServiceClass, mavlinkMsgMethod, str);

JNI call to find Java class from native results in NoCLassDefFoundError

I am using following code to access a java class from native code
JNIEnv *env = nullptr;
JMVEnv::attachCurrentJNIENv(&env);
jclass jXYZClass = env->FindClass("com/xxx/xx/xx/XYZClassName");
This call passes 4-5 times but fails after that. jXYZClass is null after some calls.
So, while compilation class is found and during execution also it was found 4-5 times. I am calling this code from different locations. Can it be some threading issue?
I think it is a multhreading problems... Especially when you say that " it was found 4-5 times. I am calling this code from different locations".
See this post for more details.
Firstly, you can print the thread id of each thread to see if they are the same thread.
You can Findclass in JNI_OnLoad:
jobject g_class;
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *pjvm, void *reserved) {
JavaVM *gJvm = pjvm; // cache the JavaVM pointer
JNIEnv *env= NULL;
env = gJvm->GetEnv((void**)&env, JNI_VERSION_1_6);
jclass tmp = env->FindClass("com/xxx/xx/xx/XYZClassName");
g_class = env->NewGlobalRef(tmp);
}
whenever you want to use this class:
// extern jobject g_class; Add this Line if this is in another cpp file
jmethodID methodID = env->GetMethodID((jclass)g_class, "<init>", "()V");
jobject new_object = env->NewObject((jclass)g_class, methodID);

What is "jobject thiz" in JNI and what is it used for?

I am having a hard time finding an answer to this. But, what is "jboject thiz" used for in JNI function calls? For example:
jobjectArray Java_com_gnychis_awmon_Test( JNIEnv* env, jobject thiz ) {
I use env to allocate objects often, but I've never used thiz and I'm not sure what it is for. Just for knowledge purposes.
The following is a JNI wrapper function which has two parameters, and returns a primitive array of objects:
jobjectArray Java_com_gnychis_awmon_Test( JNIEnv* env, jobject thiz );
From the function name you have given I don't think it is complete, that is, you haven't respected the obligatory function name convention which is:
Start the function with Java_
Append the package name separated by _ (undescores) i.e. com_company_awesomeapp. So far the function name is composed of: Java_com_company_awesomeapp
Append the Java class name where the native method has been defined,
followed by the actual function name. So at this point we should have the following function name: Java_com_company_awesomeapp_MainActivity_Test
The first parameter is a pointer to a structure storing all JNI function pointers, i.e. all the predefined functions you have available after you #include <jni.h>.
The second parameter is a reference to the Java object inside which this native method has been declared in. You can use it to call the other methods of the Java object from the current JNI function, i.e. Call Java instance methods from JNI code written in C or C++.
If for example you have the following Java class inside the MainActivity.java file:
public class MainActivity extends Activity
{
static
{
try
{
System.loadLibrary("mynativelib");
}
catch (UnsatisfiedLinkError ule)
{
Log.e(TAG, "WARNING: Could not load native library: " + ule.getMessage());
}
}
public static native Object[] Test();
}
Then, the jobject thiz parameter of the JNI function would be a reference to an object of type MainActivity.
I found this link that should help clarify the question.
https://library.vuforia.com/articles/Solution/How-To-Communicate-Between-Java-and-C-using-the-JNI
Here is an example in it that uses the "jobject".
JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargets_initApplicationNative(
JNIEnv* env, jobject obj, jint width, jint height)
{
...
jclass activityClass = env->GetObjectClass(obj);
jmethodID getTextureCountMethodID = env->GetMethodID(activityClass,
"getTextureCount", "()I");
if (getTextureCountMethodID == 0)
{
LOG("Function getTextureCount() not found.");
return;
}
textureCount = env->CallIntMethod(obj, getTextureCountMethodID);
...
}
jobject thiz means the this in java class.
Sometimes if you create a static native method like this.
void Java_MyClass_method1 (JNIEnv *, jclass);
jclass means the class itself.

Android - JNI - Recovering MyException members through JNI

In my Java code I create MyException class (extending Exception class) with the getCustomCode() method.
In my C++ code, when I call a Java method that throws MyException I need to execute the getCustomCode of this exception to properly handle the exception.
To accomplish that I execute the Java method that throws MyException with this code:
jint result = env->CallIntMethodA(javaObj, methodId, params);
Right after this line I check for JavaException with this code:
jthrowable exc = env->ExceptionOccurred();
if(exc)
{
jclass objCls = env->FindClass("com/mycompany/myapp/exception/MyException");
jmethodID codeMethod = env->GetMethodID(objCls, "getCustomCode", "()I");
if(!objCls || !codeMethod){ ........ }
// Try to execute getCustomCode java method.
jint codeResult = env->CallIntMethod((jobject)exc, codeMethod);
...
...
}
But, when I try to execute the getCustomCode through JNI it fails.
I did some checks with the JNI methods IsAssignableFrom and IsInstanceOf and the result was:
jclass objCls = env->FindClass ("com/mycompany/myapp/exception/MyException");
jclass objThrowable = env->FindClass ("java/lang/Throwable");
if(env->IsAssignableFrom(objCls, objThrowable) == JNI_TRUE) { /* TRUE! */ }
The condition returned true, so my class is correct.
Another check:
jclass objCls = env->FindClass ("com/mycompany/myapp/exception/MyException");
jclass objThrowable = env->FindClass ("java/lang/Throwable");
if(env->IsInstanceOf((jobject)exc, objCls) == JNI_TRUE) { /* FALSE */ }
if(env->IsInstanceOf((jobject)exc, objThrowable) == JNI_TRUE) { /* FALSE */ }
Both conditions returned false, so neither MyException nor Throwable is the exc class!
So, what is the jthrowable object? And how can I cast the jthrowable object to a jobject to access MyException members?
Is it possible?
Thank you!
Most likely you need to call env->ExceptionClear() before env->FindClass(...) etc. You are not allowed to call most JNI methods while an exception is active, see section 6.2.2 of this page. List of allowed functions when there is a pending exception:
ExceptionOccurred
ExceptionDescribe
ExceptionClear
ExceptionCheck
ReleaseStringChars
ReleaseStringUTFchars
ReleaseStringCritical
Release<Type>ArrayElements
ReleasePrimitiveArrayCritical
DeleteLocalRef
DeleteGlobalRef
DeleteWeakGlobalRef
MonitorExit

Categories

Resources