Android native development tool

来源:互联网 发布:e盾网络验证 死马 编辑:程序博客网 时间:2024/04/30 01:36

Android is the first free, open-source, and fully customizable mobile platform. Android provides a full stack including an operating system, middleware, and key mobile applications. It also contains a rich set of APIs that allows third-party developers to develop more applications. Android relies on Linux version 2.6 for core system services, such as security, memory management, process management, network stack, and driver model, and the minimum requirement for hardware is a 200MHz ARM processor. Therefore, if Android application developers want to build performance-critical portions of their apps in native code, they should build a set of cross-toolchains (compilers, linkers, etc.) that can generate native ARM binaries on Linux.

Android applications run in the Dalvik virtual machine, which limits the development of applications; however, Android official website has released a native development kit (Android NDK), which allows developing part of your applications using native-code languages such as C and C++. This is beneficial for developing certain classes of application in the terms of reusing existing codes and accelerating apps running speed. The NDK, however, only supports the ARMv5TE machine instruction set and provides stable headers for libc (the C library), libm (the Math library), and OpenGL ES (3D graphics library). If you desire an in-depth development on Android, a much more open and flexible native development tool is requested.

 

Android NDK provides toolchain source code, which can be downloaded fromhttp://android.git.kernel.org/. All we have to do is to rebuild it (to avoid troublesome compiling warnings and errors, you better to replace binutils-2.17 with binutils-2.18) and install it on Linux. To build the native development tool on Windows platform, Cygwin is highly recommended. Cygwin is a Linux-like environment for Windows. It provides a collection of tools that provide Linux-like look and functions. For Cygwin installation, please go to Cygwin websitehttp://www.cygwin.com/. During the installation, please make sure you select gcc, make, termcap, Flex, bison, gettext, gettext-devel, Texinfo, libiconv, and git. Git is a free open-source distributed version control system for downloading Android source code from http://android.git.kernel.org/. Generally, not all Android source code packages are needed. The core packages include 1) Android.platform.bionic, 2) Android.platform.build, 3) Android.platform.system.core, and 4) Android.platform.system.base. With toolchain installed and Android source code downloaded, it is time to configure our gcc compile environment. In common cases, gcc will search head files in the following paths:
$toolchain/lib/gcc/arm-eabi/4.2.1/include
$toolchain/arm-eabi/sys-include
$toolchain/arm-eabi/include
Therefore, for different development purpose, we only need to copy different head files from Android source code fold to the above paths. For example, to develop Bluetooth tools on Android, we should copy Bluetooth head files to $toolchain/lib/gcc/arm-eabi/4.2.1/include.

However, Android does not support glibc. It uses bionic Libc, which is written by Google, so we have to copy all bionic Libc head files to $toolchain/lib/gcc/arm-eabi/4.2.1/include. Bionic Libc head files are in the following paths:
bionic/libc/arch-arm/include
bionic/libc/include
bionic/libstdc++/include
bionic/libc/common
bionic/libm/include
bionic/libthread_db/include
and also copy AndroidConfig.h file from system/core/include/arch/linux-arm to $toolchain/lib/gcc/arm-eabi/4.2.1/include.

Now everything is ready but the most important thing—library files. How can we get precompiled library files? Here is a trick. As we know, Android emulator has all Android precompiled library files on it. We have to pull them from the emulator. We can use a tool like Busybox to do so following the example steps:
$adb push busybox /dev/tool/busybox
$adb shell chmod 777 /dev/ tool /busybox
$adb shell ./dev/ tool /busybox tar -cf /dev/ tool /libs.tar /system/lib
$adb pull /dev/ tool /libs.tar libs.tar
Unzip libs.tar, and copy all files to $toolchain/arm-eabi/lib. Meanwhile, please copy Makefiles, armelf.x and armelf.xsc, from build/core to it.

By now, toolchain setup is finished. Write a helloworld.c to test it.

int main(int argc, char **argv) {

printf(“Hello world!/n”);

return 0;
}

Compile it.
$ arm-eabi-gcc -nostdlib -Bdynamic -Wl,-T,armelf.x -Wl,-dynamic-linker,/system/bin/linker -include AndroidConfig.h -lc -o helloworld helloworld.c
Run it on Android emulator.
Bang! Warning: cannot find entry symbol _start; defaulting to 000082c8.
Bang! Bang! Error: Segmentation fault!

What is the problem? According to Android NDK makefile, the executable binary is dynamically linked. Therefore, crtbegin_dynamic.o and crtend.o are needed. If you could find Crtbegin_dynamic.S and crtend.S in $android_src/bionic/libc/arch-arm/bionic, you would get the answer. Use the following command to compile them.
arm-eabi-gcc -mthumb-interwork -o crtbegin_dynamic.o -c crtbegin_dynamic.S
arm-eabi-gcc -mthumb-interwork -o crtend.o -c crtend.S

Now compile your helloworld.c again.
$arm-eabi-gcc -nostdlib -Bdynamic -Wl,-T,armelf.x -Wl,-dynamic-linker,/system/bin/linker -include AndroidConfig.h -lc -o hello hello.c crtbegin_dynamic.o crtend.o
And then, push it to Android emulator and run it.
$adb push hello /dev/sample/helloworld
$adb shell chmod 777 /dev/sample/helloworld
$adb shell ./dev/sample/helloworld

See, life is wonderful now!

Reference:

http://developer.android.com/guide/index.html

http://benno.id.au/blog/

http://developer.android.com/sdk/ndk/index.html

P.S. About how to compile,run and package C programs in Android, please check this out.

原创粉丝点击