Android的JNI,如何加载与SONAME libxx.so.1.2.3库 [英] Android JNI, how to load library with soname libxx.so.1.2.3
问题描述
需要使用一个共享库从第三方的Android,的lib的SONAME和文件名相同,格式libxx.so.1.2.3,这是常见的Linux。我重命名的lib文件libxx.so,并使用NDK的构建中libmyjni.so链接libxx.so。在我的Java code,调用libmyjni.so功能之前,我加载它们是这样的:
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");
我libxx.so.1.2.3手动复制到/ data /本地的/ tmp /。它运作良好,这样一来,上面加载后,我可以调用函数libmyjni.so。
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.
在code的System.loadLibrary(myjni);,系统总是试图从什么地方得到libxx.so.1.2.3。我想知道,在现实世界中,我怎么会在安装过程中复制libxx.so.1.2.3的特定位置的Android设备上?让我能System.load()吧。
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.
或Android已安装自制的lib到/ System / lib目录/?
Or android has official way to install self made lib to /system/lib/?
如果libxx.so.1.2.3在格式libxx.so然后我可以使用的System.loadLibrary(XX)来加载它。
If libxx.so.1.2.3 is in format libxx.so then I can use System.loadLibrary("xx") to load it.
推荐答案
下面是最后我做什么,保持不变libxx.so.1.2.3,做所有的东西在Java中:
Here is finally what I did, keep the libxx.so.1.2.3 untouched and do all stuff in Java:
把libxx.so.1.2.3 android系统中的资产。
在MainApp.OnCreate(),将文件复制到专用文件夹,并从那里加载它:
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);
这篇关于Android的JNI,如何加载与SONAME libxx.so.1.2.3库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!