I have libmath.so file which have native methods in it. I need to call the native methods in my android code. I have created a sample android application and added the libmath.so inside libs/armeabi folder, Then ran "Right-click mouse" -> Android Tools -> Add native support. Now the following files with below content are created inside jni folder of the application.
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libmath
LOCAL_SRC_FILES := libmath.cpp
include $(BUILD_SHARED_LIBRARY)
libmath.cpp
#include <jni.h>
When I run the Project as android application it shows as follows in the console :-
17:58:41 **** Build of configuration Default for project math ****
"F:\\Vinay\\Softwares\\android-ndk-r10d\\ndk-build.cmd" all
[armeabi] Compile++ thumb: math <= libmath.cpp
[armeabi] StaticLibrary : libstdc++.a
[armeabi] SharedLibrary : libmath.so
[armeabi] Install : libmath.so => libs/armeabi/libmath.so
17:58:53 Build Finished (took 11s.695ms)
Here is the code how I am loading the library:-
public class MathJni {
static {
System.loadLibrary("math");
}
public native String calc(String paramString);
}
public static final MathJni math = new MathJni();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s= math.calc("help");
}
Once after running the android application when I launch the application through emulator I am getting the below error.
02-06 07:38:36.900: D/dalvikvm(831): Trying to load lib /data/app-lib/com.example.math-2/libmath.so 0xb3cd0848
02-06 07:38:36.900: D/dalvikvm(831): Added shared lib /data/app-lib/com.example.math-2/libmath.so 0xb3cd0848
02-06 07:38:36.910: D/dalvikvm(831): No JNI_OnLoad found in /data/app-lib/com.example.math-2/libmath.so 0xb3cd0848, skipping init
02-06 07:38:37.540: W/dalvikvm(831): No implementation found for native Lcom/example/math/MathJni;.calc:(Ljava/lang/String;)Ljava/lang/String;
02-06 07:38:37.540: D/AndroidRuntime(831): Shutting down VM
02-06 07:38:37.550: W/dalvikvm(831): threadid=1: thread exiting with uncaught exception (group=0xb3a20ba8)
02-06 07:38:37.570: E/AndroidRuntime(831): FATAL EXCEPTION: main
02-06 07:38:37.570: E/AndroidRuntime(831): Process: com.example.math, PID: 831
02-06 07:38:37.570: E/AndroidRuntime(831): java.lang.UnsatisfiedLinkError: Native method not found: com.example.math.MathJni.calc:(Ljava/lang/String;)Ljava/lang/String;
02-06 07:38:37.570: E/AndroidRuntime(831): at com.example.math.MathJni.calc(Native Method)
02-06 07:38:37.570: E/AndroidRuntime(831): at com.example.math.MainActivity.onCreate(MainActivity.java:16)
What can be the possible reason for this error.
if you have written no C++ code and only prebuilt .so files to use directly from Java, you don't have to use the NDK.
Simply drop the .so file inside your project, under libs/<abi>
for an eclipse project - under jniLibs/<abi>
for a gradle project.
Here what you did was to create a libmath NDK module with an almost empty content. When you built your project, the NDK generated a new libmath.so file with nothing from your initial library. So delete all your jni files and folders, copy your former .so files back to libs/<abi>
, and run your project again.
If you run into other issues, verify that your libmath.so implements jstring com_example_math_MathJni_calc(JNIEnv* env, jobject* obj)
, or check what you should declare native in the Java side to use your lib, with the right package name and signature (if there was a documentation that came along with your .so files, it should state that).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With