Android NDK - 无法在Android 17及更低版本上加载库:load_library

时间:2016-04-09 22:14:54

标签: android android-ndk

我使用的是实验性gradle插件,这是我在gradle文件中的NDK配置。

ndk {
    toolchain = "gcc"
    toolchainVersion = "4.9"
    moduleName = "libname"
    stl = "gnustl_shared"
    cppFlags.add("-std=c++11")
    cppFlags.add("-fexceptions")
    cppFlags.add("-frtti")
    ...
}

当我尝试使用以下代码

加载带有Android NDK的本机库时
static {
    System.loadLibrary("libname");
}

适用于Android API 18及更高版本,但它在Android API 17及更低版本上崩溃。

我收到以下错误日志

02-23 13:50:05.877 1937-1937/? D/dalvikvm: Late-enabling CheckJNI
02-23 13:50:05.905 1937-1944/? E/jdwp: Failed sending reply to debugger: Broken pipe
02-23 13:50:05.905 1937-1944/? D/dalvikvm: Debugger has detached; object registry had 1 entries
02-23 13:50:05.961 1937-1937/? D/dalvikvm: Trying to load lib /data/data/com.mycompany.helloworld/lib/libhelloworld.so 0xa6d15c60
02-23 13:50:05.961 1937-1937/? W/dalvikvm: Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/mycompany/helloworld/MainActivity;
02-23 13:50:05.961 1937-1937/? W/dalvikvm: Class init failed in newInstance call (Lcom/mycompany/helloworld/MainActivity;)
02-23 13:50:05.961 1937-1937/? D/AndroidRuntime: Shutting down VM
02-23 13:50:05.961 1937-1937/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa6296288)
02-23 13:50:05.969 1937-1937/? E/AndroidRuntime: 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.access$600(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: Cannot load library: load_library[1098]: Library     '/system/lib/libhoudini.so' not found
                                                     at java.lang.Runtime.loadLibrary(Runtime.java:370)
                                                     at java.lang.System.loadLibrary(System.java:535)
                                                     at com.mycompany.helloworld.MainActivity.<clinit>(MainActivity.java:15)
                                                     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.access$600(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) 
02-23 13:55:06.113 1937-1937/? I/Process: Sending signal. PID: 1937 SIG: 9

这是一个项目示例:

https://github.com/4brunu/djinni-hello-world

1 个答案:

答案 0 :(得分:4)

问题在于,当使用C ++运行时的共享库变体时,需要在其他库之前加载它,因此解决方案不是这样的:

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

使用此:

static {
    System.loadLibrary("gnustl_shared");
    System.loadLibrary("libname");         
}

在下面的链接中找到解决方案&#34;共享运行时&#34;:http://developer.android.com/ndk/guides/cpp-support.html#ic

  

如果您的应用针对早于Android 4.3的Android版本   (Android API级别18),您使用的是共享库变体   在给定C ++运行时,您必须先加载共享库   依赖于它的库。

     

例如,应用可能包含以下模块:

     

libfoo.so

     

libfoo.so

使用的libbar.so      

libstlport_shared.so,由libfoo和libbar使用

     

您必须以反向依赖顺序加载库:

static {
  System.loadLibrary("stlport_shared");
  System.loadLibrary("bar");
  System.loadLibrary("foo");
}

以下是工作的项目示例:https://github.com/4brunu/djinni-hello-world/commit/9c1e7aadd3a593419a6004c0528e1972d24f33c9

注意:C ++运行时有多个共享库变体,而不仅仅是gnustl_shared。可以在当前链接中找到当前库变体的列表: http://developer.android.com/ndk/guides/cpp-support.html#hr

相关问题