Android NDK basic configuration

Keywords: Android Windows Java Gradle

Android ndk configuration under Windows

ndk package
This installation uses android ndk version android-ndk-r12b-windows-x86 ʄ
Environment variable configuration
Add the unzipped ndk PATH to the PATH, and the native is "D:/ndk"
Android project configuration
1. Create a new Android project, add local.properties to ndk.dir=D:\ndk
2. Right click app - > New - > Folder - > jnifolder after completion
3. Create a new java class

public class HelloJni {
    static {
        System.loadLibrary("hello");
    }
    public native String sayHello();
}

4.gradle configuration

defaultConfig {
        applicationId "nexecheck.kstech.com.test_android"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        sourceSets.main{
            jniLibs.srcDir 'src/main/libs'
        }
        ndk{
            moduleName 'hello'
        }
    }

Android. Usedeprecatednk = true needs to be set in gradle.properties
5. Generate. h file
We need to use the method of calling NDK in Java. First, we need to generate the. h header file. jni's. h file is very troublesome. We can generate it automatically through javah command.
Run the command in the app / Directory:

javah -d src/main/jni/ -classpath build/intermediates/classes/debug/ com.example.jjz.jni.HelloJni
 Where - d is the directory where the. h file is saved
 -classpath is the directory where the. class is specified. After the project is built successfully, the. class file will be generated in the build / mediates / classes / debug / directory.
Nexecheck.kstech.com.test'android.hellojni is the package name plus the class name.
You can get a com? Example? Jjjz? jni? Hellojni. H file in the jni directory  

6. Implement. h file
Create a new file nexecheck · kstech · com · test · Android · hellojni. C to implement the sayHello method.

#include <jni.h>
#include "nexecheck_kstech_com_test_android_HelloJni.h"
JNIEXPORT jstring JNICALL Java_nexecheck_kstech_com_test_1android_HelloJni_sayHello
  (JNIEnv *env, jobject clazz){
    return (*env)->NewStringUTF(env,"hello world jni");
  }

7. After the creation is successful, enter the project view, and create Android.mk and Application.mk files in the jni folder. The contents are as follows:

Android.mk-----------------------------------------------------------
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello
LOCAL_SRC_FILES := nexecheck_kstech_com_test_android_HelloJni.c
include $(BUILD_SHARED_LIBRARY)
Android.mk-----------------------------------------------------------

Application.mk-----------------------------------------------------------
APP_MODULES:= hello
Application.mk-----------------------------------------------------------  

For detailed configuration, please refer to
Android? NDK document: https://developer.android.google.cn/ndk/guides/application_mk.html

8.ndk-build
In the directory.. / app/src/main/jni, run the command NDK build
Result:

D:\APPs\test_android\app\src\main\jni>ndk-build
[arm64-v8a] Compile        : hello <= nexecheck_kstech_com_test_android_HelloJni.c
[arm64-v8a] SharedLibrary  : libhello.so
[arm64-v8a] Install        : libhello.so => libs/arm64-v8a/libhello.so
[x86_64] Compile        : hello <= nexecheck_kstech_com_test_android_HelloJni.c
[x86_64] SharedLibrary  : libhello.so
[x86_64] Install        : libhello.so => libs/x86_64/libhello.so
[mips64] Compile        : hello <= nexecheck_kstech_com_test_android_HelloJni.c
[mips64] SharedLibrary  : libhello.so
[mips64] Install        : libhello.so => libs/mips64/libhello.so
[armeabi-v7a] Compile thumb  : hello <= nexecheck_kstech_com_test_android_HelloJni.c
[armeabi-v7a] SharedLibrary  : libhello.so
[armeabi-v7a] Install        : libhello.so => libs/armeabi-v7a/libhello.so
[armeabi] Compile thumb  : hello <= nexecheck_kstech_com_test_android_HelloJni.c
[armeabi] SharedLibrary  : libhello.so
[armeabi] Install        : libhello.so => libs/armeabi/libhello.so
[x86] Compile        : hello <= nexecheck_kstech_com_test_android_HelloJni.c
[x86] SharedLibrary  : libhello.so
[x86] Install        : libhello.so => libs/x86/libhello.so
[mips] Compile        : hello <= nexecheck_kstech_com_test_android_HelloJni.c
[mips] SharedLibrary  : libhello.so
[mips] Install        : libhello.so => libs/mips/libhello.so

Posted by eneyas on Thu, 02 Apr 2020 09:06:55 -0700