Android NDK development environment build

1. Download the recommended version of android ndk: android-ndk64-r10b-windows-x86_64

2. Configure NDK, click Window->Preferences->Android->NDK, set the NDK path.

3. Configure the command tool to generate the jni call header file for eclipse. The specific settings are as follows:

Name:javah

Location:%JAVA_HOME%/bin/avah.exe

Work Directory:${project_loc}

Arguments:-v -classpath "${project_loc}/bin/classes" -d "${project_loc}/jni" ${java_type_name}

4. Create a normal android project NDKTest, right-click Android Tools->Add Native Support on the project, and then give the .so file a name (the default is already set, which is the package name NDKTest). At this time, the project will have an additional jni folder, and there are Android.mk and NDKTest.cpp files under jni. Android.mk is the Makefile of the NDK project, and NDKTest.cpp is the source file of the NDK.

5. Add a class called JNI in android named JNI. Add the native calling method in it, the code is as follows:

 

package com.example.ndktest;

public class JNI {
	public native String NDKTestFromJNI();// native declaration, indicating that this method comes from the Native layer. The implementation process has been implemented in the native layer

	static {
		System.loadLibrary("NDKTest");// Load the library, the previous lib and suffix name do not need to be written
	}
}

 6. Click the JNI class, and then click javah of the menu tool. The header file of the jni call is generated in the jni directory of the project. The code of com_example_ndktest_JNI.h is as follows:

 

 

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_ndktest_JNI */

#ifndef _Included_com_example_ndktest_JNI
#define _Included_com_example_ndktest_JNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_example_ndktest_JNI
 * Method:    NDKTestFromJNI
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_ndktest_JNI_NDKTestFromJNI
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

 7. Reference this header file in NDKTest.cpp, and implement the jni calling method in it. The code is as follows:

 

#include <jni.h>
#include <string.h>
#include "com_example_ndktest_JNI.h"

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

 8. Call the method in JNI in android:

 

TextView tv = new TextView(this);
JNI jni=new JNI();
tv.setText(jni.NDKTestFromJNI());
setContentView(tv);

 9. Increase the so generation of other chips. By default, so only generates so in the armeabi directory. To generate so for other chip architectures, you need to add the Application.mk file in the jni directory of the project. The contents are as follows:

APP_ABI := armeabi armeabi-v7a arm64-v8a x86

 

 You can generate so packages in the four directories of armeabi, armeabi-v7a, arm64-v8a, x86, other chip architectures, and so on.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326573078&siteId=291194637