1:创建一个firstjni.java文件
在中端执行
D:\Users\caron_wy\FirstJNI\app\src\main\java\wuyu\jni\api>javac firstjni.java
生成firstjni.class
D:\Users\caron_wy\FirstJNI\app\src\main\java>javah -jni wuyu.jni.api.firstjni
生成wuyu_jni_api_firstjni.h
创建jni文件夹.拷贝生成的.h文件到jni文件夹下,创建.c文件
#include <jni.h>
//
// Created by caron_wy on 2019/2/25.
//
JNIEXPORT jint JNICALL Java_wuyu_jni_api_firstjni_add(JNIEnv *env, jobject thiz,jint a,jint b)
{
return a+b;
}
创建CMakeLists.txt文件生成so文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "browser.android.com.firstjni"
minSdkVersion 26
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
//abiFilters 'armeabi-v7a'
cppFlags ""
arguments "-DANDROID_STL=c++_shared"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt" //编译后so文件的名字
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
测试
package browser.android.com.firstjni;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import wuyu.jni.api.firstjni;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("wuyu","jni =" + firstjni.add(100,33));
}
}