2009年11月5日 星期四

[2009-11-05-4] Android NDK - 6/49 大樂透

續用"6/49 大樂透"的source file(C language),改用Android NDK(Native Development Kit)來編譯Android應用程式。

Step1:
建立一個raylottery目錄(這裡用的是cupcake)
$ cd cupcake/development #移到android的development下
$ mkdir raylottery

Step2:
複製lottery.c到development. 可在這下載download here

Step3:
撰寫一個Android.mk(標準的檔名不要更動), 至於.mk的寫法可參考其他應該程式的寫法
$ find  ~/cupcake -name "Android.mk" > log_list_all  #找出cupcake下所有的Android.mk, 並存到 log_list_all

raylottery 下的Android.mk如下
# filename: Android.mk
# 2009-11-04, RayZ

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
    lottery.c
    
LOCAL_MODULE:= lottery_ray #module name
include $(BUILD_EXECUTABLE)

Step4:
編譯lottery_ray
$ cd ~/cupcake
$ make lottery_ray
編譯的結果會告知相對的文件位置: target thumb C(原始檔案), target Executable(可執行檔),target Non-prelinked, target Strip, Install。


查看一下lottery_ray 檔案格式, 確實是ARM, executable...
$ file rayout/target/product/generic/system/bin/lottery_ray 


Step5:
把產生的lottery_ray(四個位置的任一個皆可)放到adb shell來執行
$ adb devices #查看有那些device
$ adb shell mkdir /dev/ray_test  #在emulator-5554中做一個目錄
$ adb push rayout/target/product/generic/obj/EXECUTABLES/lottery_ray_intermediates/LINKED/lottery_ray /dev/ray_test #把lottery_ray 放到emulator-5554
$ adb shell  #進入adb shell
# cd /dev/ray_test
# ./lottery_ray


在AVD上用Terminal Emulator看到的畫面就和用adb shell一樣, 確實好玩。


ps:
1) 這裡已經把android 的out 目錄改為rayout,可以參考cupcake/build/buildspec.mk.default說明。
2) adb shell 的command可以在adb shell中的/system/bin找到。

[參考]
[1] 編譯 Native C 的 helloworld模塊
[2] 手工編譯 C 模塊
[3] Android Toolchain 與 Bionic Libc
[4] Google 釋出 Android 1.5 原生開發套件
[5] GNU toolchain
[6] Android 1.6 NDK, r1

沒有留言: