Mac で Android アプリをクロスコンパイル
http://android.git.kernel.org/ にある prebuilt な toolchain を使って
Android (ARM-EABI) のクロスコンパイルが出来るようになったのでメモ
Java じゃなくて C を使ったネイティブアプリの話です
実行ファイルの場合
$ vi hello.c #include <stdio.h> int main() { printf("hello world\n"); } $ arm-eabi-gcc -I/Volumes/android/master/bionic/libc/include -I/Volumes/android/master/bionic/libc/arch-arm/include -I/Volumes/android/master/kernel/include -I/Volumes/android/master/bionic/libc/kernel/arch-arm -L/Volumes/android/master/out/target/product/generic/obj/lib -Wl,-dynamic-linker,/system/bin/linker ,-rpath-link=/Volumes/android/master/out/target/product/generic/obj/lib ,-T,/Volumes/android/master/build/core/armelf.x -nostdlib -lc /Volumes/android/master/out/target/product/generic/obj/lib/crtbegin_dynamic.o /Volumes/android/master/out/target/product/generic/obj/lib/crtend_android.o hello.c -o hello $ adb push hello /data $ adb shell # cd /data # ./hello hello world
共有ライブラリの場合
$ vi libhello.c #include <stdio.h> void hello() { printf("hello library world\n"); } $ arm-eabi-gcc -I/Volumes/android/master/bionic/libc/include -I/Volumes/android/master/bionic/libc/arch-arm/include -I/Volumes/android/master/kernel/include -I/Volumes/android/master/bionic/libc/kernel/arch-arm -L/Volumes/android/master/out/target/product/generic/obj/lib -Wl,-shared,-T,/Volumes/android/master/build/core/armelf.xsc -nostdlib -lc -fPIC libhello.c -o libhello.so $ vi test.c int main() { hello(); } $ arm-eabi-gcc -I/Volumes/android/master/bionic/libc/include -I/Volumes/android/master/bionic/libc/arch-arm/include -I/Volumes/android/master/kernel/include -I/Volumes/android/master/bionic/libc/kernel/arch-arm -L/Volumes/android/master/out/target/product/generic/obj/lib -Wl,-dynamic-linker,/system/bin/linker ,-rpath-link=/Volumes/android/master/out/target/product/generic/obj/lib ,-T,/Volumes/android/master/build/core/armelf.x -nostdlib -lc /Volumes/android/master/out/target/product/generic/obj/lib/crtbegin_dynamic.o /Volumes/android/master/out/target/product/generic/obj/lib/crtend_android.o -L. -lhello test.c -o test $ adb push libhello.so /data $ adb push test /data $ adb shell # export LD_LIBRARY_PATH=/data:$LD_LIBRARY_PATH # cd /data # ./test hello library world
コンパイルオプションが長いので適当に改行を入れてます。本当は1行です。(実際は環境変数とか使って楽してます)
面倒なので agcc スクリプトを使うのが便利
Android Tricks: Hello World C program using Android Toolchain
agcc を使うとこんな感じ
$ agcc -shared -fPIC libhello.c -o libhello.so $ agcc -L. -lhello test.c -o test