安卓接JNI的具体流程
本次使用开发环境Android Studio 3.4
折腾数天后终于弄明白,遇到各种问题,记录最方便的方法。。。
首先了解为什么要用到JNI
//一般用到高速运算的程序都会使用JNI处理图片等等…
建立安卓项目的配置文件
---------------------------------------
apply plugin: ‘com.android.application’
android {
compileSdkVersion 28
defaultConfig {
applicationId “com.example.test1”
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName “1.0”
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//必须在app下的这个文件下加上加粗的代码
sourceSets.main{
jniLibs.srcDir’src/main/jniLibs’
}
ndk{
moduleName “hello”
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’
}
}
sourceSets{
main{
jni.srcDirs=[]
}
}
}
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’
}
--------------------------------------------------------------
先编译写好的os文件
Android.mk文件如下
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello
LOCAL_SRC_FILES := hello.c
include $(BUILD_SHARED_LIBRARY)
Hello.c文件如下
#include <jni.h>
#include"com_example_test1_JNI.h"
JNIEXPORT jstring JNICALL Java_com_example_test1_JNI_str
(JNIEnv *env, jobject jobj){
return (*env)->NewStringUTF(env,“Hello C!!”);
}
在main文件下添加jniLibs文件夹,而上指定的文件名字一定和添加jniLibs的文件的文件名对应
Endl