2012년 3월 14일 수요일

Straightforward Android Native Executables

출처 : http://vilimpoc.org/blog/2010/09/26/straightforward-android-native-executables/


I spent some time trying to figure out how to build a native code "Hello, world!" program for Android and noticed that prior to the r4b release of the Android NDK, there is a lot of bad info out there on how to do this. Usually, it involves using some kind of unofficial cross-compiler, plus the entire source tree of the Android OS, and/or whatever other kludgy hacks like linking the executable statically to some kind of ARM libc. This seems bad. With the newest version of the NDK generating a proper gdbserver executable and gdb setup stubs, why anyone would want to do this by hand is beyond me.
There was a script that made sense at one point: agcc.pl (here), which essentially takes all of the Makefile fragments and command-line switches and wraps them for you, letting you treat the NDK's cross-compilers almost like a normal gcc, but this is probably still more work than it's worth.

The Easy Way

The easiest way to get the NDK to build a native executable for you is just to use
include $(BUILD_EXECUTABLE)
at the end of your Android.mk, and then build like normal.
Anything else will just cause premature baldness.
Read on to see a fully-worked example...

hello-exe.c

1#include
2int main(int argc, char ** argv)
3{
4    printf("Hello, world!\n");
5    return 0;
6}

Android.mk

1LOCAL_PATH := $(call my-dir)
2 
3include $(CLEAR_VARS)
4 
5LOCAL_MODULE    := hello-exe
6LOCAL_SRC_FILES := hello-exe.c
7 
8include $(BUILD_EXECUTABLE)

Example build + install output (it really is that easy)

01vilimpoc@funky ~
02cd ~/android-ndk-r4b/samples/hello-exe
03 
04vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe
05ls
06jni
07 
08vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe
09$ ~/android-ndk-r4b/ndk-build -B
10Compile thumb  : hello-exe <= /home/vilimpoc/android-ndk-r4b/samples/hello-exe/jni/hello-exe.c
11Executable     : hello-exe
12Install        : hello-exe => /home/vilimpoc/android-ndk-r4b/samples/hello-exe/libs/armeabi
13 
14vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe
15ls
16jni  libs  obj
17 
18vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe
19cd libs/armeabi
20 
21vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe/libs/armeabi
22ls
23hello-exe
24 
25vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe/libs/armeabi
26$ adb devices
27List of devices attached
28emulator-5554   device
29 
30vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe/libs/armeabi
31$ adb -e shell
32# mkdir /data/data/hello-exe
33# ls /data/data/hello-exe
34# exit
35 
36vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe/libs/armeabi
37$ adb -e push hello-exe /data/data/hello-exe/hello-exe
38427 KB/s (0 bytes in 13670.000s)
39 
40vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe/libs/armeabi
41$ adb -e shell
42# cd /data/data/hello-exe
43# ls -l
44-rw-rw-rw- root     root        13670 2010-09-23 12:37 hello-exe
45# chmod 777 ./hello-exe
46# ls -l
47-rwxrwxrwx root     root        13670 2010-09-23 12:37 hello-exe
48# ./hello-exe
49Hello, world!
50#
51# exit
52 
53vilimpoc@funky ~/android-ndk-r4b/samples/hello-exe/libs/armeabi
54$

댓글 없음:

댓글 쓰기