Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android JNI, how to load library with soname libxx.so.1.2.3

Need to use a shared lib for android from 3rd party, the lib's soname and file name are same, in format libxx.so.1.2.3, which is common on linux. I rename the lib file to libxx.so, and link libxx.so in libmyjni.so using ndk-build. In my java code, before calling the functions in libmyjni.so, I load them like this:

    System.load("/data/local/tmp/libxx.so.1.2.3");
    System.loadLibrary("myjni");

I have to manually copy libxx.so.1.2.3 to /data/local/tmp/. It works well in this way, after above loading, I can call functions in libmyjni.so.

In code "System.loadLibrary("myjni");", system always trying to get libxx.so.1.2.3 from somewhere. I want to know, in the real world, how could I copy libxx.so.1.2.3 to a specific location on android device during installation? so that I can System.load() it.

Or android has official way to install self made lib to /system/lib/?

If libxx.so.1.2.3 is in format libxx.so then I can use System.loadLibrary("xx") to load it.

like image 258
brewphone Avatar asked Jul 09 '12 03:07

brewphone


1 Answers

Here is finally what I did, keep the libxx.so.1.2.3 untouched and do all stuff in Java:

Put libxx.so.1.2.3 in android assets. In MainApp.OnCreate(), copy the file to private file folder and load it from there:

    AssetManager am = applicationContext.getAssets(); 
    in = am.open(OPENSSL_SO_LIB_NAME); // source instream

    File privateStorageDir = applicationContext.getFilesDir();
    String libPath = privateStorageDir.getAbsolutePath();  // copy the lib to here ...

    System.load(libPath + "/" + SO_LIB_NAME);
like image 166
brewphone Avatar answered Nov 13 '22 23:11

brewphone