java 如何在android上加载本机库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11791689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:22:50  来源:igfitidea点击:

How to load native library on android?

javaandroidandroid-ndknative

提问by Arsen Zahray

I've got a .so library, which I've extracted from an other APK.

我有一个 .so 库,是从其他 APK 中提取的。

I've copied it into /libs/armeabi location in my project.

我已将其复制到项目中的 /libs/armeabi 位置。

In my class, I'm loading it with

在我的课堂上,我正在加载它

static {
    System.loadLibrary("mylib"); 
}

The app crashes with

该应用程序崩溃

08-03 07:52:08.089: E/AndroidRuntime(932): FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access0(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.UnsatisfiedLinkError: Couldn't load mylib: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:365)
at java.lang.System.loadLibrary(System.java:535)
at com.example.mytestandroidapp.MainActivity.<clinit>(MainActivity.java:11)
08-03 07:52:08.089: E/AndroidRuntime(932):  ... 15 more

I'm doing this in eclipse.

我在日食中这样做。

Here's the screen of the layout of my project:

这是我的项目布局的屏幕:

enter image description here

在此处输入图片说明

What should I do for this to start working?

我应该怎么做才能开始工作?

采纳答案by Hans Hardmeier

"rename mylib.soin libmylib.so, and run your programm again (without modify the loadLibrary argument)" from blackbelt

“重命名mylib.solibmylib.so,并再次运行PROGRAMM(不修改调用LoadLibrary参数)”黑带从

+

+

The folder libs/armeabi-v7ashould also contain the libmylib.solibrary. If your phone is a new one, he will check there for libraries.

该文件夹libs/armeabi-v7a还应包含libmylib.so库。如果您的手机是新手机,他会在那里查看图书馆。

回答by Blackbelt

rename mylib.soin libmylib.so, and run your programm again (without modify the loadLibrary argument)

重命名mylib.solibmylib.so,并再次运行PROGRAMM(不修改调用LoadLibrary参数)

回答by Artis JIANG

Try the lib location path refer to:

试试lib位置路径参考:

context.getApplicationInfo().dataDir + "/lib"

context.getApplicationInfo().dataDir + "/lib"

and if you use API level 9, alternatively directly go to the lib directory with

如果您使用 API 级别 9,或者直接转到 lib 目录

context.getApplicationInfo().nativeLibraryDir

context.getApplicationInfo().nativeLibraryDir