Tips in developing with NDK
Building android native application
(Referred to following link: http://software.intel.com/en-us/articles/ndk-android-application-porting-methodologies/)
0. Prerequisites
1. Need to install Tegra android development pack from here
2. Install latest cygwin with development tools including gcc, unzip and etc.
1. Download a zipped source code from here.
2. Build apk file
<Added>
Set environment variable ANDROID_HOME to $ANDROID_SDK_DIR
</Added>
a. $ ndk-build NDK_DEBUG=1 APP_ABI="armeabi armeabi-v7a x86"
b. $ android.bat update project --path .
If you see a error something like "Error: The project either has no target set or the target is invalid.", then you need to define what target you want to use using "--target" option. For example,
b-1. $ android.bat update project --path . -t android-15
c. $ ant -f build.xml debug
<fix>
f-1. If the compilation fails here with message "sdk.dir is not defined" then use following command to define sdk.dir manually.
ant -Dsdk.dir=$ANDROID_HOME -f build.xml debug
f-2. With cygwin env, check all project folder including subdirectory set to readable and writable.
$(PROJECT_HOME) chmod -R a+rw *
</fix>
[Otional 1] If you want to check whether generated apk is valid or not, run below command and see the output message. Below shows an example.
o-1.
$ unzip -l bin/HelloGdbServer-debug.apk
Archive: bin/HelloGdbServer-debug.apk
Length Date Time Name
--------- ---------- ----- ----
2200 06-09-2012 17:25 res/drawable/icon.png
864 06-09-2012 19:06 res/layout/main.xml
1500 06-09-2012 19:06 AndroidManifest.xml
1296 06-09-2012 19:06 resources.arsc
3976 06-09-2012 19:06 classes.dex
125208 04-24-2012 23:20 lib/armeabi/gdbserver
10052 06-09-2012 18:52 lib/armeabi/libhello-gdbserver.so
125208 04-24-2012 23:20 lib/armeabi-v7a/gdbserver
10060 06-09-2012 18:52 lib/armeabi-v7a/libhello-gdbserver.so
214328 04-24-2012 23:20 lib/x86/gdbserver
2504 06-09-2012 18:52 lib/x86/libhello-gdbserver.so
881 06-09-2012 19:06 META-INF/MANIFEST.MF
934 06-09-2012 19:06 META-INF/CERT.SF
776 06-09-2012 19:06 META-INF/CERT.RSA
--------- -------
499787 14 files
Debugging NDK application
In the project folder, run below command.
$ ndk-gdb --force --start --verbose
Android NDK installation path: /cygdrive/c/NVPACK/android-ndk-r8/
Using default adb command: /cygdrive/c/NVPACK/android-sdk-windows/platform-tools/adb
ADB version found: Android Debug Bridge version 1.0.29
Using ADB flags:
Using auto-detected project path: .
Found package name: com.example.hellogdbserver
ABIs targetted by application: x86
Device API Level: 15
Device CPU ABI: x86
Compatible device ABI: x86
Using gdb setup init: ./libs/x86/gdb.setup
Using toolchain prefix: /cygdrive/c/NVPACK/android-ndk-r8//toolchains/x86-4.4.3/prebuilt/windows/bin/i686-android-linux-
Using app out directory: ./obj/local/x86
Found debuggable flag: true
Found device gdbserver: /data/data/com.example.hellogdbserver/lib/gdbserver
Found data directory: '/data/data/com.example.hellogdbserver'
Found first launchable activity: .HelloGdbServer
Launching activity: com.example.hellogdbserver/.HelloGdbServer
## COMMAND: /cygdrive/c/NVPACK/android-sdk-windows/platform-tools/adb shell am start -n com.example.hellogdbserver/.HelloGdbServer
Starting: Intent { cmp=com.example.hellogdbserver/.HelloGdbServer }
Error while mapping shared library sections:
libOpenglSystemCommon.so: No such file or directory.
Error while mapping shared library sections:
gralloc.goldfish.so: No such file or directory.
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
(gdb) list
1 /*
2 * Copyright (C) 2010 Max Vilimpoc
(gdb) Line number 31 out of range; jni/hello-gdbserver.c has 30 lines.
c
Continuing.
[New Thread 2259]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 2259]
0xad5bd4af in Java_com_example_hellogdbserver_HelloGdbServer_invokeCrash (
env=0x8534500, clazz=0xb49b6c70) at jni/hello-gdbserver.c:29
29 *crasher = 0xdeaddead;
(gdb) bt
#0 0xad5bd4af in Java_com_example_hellogdbserver_HelloGdbServer_invokeCrash (
env=0x8534500, clazz=0xb49b6c70) at jni/hello-gdbserver.c:29
#1 0xb72a3b80 in ?? ()
#2 0x08534500 in ?? ()
Error case #1.
If you cannot see debug symbols, need to check gdb.debug file whether it has wrong line ending codes. If such case, then run below command to convert dos style line ending to Unix style so that gdb can parse it to display symbols correctly. After this conversion, restart ndk-gdb command above then you will see debug symbol.
$ tr -d '\012' < libs/x86/gdb.setup | tr '\015\015' '\012\012' > libs/x86/gdb.setup