Android NDK 簡單來說就是可以讓你的應用使用natice-code語言, (比如C ,C++)的工具集
一:what is the ndk
Ndk 是允許你在你的工程中可以使用native-code (比如C or c++)的工具, 你可以利用ndk來構建你的natice-app
正如我們知道的那樣, Java 是構建android app 的預設語言, 然而, java 程式碼很容易被反編譯, 但是這對C++ code 來說這並不簡單, 並且c++ 程式碼更加高效
二:why is ndk
C++ 程式碼反編譯困難
對於計算複雜量大的程式碼, C or C++ code can increase the performance of your app
三:buliding native app
如果你使用android studio 開發, 你需要下載安裝ndk包, More info: NDK
1, 首先在程式碼中宣告要在C++ code中要實現的native方法
/**
* auth:huanjulu
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
((TextView) findViewById(R.id.text)).setText(nativeMethod());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* native code
*
* @return
*/
public native String nativeMethod();
}複製程式碼
2, 對包含native
關鍵字方法的類, 生成.h
標頭檔案
進入到本地Java資料夾下 ,在Terminal input the follow commond
javah -d ../jni jniproject.ndkproject.MainActivity複製程式碼
first , jniproject.ndkproject
is your package name and MainActivity
is the class name that contains native code
and after that , you can see the .h
file of MainActivity
class , it looks like :
and we can looks what contents it contains
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class jniproject_ndkproject_MainActivity */
#ifndef _Included_jniproject_ndkproject_MainActivity
#define _Included_jniproject_ndkproject_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: jniproject_ndkproject_MainActivity
* Method: nativeMethod
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_jniproject_ndkproject_MainActivity_nativeMethod
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif複製程式碼
3, android.mk``application.mk
file
在你的jni directory, 新建android.mk
,
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hellojni
LOCAL_SRC_FILES := hellojni.cpp
include $(BUILD_SHARED_LIBRARY)複製程式碼
application.mk
APP_MODULES := hellojni //生成的.so name
APP_ABI := all //生成的.so 目標cpu平臺複製程式碼
if you want to know more information of syntax about them, here is the dox
android.mk/#intro
4, 編寫你的natie-code 實現檔案, (ig .cpp or .c)
#include "jniproject_ndkproject_MainActivity.h"
JNIEXPORT jstring JNICALL Java_jniproject_ndkproject_MainActivity_nativeMethod
(JNIEnv *env, jclass type) {
return env->NewStringUTF("this code from jni cpp");
}複製程式碼
as wo can see that in above c++ code
first , you need to include you .h
file
#include "jniproject_ndkproject_MainActivity.h"複製程式碼
接下來你需要按照c++ 程式碼的規範編寫實現方法, 請注意方法實現的命名規範 (JNIEXPORT 返回值型別 JNICALL 包名類名方法名)
5, ndk-build
under the jni
dictionary, generate .so
file
luhuanju:jni huanjulu$ ndk-build複製程式碼
接下來你需要把生成的app/src/main/libs
下的平臺的.so 檔案複製到 app/libs
下
6, app/build.gradle
config seeting
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "jniproject.ndkproject"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {//指定生成的lib,比如此時生成native.so
moduleName "hellojni"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
compile 'com.android.support:design:25.2.0'
testCompile 'junit:junit:4.12'
}複製程式碼
the the last step is that you just need call you natice-code library
package jniproject.ndkproject;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
/**
* auth:huanjulu
*/
public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("hellojni");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
((TextView) findViewById(R.id.text)).setText(nativeMethod());
}
/**
* native code
*
* @return
*/
public native String nativeMethod();
}複製程式碼
here is source code