16

Folks,

I am working on an android application where I need a third party .so library. I built this third party library (with ndk-build) as per their instructions and was then looking to include this .so in to my Android project.

Therefore I followed the steps described in docs/PREBUILTS.html and successfully build the new .so in the jni/prebuilt directory. Now I tried leveraging the .so facilities by using it in a simple test android app. So what i do is :

static {
  Log.i("load so > ","load so");
  System.loadLibrary("xyz");
   }
/* The native functions */
private static native int openFile(String filename);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try{
        String path =  getPathForDownloadDirectoryFile();
        Log.i("file path> ", path);
        int num= openFile(path);
    }catch(Exception e){
        Log.e(">", "could not open the file");
    }
}

Now when I run my app I get a debug message saying : No JNI_OnLoad found in /data/data/com.example.myfirstapp/lib/xyz.so 0x411e6738, skipping init and then the application shuts down.

For More Info, Here is the error log :

No JNI_OnLoad found in /data/data/com.example.mysecondapp/lib/xyz.so 0x411e67a0,   skipping init
W/dalvikvm(  570): No implementation found for native    Lcom/example/mysecondapp/MainActivity;.openFile:(Ljava/lang/String;)I
D/AndroidRuntime(  570): Shutting down VM
W/dalvikvm(  570): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
E/AndroidRuntime(  570): FATAL EXCEPTION: main
E/AndroidRuntime(  570): java.lang.UnsatisfiedLinkError: Native method not found:  com.example.mysecondapp.MainActivity.openFile:(Ljava/lang/String;)I
E/AndroidRuntime(  570):    at com.example.mysecondapp.MainActivity.openFile(Native  Method)
E/AndroidRuntime(  570):    at   com.example.mysecondapp.MainActivity.onCreate(MainActivity.java:31)
E/AndroidRuntime(  570):    at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime(  570):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime(  570):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
E/AndroidRuntime(  570):    at    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime(  570):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
E/AndroidRuntime(  570):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
E/AndroidRuntime(  570):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  570):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(  570):    at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime(  570):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  570):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(  570):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime(  570):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(  570):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  146):   Force finishing activity com.example.mysecondapp/.MainActivity

As I could see that native implementation for the openFile() method was not found but the same xyz.so lib worked pretty neat with the original sample app from the third party. I am pretty much a starter with Android-ndk world.

Java-Android-NDK Ninjas ..any guess on what I might be missing ? I'll highly appreciate any help here :)

Bhuvnesh Pratap
  • 437
  • 1
  • 5
  • 16

4 Answers4

17

As guycole said "No JNI_OnLoad" is just a warning , your problem lies elsewhere .

As you mentioned you successfully compiled your "so" file , the problem may lie in your function signatures inside your c/C ++ code it should be something like this

JNIEXPORT jint JNICALL Java_com_your_package_class_method(JNIEnv *d, jobject e, jstring f)
{
//some action

}

The function signatures comes from the header file which is generated using javah tool.You need to generate header file and use the function signature with your package name. For different package and class names the header file and corresponding function signature will change .

worked pretty neat with the original sample app from the third party

This might be the reason its running on the sample app and not on your app.

refer: https://thenewcircle.com/s/post/49/using_ndk_to_call_c_code_from_android_apps

Sunny Kumar Aditya
  • 2,806
  • 4
  • 26
  • 38
  • right ! the package name could be the reason ! Let me try it out with proper packaging . Will get back to you then, thanks ! – Bhuvnesh Pratap Sep 27 '12 at 17:11
  • I build .so with new package structure which is my own but now I am getting Failure [INSTALL_FAILED_MISSING_SHARED_LIBRARY] error while installing the apk on device in-spite of the fact that xyz.so is present under Project_folder/libs/arm_folder . Any idea what could be behind this ? – Bhuvnesh Pratap Sep 27 '12 at 19:54
  • paste your make file and also try to run the example given in the link , you will get some clarity . – Sunny Kumar Aditya Sep 27 '12 at 19:56
  • 1
    It should be in **Project_folder/libs/armrabi**. You can also open the **.apk** file with WinRAR or other unzip tool, and see what **.so** files it contains. – Alex Cohn Sep 27 '12 at 22:59
  • 1
    I goofed up my AndroidManifest file and hence the error! things are working fine now . Thanks all :) – Bhuvnesh Pratap Sep 29 '12 at 14:59
6

The "No JNI_OnLoad" message is just a warning. JNI_OnLoad is an optional initialization hook.

I guess your problem is inside the openFile() method. Try commenting out the call from Java and see how far you get.

I have a blog post about JNI and some sample code at http://guycole.blogspot.com/2012/03/yet-another-android-ndk-blog-posting.html - perhaps you will find it useful.

Good luck.

guycole
  • 788
  • 5
  • 10
2

It also comes with this log

??-?? ??:??:??.???: INFO/(): java.lang.UnsatisfiedLinkError: Couldn't load *: findLibrary returned null

right??

I think it's the problem of android.mk files. 1:try to swith to armabi v7. 2:load funciton will call open(). check permission of the so.

0

As mentioned in the previous answers, No JNI_OnLoad is only a warning.

I had got similar problem, I figured the problem is because of file operations.

My app was not having external storage write permission.After adding the below code in manifest it was working fine

JK_S
  • 145
  • 1
  • 5