使用Android Studio编写NDK问题
1. 不能正常加载编译出的so文件,异常如下
02-18 21:18:50.241 17027-17027/com.android.encrypt E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at com.android.encrypt.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$600(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1235)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5071)
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:808)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsatisfiedLinkError: Couldn‘t load Encrypt from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.android.encrypt-2.apk,libraryPath=/data/app-lib/com.android.encrypt-2]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:365)
at java.lang.System.loadLibrary(System.java:535)
at com.android.encrypt.CommonEncrypt.<clinit>(CommonEncrypt.java:20)
at com.android.encrypt.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$600(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1235)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5071)
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:808)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
at dalvik.system.NativeStart.main(Native Method)
分析之后,是因为编译出的so没有打包到apk中,因此是Android Studio的打包过程可能出现了问题
查阅相关资料之后,发现打包相应的jar或者so,在build.gradle文件中配置,相应参考文档:http://tools.android.com/tech-docs/new-build-system/user-guide
要把so库打包到apk中,需要在build.gradle加入下面内容:
task copyNativeLibs(type: Copy) {
from fileTree(dir: ‘libs‘, include: ‘armeabi/*.so‘) into ‘build/lib‘
}
tasks.withType(Compile) {
compileTask -> compileTask.dependsOn copyNativeLibs
}
clean.dependsOn ‘cleanCopyNativeLibs‘
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniFolders = [new File(buildDir, ‘lib‘)]
}
2. java层找不到native方法,异常如下:
02-18 21:27:21.802 18785-18785/com.android.encrypt E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: Native method not found: com.android.encrypt.CommonEncrypt.encrypt:([B)[B
at com.android.encrypt.CommonEncrypt.encrypt(Native Method)
at com.android.encrypt.CommonEncrypt.Encrypt(CommonEncrypt.java:51)
at com.android.encrypt.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$600(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1235)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5071)
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:808)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
at dalvik.system.NativeStart.main(Native Method)
02-18 21:27:21.862 18785-18785/com.android.encrypt I/Process﹕ Sending signal. PID: 18785 SIG: 9
解决方法:如果是C++文件(.cpp或者.cc),要使用extern "C" { } 把本地方法括进去。
3.待续
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。