Android NDK development basics and androidndk Development
Native Development Kit download NDK Development Kit
Link: http://developer.android.com/ndk/downloads/index.html
Select NDK r10e
Android-ndk-r10e-windows-x86_64.exe
The size of the Self-extracted file is 400 mb, and 3 GB + after decompression.
Configure NDK Environment Variables
Add the ndk directory to the path of the system environment variable, and then enter ndk-build verification in the newly opened cmd:
HelloJni
Android Native Development Tools are installed during the installation of ADT in Eclipse,
Import the example located in android-ndk-r10e \ samples \ hello-jni to Eclipse:
Initial directory structure:
Open cmd and switch to the HelloJni project directory:
Run ndk-build to compile:
Go back to Eclipse and refresh the HelloJni directory. Find that the obj folder is added, which contains the. so library files corresponding to each architecture, such:
Then run the project:
Note that many architectures are compiled when ndk-build is executed, which takes a long time. If you only want to compile the arm we need, you can compile the Application under the jni directory of the project. the following content of the mk file is commented out:
#APP_ABI := all
Clear the previous compilation:
ndk-build clean
The next ndk-build will be completed soon:
The first NDK Project
1. Create an Android Project
Create an native method in MainActivity:
public static native String getStringFromC();
2. Create a jni directory
3. Compile the native method on the java Layer
4. Generate the JNI header file
Run cmd to switch to the project directory, and then execute:
Javah-classpath bin/classes-d jni com. zms. hellondk. MainActivity
Javah-classpath bin/classes; D: \ Android \ sdk \ platforms \ android-22 \ android. jar-d jni com. zms. hellondk. MainActivity
This is troublesome. You can also add android. jar to the environment variable: D: \ Android \ sdk \ platforms \ android-22 \ android. jar.
The. h header file is generated under the jni directory:
The format is as follows:
Then, create our own c file in the jni directory:
Hello. c
#include<stdio.h>#include<stdlib.h>#include"com_zms_hellondk_MainActivity.h"JNIEXPORT jstring JNICALL Java_com_zms_hellondk_MainActivity_getStringFromC( JNIEnv env, jclass jclass) { return (*env)->NewStringUTF(env, "Hello from JNI !");}
Then, create the Android. mk file in the jni directory:
Android. mk
LOCAL_PATH: = $ (call my-dir) include $ (CLEAR_VARS) LOCAL_MODULE: = HelloNdkLOCAL_SRC_FILES: = hello. c # which c file to compile include $ (BUILD_SHARED_LIBRARY)
Then go to cmd and switch to the project directory for compilation:
Note: Add loadLibrary to MainActivity. The Module name is enclosed in quotation marks. The Code declared in the static area will be executed before the onCreate method:
package com.zms.hellondk;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;public class MainActivity extends Activity { public static native String getStringFromC(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textFromJni = (TextView) findViewById(R.id.textFromJni); textFromJni.setText(getStringFromC()); } static { System.loadLibrary("HelloNdk"); }}
Otherwise, the following error occurs:
(--, Don't ask how I know)
Concepts related to NDK