android中間件開發,本來利用NDK就直接生成了一個SO文件,然後直接打包到APK裏,即可運行,但是由於一般真機是不帶有root權限的,即使SSH可以解決root權限,當到了java層,root權限又失效了。經常碰到operation not permitted 或 permission delied 之類的錯誤,但目前還有一個可以運行在真機上的C編譯器,因爲手機的資源畢竟是很有限的,可喜的是android-NDK給我們提供了這個交叉編譯環境,只是生成SO文件的時候,把裏面的細節全部省掉了。不過還是保留了許多中間過程的開發HTML文檔。下面說說具體的實現過程:
1,下載NDK,並配合NDK環境變量爲NDK的安裝路徑
2,根據NDK裏docs文檔裏的standalone-toolchain.html來抽取交叉編譯的環境。
3,配置SYSROOT環境變量: SYSROOT=$NDK/platforms/android-8/arch-arm //android-8是你的android開發版本所定
4,然後運行命令:
$NDK/build/tools/make-standalone-toolchain.sh --platform=android-5 --install-dir=/tmp/my-android-toolchain //默認是arm,x86:--arch=x86
/tmp/my-android-toolchain是你交叉編譯環境的複製路徑。最好別放在tmp目錄裏,因爲重啓機子就立即消失了。這個新生成的文件 夾即是你的交叉編譯環境
5,配置PAHT和CC環境變量:
export PATH=/tmp/my-android-toolchain/bin:$PATH
export CC=arm-linux-androideabi-gcc //x86 export CC=i686-android-linux-gcc
如果需要長久有效,在/etc/profile里加上PATH=/..../bin:$PATH
export PATH
其他的類似處理。
6,把你要需要編譯的C文件放在以上生成的編譯環境的bin目錄下,例如:/tmp/my-android-toolchain/bin,進入這個目錄裏,輸入命令: $CC -o hello hello.c (這相當於我們平時的gcc命令,只是這裏需要引用arm裏的庫,而不是X86裏的庫)
hello.c:
#include<string.h>
#include<stdio.h>
int main()
{
printf("hello,toolchain!/n");
return 0;
}
即生成了可在手機arm裏運行的可執行文件hello
7,把這個可執行文件hello放到手機裏的/data/目錄下,方法有很多,這裏介紹兩種:
一、把這個文件放在電腦的某個目錄下,然後進入DOS的這個目錄下,運行如下命令:adb push hello /data/
二、放在SDCARD目錄下,然後執行cp /sdcard/hello /data/也可以
8,進入/data/目錄下,運行這個hello文件,例如“./hello”,即可以在adb shell裏看到打印結果: hello,toolchain!
隨着r5中對native activity的支持,對ndk的關注比較緊,多麼希望對c和c++更多的支持啊。下面是翻譯自ndk的CHANGES.html中對r6的描述。
android-ndk-r6
IMPORTANT CHANGES:
- Official support for the x86 ABI.
This release of the Android NDK now provides support for the 'x86' ABI.
This allows you to generate machine code that runs on future x86-based
Android devices.
Note that by default, code is still generated for ARM-based devices.
You can however add 'x86' to your APP_PLATFORM definition in your
Application.mk. For example, the following line instructs ndk-build
to build your code for three distinct ABIs:
APP_ABI := armeabi armeabi-v7a x86
Unless you rely on ARM-based assembly sources, you shouldn't need to touch
your Android.mk files to build x86 machine code.
For all details regarding x86 support, please read the new documentation
file named docs/CPU-X86.html.
Don't hesitate to file NDK bugs related to x86 at http://b.android.com
- You can build a standalone x86 toolchain using the --toolchain=x86-4.4.3
option when calling make-standalone-toolchain.sh. See
docs/STANDALONE-TOOLCHAIN.html for more details.
- The new 'ndk-stack' tool can be used to translate stack traces
(as reported by adb logcat in case of crash in native code) into
something more readable, i.e. containing function / source file /
line number information corresponding to each stack frame.
For more information and usage example, see the new documentation
file docs/NDK-STACK.html
OTHER FIXES & CHANGES:
- The arm-eabi-4.4.0, which had been deprecated since NDK r5, has been
finally removed from the NDK distribution.
重要的變化:
-官方支持x86 ABI。
新發布的Android NDK 版本 現在提供了對x86 ABI的支持。
這允許你生成的機器碼在未來的基於x86架構的Android設備上運行。
注意,代碼仍然默認的生成基於ARM的設備。然而你可以在Application.mk中的APP_PLATFORM定義中添加‘x86’。
例如,下面一行指示ndk-build來構建你的代碼向三種不同的ABI:
APP_ABI := armeabi armeabi-v7a x86
除非你依賴基於ARM的彙編源,否則你不需要管Android.mk來構建x86機器碼。
關於對x86支持的所有細節,請閱讀新文檔docs目錄下面的CPU-X86.html。
有關x86的bugs,請不用遲疑的歸檔在http://b.android.com
- 你可以構建一個單獨的x86工具鏈,當調用make-standalone-toolchain.sh時使用 --toolchain=x86-4.4.3選項。
具體細節請看docs/STANDALONE-TOOLCHAIN.html。
- 新的“ndk棧”工具可以翻譯棧的痕跡(在native代碼crash時,adb logcat報告的)更多一些可讀信息。
即,包含函數/源文件/行數信息相應的每個棧的幀。
更多信息和用法示例,請閱讀新的文檔docs/NDK-STACK.html。
其它 FIXES & CHANGES:
- arm-eabi-4.4.0, 從NDK r5 已經過時(deprecated),已經從NDK分佈上永遠的移除了。