How to Import a Native Library (.So File) into Eclipse

How do I import a native library (.so file) into Eclipse?

See how they set things up in the sample project:
http://code.google.com/p/apv/source/browse/#hg%2Fpdfview

This NDK tutorial may also be useful in terms of helping you figure out how things work with the NDK:
http://mobile.tutsplus.com/tutorials/android/ndk-tutorial/

The basics are this:

  1. The .so library files typically go in the project_root_dir/libs subfolder. Also, generally they are in further subfolders that describe their architecture (e.g. project_root_dir/libs/armeabi/libpdfview2.so).

  2. To use the library in an activity you add a static library loader to the activity as shown below:

    static

    {

    System.loadLibrary("pdfview2"); // Notice lack of lib prefix

    }

  3. You then define the native functions you are importing. You can recognize these functions thanks to the native keyword. Look in the file below to see what functions they import in the sample:

http://code.google.com/p/apv/source/browse/pdfview/src/cx/hell/android/pdfview/PDF.java?r=560343d2dad904c5c925b6cadf97b90430fd25f4

Here are some examples:

private native int parseBytes(byte[] bytes);  
private native int parseFile(String fileName);
private native int parseFileDescriptor(FileDescriptor fd);

How to correctly load shared libraries into Eclipse

Just a short recap. I don't know your exact env. but I can reproduce and fix the issue similar to yours:

> git clone https://github.com/mkowsiak/jnicookbook.git
> cd jnicookbook/recipes/recipeNo023
> make
> make test
/usr/lib64/jvm/java/bin/java -Djava.library.path=:./lib -cp target recipeNo023.HelloWorld
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/test/workspace/jnicookbook/recipes/recipeNo023/lib/libHelloWorld.so: libAnotherFunction.so: cannot open shared object file: No such file or directory
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at recipeNo023.HelloWorld.<clinit>(HelloWorld.java:11)
Makefile:14: recipe for target 'test' failed
make: *** [test] Error 1

Now, let's see what happens with the lib

test@linux-875l:~/workspace/jnicookbook/recipes/recipeNo023> ldd lib/libHelloWorld.so 
linux-vdso.so.1 (0x00007ffd34936000)
libAnotherFunction.so => not found
libc.so.6 => /lib64/libc.so.6 (0x00007f470c182000)
/lib64/ld-linux-x86-64.so.2 (0x0000556276681000)

It's not there. What we can do is to add it on the LD_LIBRARY_PATH

test@linux-875l:~/workspace/jnicookbook/recipes/recipeNo023> export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`/lib

and try again. Works.

test@linux-875l:~/workspace/jnicookbook/recipes/recipeNo023> make test/usr/lib64/jvm/java/bin/java -Djava.library.path=:/home/test/workspace/jnicookbook/recipes/recipeNo023/lib:./lib -cp target recipeNo023.HelloWorld
library: :/home/test/workspace/jnicookbook/recipes/recipeNo023/lib:./lib
Hello world!
Hello from another function!

What you can do - in Eclipse - is to provide location of your libs inside project settings:

Project -> Properties -> Java Build Path 
-> Libraries -> JRE System Library
-> Native library location -> Edit...
-> External folder

Update:

There still might be an issue if you don't have libgpc64.so on LD_LIBRARY_PATH.

There is one more thing you can try.

While building GMAapiJNI64.so, try to use following:

-Wl,-rpath=$LOCATION_OF_LIBGPC -lgpc64

This time, Eclipse should be able to properly start your code even though you don't have your lib in LD_LIBRARY_PATH

How to bundle a native library (.so + .jar) for reuse in new Eclipse Android projects?

My procedure was OK. I found the problem: my app was loading the lib and it was running the C++ code. By the way the crash happened early on a socket() call. I had to set the INTERNET permission into the manifest file.



Related Topics



Leave a reply



Submit