使用 Java 反射加载 Android Native 库
我正在尝试使用 Java 反射加载 Android 本机库,但在运行时找不到该库。通常我使用以下命令加载本机库:
System.loadLibrary("mylib");
一切都按预期工作。现在我尝试使用反射调用 loadLibrary
方法,如下所示:
Class<?> system = Class.forName("java.lang.System");
Method loadLibrary = system.getDeclaredMethod("loadLibrary", String.class);
loadLibrary.invoke(null, "mylib");
但在运行时我得到:
Caused by: java.lang.UnsatisfiedLinkError: Library mylib not found; tried [/system/lib64/libmylib.so, /vendor/lib64/libmylib.so]
at java.lang.Runtime.loadLibrary0(Runtime.java:1001)
at java.lang.System.loadLibrary(System.java:1530)
at java.lang.reflect.Method.invoke(Native Method)
似乎使用反射 loadLibrary
在错误的路径中查找库。这些/system/lib64:/vendor/lib64
是java.library.path
属性的路径,但它不会在apk内部查找。当然,该库存在于最终的 apk 中,并且它位于正确的位置,适用于正确的 ABI。
注意
我无法使用,
System.load("absolute-path-to-lib/mylib.so");
因为从我调用它的地方我没有可用的路径,我没有任何Context
或Application< /code> 尚无法检索路径。
您知道如何强制 loadLibrary
在当前 apk 中搜索吗?谢谢你!
I'm trying to load an Android native library with Java reflection, but at runtime the library is not found. Normally i load a native library with:
System.loadLibrary("mylib");
and everything works as expected. Now i try to call the loadLibrary
method with reflection, like this:
Class<?> system = Class.forName("java.lang.System");
Method loadLibrary = system.getDeclaredMethod("loadLibrary", String.class);
loadLibrary.invoke(null, "mylib");
but at runtime i get:
Caused by: java.lang.UnsatisfiedLinkError: Library mylib not found; tried [/system/lib64/libmylib.so, /vendor/lib64/libmylib.so]
at java.lang.Runtime.loadLibrary0(Runtime.java:1001)
at java.lang.System.loadLibrary(System.java:1530)
at java.lang.reflect.Method.invoke(Native Method)
It seems like with reflection loadLibrary
look for the library in the wrong path. These /system/lib64:/vendor/lib64
are the paths of java.library.path
property, but it does not look inside the apk. Of course the library is present in the final apk, and it's in the right place, for the right ABI.
NOTE
I can't use
System.load("absolute-path-to-lib/mylib.so");
because from where i'm calling this i don't have the path available, i don't have any Context
or Application
available yet to retrive the path.
Do you have any idea how to force loadLibrary
to search in the current apk ? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论