首先我們要先要有幾樣東西要做準備:
- 下載Android NDK
- 若是系統為Windows的話要先下載Cygwin,因為我們等等會需要Linux指令,如果是OS X或Linux作業系統就可以直接使用指令操作
- 一台root過的手機
Step 1.
安裝NDK後,先到NDK下載的位置裡的的build/tools目錄底下,指令為:
cd <NDK>/build/toolsWindows指令為:
cd /cygdrive/c/<NDK>/build/tools/
Step 2.
進入tools目錄中,有一個檔案叫make-standalone-toolchain.sh,我們要利用這個Android官方的shell script產生toolchain,指令為:
./make-standalone-toolchain.sh --system=<system> --toolchain=<toolchain_version>--platform=<android_version> --install-dir=<install toolchain localtion> --ndk-dir=<NDK location>
<system>為你目前的作業系統,例如os x為darwin-x86_64,windows為windows-x86_64
<toolchain_version>為toolchain的版本,你可以到NDK目錄底下的toolchains查看有哪些版本,就目前最新的為arm-linux-androideabi-4.9
<android_version>在NDK目錄底下的platform可以查看有哪些版本可用,目前最新的為android-21
Step 3.
設定好我們的toolchain後,可以到我們剛剛<install toolchain localtion>的路徑下的bin資料夾裡去查看有個檔案叫arm-linux-androideabi-gcc,這就是arm complier執行檔。
若我們要在任何位置都可以使用此目錄我們必須設定環境變數,所以指令為:
echo "export PATH=\$PATH:<install toolchain localtion>/bin/" >> ~/.bash_profileWindows的指令為:
echo "export PATH=\$PATH:/cygdrive/c/<install toolchain localtion>/bin/" >> ~/.bash_profile
以上的設定做完以後,我們可以先做一個測試,先利用你的電腦寫出以下的程式碼然後產生c檔,我將此檔案命名為test.c
# include <stdio .h>
int main()
{
printf("Hello, world!\n");
return 0;
}
我們用arm-linux-androideabi-gcc產生手機能執行的bin檔指令為:
arm-linux-androideabi-gcc test.c -o test.bin這時我們就要用Android ADB來將bin檔push進入到手機裡,
這邊我們是push到tmp 的資料夾(注意資料夾和檔案的權限),指令為:
adb push test.bin <android phone push localtion>
./test.bin
參考資料:
http://www.ikravets.com/computer-life/programming/2014/01/18/an-android-cross-compiling-environment-on-windowsunix-like-systems-using-ndk
https://developer.android.com/ndk/guides/standalone_toolchain.html
小弟我第一次寫網誌,若有寫錯或有任何寶貴的意見,都可以跟我說,感謝你~


沒有留言:
張貼留言