Android Studio spurious "Cannot resolve corresponding JNI function" - android

Android Studio is showing this error. Here's a snippet of the relevant code:
public class MainActivity extends AppCompatActivity {
...
static native void runTests(String path);
static {
System.loadLibrary("TestLib");
}
}
In that first line, runTests is highlit in red and if you hover over it, you see the message "Cannot resolve corresponding JNI function"
To be clear, the app does build and run just fine. It's just that Android Studio thinks that this symbol can't be resolved.

OK, I figured it out. If my function is defined using the standard naming convention, e.g.
extern "C"
JNIEXPORT void JNICALL
Java_com_example_myapplication_MainActivity_runTests(JNIEnv *env, jclass cls, jstring path)
Then Android Studio figures it out.
My problem was that I was just defining it as
static void
runTests(JNIEnv *env, jclass cls, jstring path)
and using env->RegisterNatives() to link it into Java. It was too much to ask of Android Studio to figure that out.

Related

Android NDK RegisterNatives more full example

I'm try to make Android Native C++ project in Android Studio 2020.3.1.
Here and here provided common tips for using RegisterNatives in JNI_OnLoad. But in this examples i can't understand what is nativeFoo and nativeBar ? These are methods or functions of c++ code? And what is doing MyClass in example ? I want more full and clear code example for Android NDK C++ native. Thank you!
Going backwards from the RegisterNatives call in the first link, it hooks up native C++ methods to the following Java class:
package com.example.app.package;
class MyClass {
public native void nativeFoo();
public native bool nativeBar(String, int);
}
and it expects that you defined the following C++ functions earlier:
JNIEXPORT void JNICALL nativeFoo(JNIEnv *env, jobject object) { ... }
JNIEXPORT jboolean JNICALL nativeBar(JNIEnv *env, jobject object, jstring a, jint b) { ... }

Calling native function from the Android API level 28 Application

I have created a pre-built Native library libNative.so, that has a JNI function to be called from the Android application.
libNative.so is located in the device at /system/x86_64.
Now, I want to call the JNI function implemented in my pre-built Native library from Android application. I am following below Procedure.
1> Load the Native lib in MainActivity:
static { System.loadLibrary("Native"); }
2> Declared the native method as:
public native int Init();
3> Function definition in the shared lib is:
JNIEXPORT jint Java_App_PACKAGE_NAME_MainActivity_Init(JNIEnv* env, jobject callingObject){
return 0;
}
4>I'm calling the native function in onCreate:
int i = Init();
Now If I build and run the application. I get the error as:
AndroidRuntime: java.lang.UnsatisfiedLinkError: No implementation found for int App_PACKAGE_NAME_MainActivity.Init() (tried Java_App_PACKAGE_NAME_MainActivity_Init and Java_App_PACKAGE_NAME_MainActivity_Init__)
Please help me to resolve the above error.
Why is it that I get linker error even though loading of the library is success.
Thanks in advance !
I found the mistake I was doing.The definition of JNI function in the native library was wrong. I changed as below and everything started working!.
extern "C" JNIEXPORT jint JNICALL
Java_App_PACKAGE_NAME_MainActivity_Init(JNIEnv* env, jobject callingObject){
}

Pass jobjectArray from JNI to Java crash on Android API 21/22 but work on API 19

I use follow code to handle JNI array in Android and JNI.
However I found return "jobjectArray" cannot complete on API 21/22 (Android 5.0) but works on API 19. (Android 4.4)
(cannot complete mean it return on JNI part but it hang and no response on Java)
Here is the pseudo code I try to implement in my Android App.
jobjectArray Java_com_test_Simplejni(JNIEnv* env, jobject thisObj)
jclass localClass = env->FindClass("java/lang/Object");
jclass objClass = reinterpret_cast<jclass>(env->NewGlobalRef(localClass));
args = env->NewObjectArray(len, objClass, 0);
return args;
}
The java part function as follow:
String[] Simplejni();
The error message as follow:
JNI DETECTED ERROR IN APPLICATION: attempt to return an instance of java.lang.Object[] from com.test.Simplejni
Please advise any suggestion how to investigate this issue, thank you.
Update: 20150427
I try to simpreturn empty jobjectArray which works in Android 4.4 but failed in Android 5.0 (with the same code)
My IDE is Android Studio
I found a way to solve this:
you just replace java/lang/Object with your java object class on API 21+, for example,
jclass localClass = env->FindClass("com/example/YourLocalClass");,
From the logs, we can know the object class is not the instance of yourJavaLocalObject class.

Sporadically UnsatisfiedLinkError with Android NDK

I'm looking for clues why my Android app sporadically throws an UnsatisfiedlinkError exception on a specific ndk call:
I have an Android app that loads one native library through ndk.
The native library is written in C++ and uses STL (in my makefile I have set APP_STL := stlport_static). No other libraries are used or required. The native library has about 300 native methods for about 20 java classes.
All works fine, most of the time. But every now and then my users get an UnsatisfiedLinkError exception. The exception is always triggered at the same spot with the same native function call, but strangely enough it is not even the first call to a native function, not even the first call to that specific native function.
Any clue would be welcome why one specific native call could fails. Can ndk easily be corrupted? How do you debug for such a problem?
Simple test programs with the same call work just fine. Nevertheless here's some parts of the source code:
Here's the problem ndk function in Java:
public class Server {
...
static private native long longObserveError( );
...
}
Here's the header file:
extern "C" {
...
JNIEXPORT jlong JNICALL Java_be_gentgo_tetsuki_Server_longObserveError
(JNIEnv *, jclass);
...
}
And here's the implementation:
IGSProtocol *server = 0; // initialized later...
JNIEXPORT jlong JNICALL Java_be_gentgo_tetsuki_Server_longObserveError
(JNIEnv *env, jclass)
{
Messenger &mess = server->ObserveError( );
return (long)(&mess);
}
What is the name of your library file? I came across another SO post (https://stackoverflow.com/a/18024816) where the guy had a library name that was conflicting with a system library that was only present on some devices. Renaming his library fixed the problem.

calling a constructor fails in JNI Android

I am trying to call a constructor from my JNI Android code; but somehow it fails with the following exception..
I am sure I am missing something really small ; but I am not able to figure out ... can anyone please point out ?
02-14 16:56:56.689: W/dalvikvm(397): JNI WARNING: JNI method called with exception raised
02-14 16:56:56.710: W/dalvikvm(397): in Lpv/ndk/Employee;.createWithAge (I)Lpv/ndk/Employee; (GetMethodID)
02-14 16:56:56.710: W/dalvikvm(397): Pending exception is:
02-14 16:56:56.710: I/dalvikvm(397): Ljava/lang/NoClassDefFoundError;: pv.ndk.Employee;
02-14 16:56:56.723: I/dalvikvm(397): at pv.ndk.Employee.createWithAge(Native Method)
02-14 16:56:56.740: I/dalvikvm(397): Caused by:
02-14 16:56:56.740: I/dalvikvm(397): Ljava/lang/ClassNotFoundException;: pv.ndk.Employee; in loader dalvik.system.PathClassLoader[/data/app/pv.ndk-2.apk]
Here is my code :
Employee class has 2 constructors and a method to return the age
Employee()
Employee(int age)
int getAge();
suprisingly when I invoke the method getAge()..the call goes through and fetches the age... But when I try to call the "Employee constructor" and then try to getAge() this exception is thrown...
somehow It is not able to find the class:
I have tried with all the possiblities I came across for GetObjectClass()
this works ; when I pass the Employee object in the call
JNIEXPORT jint JNICALL Java_pv_ndk_Employee_getAgeC(JNIEnv *env, jobject callingObject, jobject employeeObject)
{
jclass employeeClass = env->GetObjectClass(employeeObject);
jmethodID midGetName = env->GetMethodID(employeeClass, "getAge", "()I");
int age = env->CallIntMethod(employeeObject, midGetName);
//other code
}
THIS DOES NOT WORK ; I am not sure why it is not able to find the class..
I have tried with "Lpv/ndk/Emploee" "LEmployee" "pv/ndk/Employee" .. and even tried with the following approach:
jclass localRefCls = env->FindClass("pv/ndk/Employee");
jclass clazzEmployee = (_jclass*)env->NewGlobalRef(localRefCls);
JNIEXPORT jobject JNICALL Java_pv_ndk_Employee_createWithAge(JNIEnv *env, jobject callingObject, jint age) {
jclass employeeClass = env->FindClass("pv/ndk/Employee");
jmethodID midConstructor = env->GetMethodID(employeeClass, "<init>", "(I)V"); // BOOM SCREWED HERE .. !!
}
But everytime my code comes to env->GetMethodID - the vm Aborts ...
surprisingly the same code works today :)
probably it had to do with the way I was deploying the code... I believed that "auto build" should have taken care of it... Really sorry for creating confusion here regarding a working code snippet... I am new to NDK and JNI development ...
This definately works
jclass employeeClass = env->FindClass("pv/ndk/Employee");
//constructor call depicted by <init> and it's parameters (I) and return type V
jmethodID midConstructor = env->GetMethodID(employeeClass, "<init>", "(I)V");
I think the reason might have to do with the "native shared-library" not getting deployed after new modification... These are the steps I used to follow :
[1] I re-built the "Shared-library" with the modification ; using the "ndk-build"
__[2] Then I deploy the android project on my device
but seems Eclipse caches the "library" ??? [ please correct me if I am wrong ]
Hence the changes were taken into account...
Solution : [ possibly ]
[1] I rebuilt the library __ [2] I cleaned the whole project __ [3] then deployed the project on device...
FindClass method needs full class signature.
Note:if you have any inner classes / will not work,you have to use $
for example if your package name is A.B.C and your main class name is D and your subclass name is E then you have to use FindClass("LA/B/C/D$E;");

Categories

Resources