4

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.

Vinay
  • 705
  • 2
  • 7
  • 22
  • 1
    android code? Since when is native code not android code? – Deduplicator Feb 06 '15 at 12:54
  • 1
    You haven't shown us your C++ code. – Michael Feb 06 '15 at 12:57
  • Michael is extremely right. Despite lack of information, I still guess you don't have a calc method in MathJni class which acceps a java.lang.String instance as parameter... – Géza Török Feb 06 '15 at 13:00
  • I don't have any c++ code. when i add the .so to android project and ran the "Android Tools -> Add native support" the .cpp file was generated and it is what I have attached here. – Vinay Feb 06 '15 at 14:13
  • Sorry I forgot to add public native String calc(String paramString); line in MathJni class. Now i have added it. – Vinay Feb 06 '15 at 14:16

1 Answers1

3

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).

ph0b
  • 14,353
  • 4
  • 43
  • 41
  • When I try as mentioned it is giving the below error in logcat while lanching the app. 02-06 10:34:02.840: E/AndroidRuntime(1259): java.lang.UnsatisfiedLinkError: Couldn't load math from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.math-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.math-1, /system/lib]]]: findLibrary returned null – Vinay Feb 06 '15 at 15:36
  • Are you still using System.loadLibrary() ? your error makes me believe you switched to System.load() instead. you should get more debug information from your logcat than `findLibrary returned null`. Also, check that your .so files are correctly integrated into your apk by opening it as a zip file, and verify there are inside `lib/`. – ph0b Feb 06 '15 at 15:40