How to locate crash crash crash errors in ndk development using android studio
In the development of ndk, once the jni layer code has problems, it will print the following information, and then flip back directly. At this time, people are very mad. They can only trace the problem by printing logs in various jni functions, but the efficiency is too low. It is difficult to locate the problem when there are multiple threads.
The articles searched on the Internet to locate the ndk crash are all exemplified by eclipse. The so files generated at compile time are stored in the obj/local/armeabi/directory, but there is no obj/local/armeabi/directory at all if developed with android studio.
So here's how to locate the ndk crash in android studio
- First, we need to change the show only selected application option in Logcat to No Filters (red box 1), then we can see the DEBUG information printed by the system.
- Backtrace (red box 2) is found in the DEBUG information, which is the crash information given by the system, but only the function is given, not the crash caused by that line of code.
- Write down the information 00 PC 00013122 (red box 3), which is used to locate the address of the crash code
To locate the address of the crash code accurately, we need to use the adrr2line tool in the ndk toolkit.
- First of all, find this tool. linux system is located in the following position
/home/gavinandre/Documents/Android/android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-addr2line
You can use this command in any directory by making a soft link in that directory.
sudo ln -s arm-linux-androideabi-addr2line /usr/local/bin/addr2line
- Find the address of the so file that caused the crash and search the so file in the app directory
$ find -name "libsocket_camera-lib.so"
./build/intermediates/bundles/debug/jni/armeabi-v7a/libsocket_camera-lib.so
./build/intermediates/bundles/default/jni/armeabi-v7a/libsocket_camera-lib.so
./build/intermediates/cmake/release/obj/armeabi-v7a/libsocket_camera-lib.so
./build/intermediates/cmake/debug/obj/armeabi-v7a/libsocket_camera-lib.so
./build/intermediates/transforms/mergeJniLibs/release/folders/2000/3/main/lib/armeabi-v7a/libsocket_camera-lib.so
./build/intermediates/transforms/mergeJniLibs/debug/folders/2000/3/main/lib/armeabi-v7a/libsocket_camera-lib.so
./build/intermediates/transforms/stripDebugSymbol/release/folders/2000/3/main/lib/armeabi-v7a/libsocket_camera-lib.so
./build/intermediates/transforms/stripDebugSymbol/debug/folders/2000/3/main/lib/armeabi-v7a/libsocket_camera-lib.so
You can see that android studio compiles and generates a lot of so files. The so files that I've learned correctly are generally this: build/intermediates/transforms/merge JniLibs/release/folders/2000/3/main/lib/armeabi-v7a/libsocket_camera-lib.
Then you can use the addrline command in the format of addr2line-e file location crash address (red box 3: 00013122)
addr2line -e build/intermediates/transforms/mergeJniLibs/release/folders/2000/3/main/lib/armeabi-v7a/libsocket_camera-lib.so 00013122
If the so file is correct, the following information will be printed: socket_camera.cpp is the crashed CPP file, 304 is the number of rows, and then check whether the location is in the function given in DEBUG information (red box 4).
/home/gavinandre/Documents/Workspace/AndroidStudioProject/DisinfectionRobot/nvadtslibrary/src/main/cpp/nvadts/video_engine.cpp:304
If the so file is wrong, it will print a question mark or an incorrect position, and then it is necessary to change the so file and try more.
??:?