一、ndk下載
NDK 下載 ?|? Android NDK ?|? Android Developers
二、ndk環境變量配置
ndk解壓:
unzip android-ndk-r26d-linux.zip?
環境變量配置:
?export NDK_HOME=/rd/own/test/android-ndk-r26d/
?export PATH=$PATH:$NDK_HOME
三、編譯測試驗證
3.1 程序創建
創建helloworld, 在其下創建jni目錄,并準備好hello.c和Android.mk
rd@ubuntu:~/test/android-ndk-r26d$ cat helloword/jni/hello.c
#include <stdio.h>
int main (int argc,char *argv[])
{printf("hello world!");return 0;
}
rd@ubuntu:~/test/android-ndk-r26d$ cat helloword/jni/Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE ? ?:= helloworld
LOCAL_SRC_FILES := hello.c
#include $(BUILD_SHARED_LIBRARY)
include $(BUILD_EXECUTABLE)
編譯庫文件Android.mk參考:
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := FRE
LOCAL_SRC_FILES := FlashRuntime.so
include $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE := NativeQCAR
LOCAL_SRC_FILES := main.c
LOCAL_SHARED_LIBRARIES := FRE
include $(BUILD_SHARED_LIBRARY)
備注:如果不創建jni目錄,會報NDK_PROJECT_PATH 找不到。
Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
如果不創建jni目錄,則需要添加額外的配置( NDK_PROJECT_PATH、NDK_APPLICATION_MK和APP_BUILD_SCRIP),如下:
ndk-build NDK_PROJECT_PATH=/rd/test/android-ndk-r26d/helloword ?NDK_APPLICATION_MK=/rd/test/android-ndk-r26d/Application.mk
cat Application.mk
APP_BUILD_SCRIPT := /rd/test/android-ndk-r26d/helloword/Android.mk
3.2 編譯過程
~/test/android-ndk-r26d/build/ndk-build
Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-21.
[arm64-v8a] Compile ? ? ? ?: helloworld <= hello.c
[arm64-v8a] Executable ? ? : helloworld
[arm64-v8a] Install ? ? ? ?: helloworld => libs/arm64-v8a/helloworld
[armeabi-v7a] Compile thumb ?: helloworld <= hello.c
[armeabi-v7a] Executable ? ? : helloworld
[armeabi-v7a] Install ? ? ? ?: helloworld => libs/armeabi-v7a/helloworld
[x86] Compile ? ? ? ?: helloworld <= hello.c
[x86] Executable ? ? : helloworld
[x86] Install ? ? ? ?: helloworld => libs/x86/helloworld
[x86_64] Compile ? ? ? ?: helloworld <= hello.c
[x86_64] Executable ? ? : helloworld
[x86_64] Install ? ? ? ?: helloworld => libs/x86_64/helloworld
3.3 編譯結果,默認會編譯出不同架構的可執行文件
./helloword/libs/arm64-v8a/helloworld
./helloword/libs/x86/helloworld
./helloword/libs/x86_64/helloworld
./helloword/libs/armeabi-v7a/helloworld
./helloword/obj/local/arm64-v8a/objs/helloworld
./helloword/obj/local/arm64-v8a/helloworld
./helloword/obj/local/x86/objs/helloworld
./helloword/obj/local/x86/helloworld
./helloword/obj/local/x86_64/objs/helloworld
./helloword/obj/local/x86_64/helloworld
./helloword/obj/local/armeabi-v7a/objs/helloworld
./helloword/obj/local/armeabi-v7a/helloworld
?
3.4 測試執行
adb push ?helloworld /data
data/helloworld
hello world!
3.5 編譯說明
也可選擇其他編譯選項:
在jni目錄或者helloword目錄下執行:
ndk-build APP_ABI=all ? ? ? ? ?//編譯所有平臺
ndk-build APP_ABI=armeabi-v7a ?//編譯armv7
ndk-build APP_ABI=mips ? ? ? ? //編譯mips
ndk-build APP_ABI=arm64-v8a //編譯armv8A
其他ABI編譯參考:build/cmake/abis.cmake
~/test/android-ndk-r26d/build/ndk-build // 默認多架構編譯
生成的可執行文件在helloworld/libs或者helloword/obj下,不同平臺對應不同的目錄。