標籤:android compiler android ndk shell
1.編譯可執行程式
1.1 通過mk指令碼編譯目錄結構:
mk_appjnimain.cAndroid.mk
Android.mk內容十分滿簡單:
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE:= main_execLOCAL_SRC_FILES := main.cinclude $(BUILD_EXECUTABLE)
命令下,cd到jni的上一級目錄,然後執行ndk-build命令:
E:\GitHub\ndk_tutorial\android_build_c_exec\mk_ap>ndk-build[armeabi] Compile thumb : main_exec <= main.c[armeabi] Executable : main_exec[armeabi] Install : main_exec => libs/armeabi/main_exec
在jni同級目錄下, libs/armeabi/下產生可執行程式main_exec
1.2 通過android ndk提供的arm-linux-androideabi-gcc.exe工具編譯目錄結構:
gcc_appmain.c
命令列,cd到gcc_app目錄下,然後執行下面命令:
D:\android-ndk-r9b-windows-x86\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-gcc.exe --sysroot=D:\android-ndk-r9b-windows-x86\platforms\android-13\arch-arm -o main_exec main.c
在目前的目錄下產生main_exec可執行程式。
--sysroot是執行系統的include和lib目錄,去掉會報一堆定義找不到錯誤。具體可以gcc --help查看。
2.adb將main_exec拷貝到手機上執行具體操作如下:
E:\GitHub\ndk_tutorial\android_build_c_exec\gcc_app>adb push main_exec /sdcard/1528 KB/s (6260 bytes in 0.004s)E:\GitHub\ndk_tutorial\android_build_c_exec\gcc_app>adb shell[email protected]:/ $ cp /sdcard/main_exec /data/local/cp /sdcard/main_exec /data/local/cp: /data/local/main_exec: Permission denied1|[email protected]:/ $ cp /sdcard/main_exec /data/local/tmpcp /sdcard/main_exec /data/local/tmp[email protected]:/ $ cd /data/local/cd /data/local/[email protected]:/data/local $ cd tmpcd tmp[email protected]:/data/local/tmp $ ls -l main_execls -l main_exec-rwxr-x--x shell shell 6260 2014-06-06 16:30 main_exec[email protected]:/data/local/tmp $ chmod 751 main_execchmod 751 main_exec[email protected]:/data/local/tmp $ ./main_exec./main_exechello world
通過adb push到sdcard上,然後進入shell模式,從sdcard複製到/data/local/tmp 下(注意我的n4手機上/data/local下許可權不夠,進入tmp可以)。chmod 751 設定可執行程式許可權,然後執行。!。
3.參考1. http://stackoverflow.com/questions/21424366/how-to-use-arm-linux-androideabi-compiler2. http://stackoverflow.com/questions/9868309/how-can-i-run-c-binary-executable-file-in-android-from-android-shell
相關代碼:https://github.com/dizuo/ndk_tutorial/tree/master/android_build_c_exec