赞
踩
目录
JNI(Java Native Interface)意为JAVA本地调用,它允许Java代码和其他语言写的代码进行交互,
简单的说,一种在Java虚拟机控制下执行代码的标准机制。
Android NDK(Native Development Kit )是一套工具集合,允许你用像C/C++语言那样实现应用程序的一部分。
1)、安全性,java是半解释型语言,很容易被反汇编后拿到源代码文件,我们可以在重要的交互功能使用C语言代替。
2)、效率,C语言比起java来说效率要高出很多。
初始化错误;
访问错误;
内存泄露;
参数错误;
堆栈溢出;
类型转换错误;
数字除0错误;
数组越界(这个问题是最难排查,现象还千奇百怪)。
NDK安装包中提供了三个调试工具:addr2line、objdump和ndk-stack,其中ndk-stack放在$NDK_HOME目录下,与ndk-build同级目录。addr2line和objdump在ndk的交叉编译器工具链目录下,
下面是我本机NDK交叉编译器工具链的目录结构
方法一:ndk-stack
命令如下
adb logcat | ndk-stack -sym D:\wy\mySource\crashTest\app\build\intermediates\cmake\debug\obj\arm64-v8a\
运行后效果
- ********** Crash dump: **********
- Build fingerprint: 'OPPO/R9s/R9s:6.0.1/MMB29M/1522231161:user/release-keys'
- pid: 16844, tid: 16844, name: angyi.crashtest >>> com.wangyi.crashtest <<<
- signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1230
- Stack frame 08-16 14:46:43.201 754 754 F DEBUG : #00 pc 0000000000007a38 /data/app/com.wangyi.crashtest-1/lib/arm64/libnative-lib.so (_ZN7_JNIEnv13NewShortArrayEi+24): Routine _JNIEnv::NewShortArray(int) at D:/Tool/Android/Sdk/ndk-bundle/platforms/android-21/arch-arm64/usr/include/jni.h:9
- 05
- Stack frame 08-16 14:46:43.201 754 754 F DEBUG : #01 pc 0000000000007a04 /data/app/com.wangyi.crashtest-1/lib/arm64/libnative-lib.so (_Z9willCrashv+52): Routine willCrash() at D:\wy\mySource\crashTest\app\src\main\cpp/native-lib.cpp:17
- Stack frame 08-16 14:46:43.201 754 754 F DEBUG : #02 pc 0000000000007af0 /data/app/com.wangyi.crashtest-1/lib/arm64/libnative-lib.so (Java_com_wangyi_crashtest_MainActivity_stringFromJNI+108): Routine Java_com_wangyi_crashtest_MainActivity_stringFromJNI at D:\wy\mySource\crashTest\app\src
- \main\cpp/native-lib.cpp:32
- Stack frame 08-16 14:46:43.201 754 754 F DEBUG : #03 pc 00000000006b784c /data/app/com.wangyi.crashtest-1/oat/arm64/base.odex (offset 0x2b4000) (java.lang.String com.wangyi.crashtest.MainActivity.stringFromJNI()+128)
- Stack frame 08-16 14:46:43.201 754 754 F DEBUG : #04 pc 00000000006b7738 /data/app/com.wangyi.crashtest-1/oat/arm64/base.odex (offset 0x2b4000) (void com.wangyi.crashtest.MainActivity.onCreate(android.os.Bundle)+252)
- Stack frame 08-16 14:46:43.201 754 754 F DEBUG : #05 pc 0000000072e9ac10 /data/dalvik-cache/arm64/system@framework@boot.oat (offset 0x227b000)
- Stack frame 08-16 14:46:43.623 2863 4355 D ErrorSaveTask: #00 pc 0000000000007a38 /data/app/com.wangyi.crashtest-1/lib/arm64/libnative-lib.so (_ZN7_JNIEnv13NewShortArrayEi+24): Routine _JNIEnv::NewShortArray(int) at D:/Tool/Android/Sdk/ndk-bundle/platforms/android-21/arch-arm64/usr/include/jn
- i.h:905
- Stack frame 08-16 14:46:43.623 2863 4355 D ErrorSaveTask: #01 pc 0000000000007a04 /data/app/com.wangyi.crashtest-1/lib/arm64/libnative-lib.so (_Z9willCrashv+52): Routine willCrash() at D:\wy\mySource\crashTest\app\src\main\cpp/native-lib.cpp:17
- Stack frame 08-16 14:46:43.623 2863 4355 D ErrorSaveTask: #02 pc 0000000000007af0 /data/app/com.wangyi.crashtest-1/lib/arm64/libnative-lib.so (Java_com_wangyi_crashtest_MainActivity_stringFromJNI+108): Routine Java_com_wangyi_crashtest_MainActivity_stringFromJNI at D:\wy\mySource\crashTest\ap
- p\src\main\cpp/native-lib.cpp:32
- Stack frame 08-16 14:46:43.623 2863 4355 D ErrorSaveTask: #03 pc 00000000006b784c /data/app/com.wangyi.crashtest-1/oat/arm64/base.odex (offset 0x2b4000) (java.lang.String com.wangyi.crashtest.MainActivity.stringFromJNI()+128)
- Stack frame 08-16 14:46:43.623 2863 4355 D ErrorSaveTask: #04 pc 00000000006b7738 /data/app/com.wangyi.crashtest-1/oat/arm64/base.odex (offset 0x2b4000) (void com.wangyi.crashtest.MainActivity.onCreate(android.os.Bundle)+252)
- Stack frame 08-16 14:46:43.623 2863 4355 D ErrorSaveTask: #05 pc 0000000072e9ac10 /data/dalvik-cache/arm64/system@framework@boot.oat (offset 0x227b000), app_pack=com.wangyi.crashtest, client_time=2018-08-16 14:46:43}
可以快速定位到错误的代码
方法二:arm-linux-androideabi-addr2line
运行命令(00000235为报错的地址):
arm-linux-androideabi-addr2line -e D:\wy\mySource\crashTest\app\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so 00000235
方法三:arm-linux-androideabi-objdump
运行命令:
arm-linux-androideabi-objdump -S -D D:\wy\mySource\crashTest\app\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so > logcat.log
先写到这了,后面在补充native异常上传java层,有什么问题大家可以一起相互交流学习。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。