本文AndroidStudio版本 3.2.1
JNI開發(fā)流程
1. 編寫java部分
Java中聲明native方法
/home/nan/Work/jniProject/com/nan/JniTest.java
package com.nan;
import java.lang.System;
public class JniTest {
// 1. 加載動態(tài)庫
static {
System.loadLibrary("jni-test");
}
public static void main(String[] args) {
JniTest jniTest = new JniTest();
System.out.println(jniTest.get());
jniTest.set("hello world");
}
public native String get();
public native void set(String str);
}
2. 編寫jni部分
1. 編譯java源文件得到class文件,通過javah命令到處jni的頭文件
命令如下
nan@breeze:~/Work/jniProject$ javac com/nan/JniTest.java
nan@breeze:~/Work/jniProject$ javah com.nan.JniTest
當前目錄下會生成一個com_nan_JniTest.h頭文件,用于編寫對應jni cpp用
/home/nan/Work/jniProject/com_nan_JniTest.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_nan_JniTest */
#ifndef _Included_com_nan_JniTest
#define _Included_com_nan_JniTest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_nan_JniTest
* Method: get
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_nan_JniTest_get
(JNIEnv *, jobject);
/*
* Class: com_nan_JniTest
* Method: set
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_nan_JniTest_set
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
2. 實現(xiàn)jni方法
將com_nan_JniTest.h復制到jni目錄下,接著創(chuàng)建test.cpp文件
/home/nan/Work/jniProject/jni/test.cpp
#include "com_nan_JniTest.h"
#include <stdio.h>
JNIEXPORT jstring JNICALL Java_com_nan_JniTest_get
(JNIEnv *env, jobject thiz) {
printf("invoke get() in c++\n");
return env->NewStringUTF("Hello from JNI!");
}
JNIEXPORT void JNICALL Java_com_nan_JniTest_set
(JNIEnv *env, jobject thiz, jstring string) {
printf("invoke set() in c++\n");
char* str = (char*)env->GetStringUTFChars(string,NULL);
printf("%s\n",str);
env->ReleaseStringUTFChars(string, str);
}
3. 編譯so庫
編譯命令如下
nan@breeze:~/Work/jniProject/jni$ gcc -shared -I /usr/lib/jvm/java-7-openjdk-amd64/include -fPIC test.cpp -o libjni-test.so
jni目錄下會生成libjni-test.so庫文件.
注:
Ubuntu16.04,gcc為4.8時報如下錯誤.
gcc: error trying to exec 'cc1plus': execvp: No such file or directory
gcc切換為5.4.0時執(zhí)行成功.
4. 執(zhí)行java程序并調用native方法
nan@breeze:~/Work/jniProject$ java -Djava.library.path=jni com.nan.JniTest
invoke get() in c++
Hello from JNI!
invoke set() in c++
hello world
NDK開發(fā)流程
1. 手動ndk-build
1. 新建Android Project,并使用AndroidStudio下載ndk-bundle
新建Project->F4->SDK Location->Android NDK location->點擊Download->完成后如圖
2. 聲明所需的native方法
通過MainActivity中的按鈕點擊后從native層獲取字符串設置給TextView,并給native層設置字符串
public class MainActivity extends Activity {
public static final String TAG = "breeze";
static {
System.loadLibrary("jni-test");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tvContent = findViewById(R.id.tv_content);
Button btnCaller = findViewById(R.id.btn_call);
btnCaller.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvContent.setText(get());
set("I am from JniProject app!");
}
});
}
public native String get();
public native void set(String str);
}
3. 編寫native方法
在main目錄下創(chuàng)建jni目錄,創(chuàng)建test.cpp
/home/nan/AndroidStudioProjects/JniProject/app/src/main/jni/test.cpp
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include <stdio.h>
/* Header for class com_nan_jniproject_MainActivity */
#ifndef _Included_com_nan_jniproject_MainActivity
#define _Included_com_nan_jniproject_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_nan_jniproject_MainActivity
* Method: get
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_nan_jniproject_MainActivity_get
(JNIEnv * env, jobject thiz) {
printf("invoke get in c++\n");
return env->NewStringUTF("I am from Jni in libjni-test.so !");
}
/*
* Class: com_nan_jniproject_MainActivity
* Method: set
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_nan_jniproject_MainActivity_set
(JNIEnv * env, jobject thiz, jstring string){
printf("invoke set in c++\n");
char* str = (char*)env->GetStringUTFChars(string,NULL);
printf("%s\n",str);
env->ReleaseStringUTFChars(string,str);
}
#ifdef __cplusplus
}
#endif
#endif
沒有獨立的頭文件,聲明和實現(xiàn)均在test.cpp中. 但我們編寫時可通過javah com.nan.jniproject.MainActivity
先生成頭文件,然后將其內容拷貝至test.cpp中,然后改下方法體即可.
4. 創(chuàng)建Android.mk編譯規(guī)則為cpp文件
/home/nan/AndroidStudioProjects/JniProject/app/src/main/jni/Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# 模塊名稱
LOCAL_MODULE := jni-test
# 源代碼文件
LOCAL_SRC_FILES := test.cpp
include $(BUILD_SHARED_LIBRARY)
5. 創(chuàng)建Application.mk來指定so庫生成的目標CPU架構類型
armeabi-v7a最常用,故指定該類型
/home/nan/AndroidStudioProjects/JniProject/app/src/main/jni/Application.mk
APP_ABI := armeabi-v7a
6. 編譯so庫并運行Project
- ndk-build命令編譯so庫
nan@breeze:~/AndroidStudioProjects/JniProject/app/src/main$ ndk-build
Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-16.
Android NDK: WARNING: APP_PLATFORM android-16 is higher than android:minSdkVersion 1 in ./AndroidManifest.xml. NDK binaries will *not* be compatible with devices older than android-16. See https://android.googlesource.com/platform/ndk/+/master/docs/user/common_problems.md for more information.
[armeabi-v7a] Compile++ thumb: jni-test <= test.cpp
[armeabi-v7a] StaticLibrary : libstdc++.a
[armeabi-v7a] SharedLibrary : libjni-test.so
[armeabi-v7a] Install : libjni-test.so => libs/armeabi-v7a/libjni-test.so
可以看到libjni-test.so已生成在libs目錄下/home/nan/AndroidStudioProjects/JniProject/app/src/main/libs/armeabi-v7a/libjni-test.so
- AndroidStudio默認識別的so庫文件夾main/jniLibs/,故我們在main下創(chuàng)建jniLibs目錄,將剛才libs下生成的直接拷到該位置
/home/nan/AndroidStudioProjects/JniProject/app/src/main/jniLibs/armeabi-v7a/libjni-test.so
- 除此外還需在Module的build.gradle配置參數(shù)如圖
注意build.gradle中添加了sourceSets部分,關注注釋1和2部分 - 運行代碼
這時代碼可以正常運行,點擊Button后會調用native函數(shù)并在TextView上顯示"I am from Jni in libjni-test.so !"
2. AndroidStudio自動ndk-build
手動寫cpp沒有任何提示功能,且編譯移動文件繁瑣,通過AndroidStudio配置可實現(xiàn)一鍵編譯運行,代碼回到初識創(chuàng)建狀態(tài),一步一步來.
1. 聲明所需native方法(同上)
2. 創(chuàng)建jni Folder
創(chuàng)建后就會在Project視圖下多出cpp文件夾,這個里面就是用來寫c++的
3. 編寫Android.mk
在cpp文件夾下編寫Android.mk,可以將上面的Android.mk直接考入即可.先將源文件注釋掉,因為還沒有c++文件,待編寫后加入即可
4. 編寫c++文件
首先配置自動編譯且可以編寫c++有提示功能
File->Link C++ Project with Gradle->Build System選項選擇ndk-build方式->Project path選擇Android.mk文件->OK 這時Studio就會自動編譯
這時可以cpp下創(chuàng)建c++文件進行編寫,已經有提示功能了哦,也可以直接將上面的test.cpp直接拷入,然后在Android.mk源文件進行指定. 這時已可以運行代碼了
5. 運行程序
運行后產生的so庫文件和第4步配置后的build.gradle的變化一并展示,如圖
可以發(fā)現(xiàn)AndroidStudio默認會生成如圖4個cpu架構的so庫. 對于build.gradle只多出了注釋1的部分.
6. AndroidStudio指定so庫CPU架構
build.gradle中配置
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.nan.jniproject"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// 1. 添加該部分 {
ndk {
abiFilters 'armeabi-v7a' // , ‘arm64-v8a’, ‘x86’, ‘x86_64’
}
// }
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
ndkBuild {
path file('src/main/jni/Android.mk')
}
}
}
注釋1部分就加入了指定cpu類型,指定armeabi-v7a,如果要支持其他加入即可,armeabi-v7a基本上涵蓋了市面上絕大部分手機.
注意ndk{}必須放在defaultConfig下,否則報如下錯.
Could not find method ndk() for arguments
至此開發(fā)流程我們就清楚了!!!