android ndk

By Long Luo

1
ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk APP_BUILD_SCRIPT=Android.mk clean
1
2
3
4
5
Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-19.
[armeabi-v7a] Clean : hello-exe [armeabi-v7a]
[arm64-v8a] Clean : hello-exe [arm64-v8a]
[x86] Clean : hello-exe [x86]
[x86_64] Clean : hello-exe [x86_64]

hello.c

1
2
3
4
5
6
#include <stdio.h>

int main(int argc, char ** argv){
printf("Hello world from NDK executable!\n");
return 0;
}

Android.mk

1
2
3
4
5
6
7
8
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := hello-exe
LOCAL_SRC_FILES := hello.c

include $(BUILD_EXECUTABLE)

Application.mk

1
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk APP_BUILD_SCRIPT=Android.mk
``


```txt
Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-19.
[armeabi-v7a] Compile thumb : hello-exe <= hello.c
[armeabi-v7a] Executable : hello-exe
[armeabi-v7a] Install : hello-exe => libs/armeabi-v7a/hello-exe
[arm64-v8a] Compile : hello-exe <= hello.c
[arm64-v8a] Executable : hello-exe
[arm64-v8a] Install : hello-exe => libs/arm64-v8a/hello-exe
[x86] Compile : hello-exe <= hello.c
[x86] Executable : hello-exe
[x86] Install : hello-exe => libs/x86/hello-exe
[x86_64] Compile : hello-exe <= hello.c
[x86_64] Executable : hello-exe
[x86_64] Install : hello-exe => libs/x86_64/hello-exe

Android.mk

1
2
3
4
5
6
7
8
9
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := hello-jni

LOCAL_SRC_FILES := hello-jni.c

include $(BUILD_SHARED_LIBRARY)

Application.mk

1
2
APP_PLATFORM := android-21
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h>
#include <jni.h>

JNIEXPORT jstring JNICALL
Java_me_longluo_hellojni_MainActivity_getStringFromJNI(JNIEnv *env, jobject thiz) {
return (*env)->NewStringUTF(env, "Hello from native code!");
}


JNIEXPORT jint JNICALL
Java_me_longluo_hellojni_MainActivity_getIntSqure(JNIEnv* env, jobject obj, jint value) {
return value * value;
}
1
ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk APP_BUILD_SCRIPT=Android.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-19.
[armeabi-v7a] Compile thumb : hello-jni <= hello-jni.c
[armeabi-v7a] SharedLibrary : libhello-jni.so
[armeabi-v7a] Install : libhello-jni.so => libs/armeabi-v7a/libhello-jni.so
[arm64-v8a] Compile : hello-jni <= hello-jni.c
[arm64-v8a] SharedLibrary : libhello-jni.so
[arm64-v8a] Install : libhello-jni.so => libs/arm64-v8a/libhello-jni.so
[x86] Compile : hello-jni <= hello-jni.c
[x86] SharedLibrary : libhello-jni.so
[x86] Install : libhello-jni.so => libs/x86/libhello-jni.so
[x86_64] Compile : hello-jni <= hello-jni.c
[x86_64] SharedLibrary : libhello-jni.so
[x86_64] Install : libhello-jni.so => libs/x86_64/libhello-jni.so

参考文献

  1. NDK 使用入门
  2. ndk-build 脚本
  3. Android.mk
  4. Application.mk
  5. 使用预构建库

https://corochann.com/build-executable-file-with-android-ndk-after-lollipop-android-api-21-252/

https://stackoverflow.com/questions/24818902/running-a-native-library-on-android-l-error-only-position-independent-executab

https://saurabhsharma123k.blogspot.com/2017/02/generate-so-file-by-using-ndk-and-use.html

Compiling for Android with Android NDK and ndk-build https://talkchess.com/viewtopic.php?t=76133