The so-called ndk native program has nothing to do with the upper-layer java application of Android, and does not need to involve jni to encapsulate the native interface. In general, it is to push the compiled pure C/C ++ program to the Android device or simulator, and then run the program on the device. Debugging is implemented through attach to gdbserver. We recommend that you run Ubuntu or mac osx. cygwin should be installed in windows to simulate the posix environment. The procedure is as follows:
0. Preparations
First, make sure that there is an Android development environment on the machine, such as downloading the Android SDK and the NDK package, configuring related environment variables, and starting Android devices or simulators. Of course, you also need to compile and generate the Native program with the Symbol debugging. I use the gcc compiler under the NDK to compile the program by configuring the compiling environment by myself, you can also directly use ndk-build.
1. Deploy gdbserver on the device
The so-called deployment is to copy the gdbserver under android ndk to the device. You can run the following command:
Adb push $ ANDROID_NDK_ROOT/prebuilt/android-arm/gdbserver/data
2. Deploy your Native program on the device
You need to deploy the compiled program and related so libraries on the device. Note that the so library should be placed under/system/lib, And the/system path is read-only by default, you can reset it through adb remount.
Adb push./myapp/data
Adb push./libmylib. so/system/lib
3. Copy the debugging environment on the device to the local device.
Because remote debugging requires some libraries on the target machine, copy the following files to the local folder.
Adb pull/system/lib./debugging/lib
Adb pull/system/bin/linker./debugging/lib
4. Run your program on the device through gdbserver
Adb shell gdbserver: 12345/data/myapp
5. Place the local TCP port forward to the TCP port of the device locally
Adb forward tcp: 12345 tcp: 12345
6. Run the gdb program in the Android ndk path locally.
$ ANDROID_NDK/toolchains/arm-linux-androideabi-4.4.3/prebuild/darwin-x86/bin/arm-linux-androideabi-gdb
Note that if you are using the Linux NDK package in Ubuntu, the path will be a bit different, the darwin-x86 should be linux-x86. The most safe or self-search under the NDK.
7. Start gdb and set the solib search path under gdb.
It enables gdb to find the lib related to debugging, that is, the files pulled from the device in step 3.
(Gdb) set solib-search-path./debugging/lib
8. Set the Native program you want to debug under gdb
(Gdb) file./myapp
9. Connect to the gdbserver of the device
(Gdb) target remote: 12345
The above 6 ~ You can also run the following command in Step 9:
$ ANDROID_NDK/toolchains/arm-linux-androideabi-4.4.3/prebuild/darwin-x86/bin/arm-linux-androideabi-gdb -- eval-command = "set solib-search-path. /debugging/lib "-- eval-command =" file. /myapp "-- eval-command =" target remote: 12345"
10. Start debugging.
Run the program through continue or c. Note that run is not used, because the program has actually started on the target machine, but break is in the program portal.
This article will not discuss the specific usage of gdb Debugging commands. There should be a lot of related articles on the internet, maybe I will write an article on how to use gdb later. In addition, you can configure compilation and Debugging commands to some general ides for ease of use, such as eclipse and codeblocks.