1.安装NDK ,JDK并对JDK进行配置。
1.1.安装

1.2.JDK环境变量配置参考:https://jingyan.baidu.com/article/3c343ff70bc6ea0d377963df.html
检查JDK是否配置正确:
a:java -version
b:javac
c:javah
出现如下提示标识配置成功:

2.新建普通的Android工程
2.1.新建JniTest文件
public class JniTest {
private JniTest(){}
public static JniTest instatnce=null;
public static JniTest getInstatnce(){
if(instatnce==null){
instatnce=new JniTest();
}
return instatnce;
}
static {
System.loadLibrary("JniTest");//jni模块加载名称
}
public static native String getJniString();//红色的本地方法,暂时不用理会
}

2.2.build---->make project 生成JniTest.class文件


2.3.打开Termial终端,cd到debug目录,执行javah -jni com.example.jianshui.firstndk.JniTest(注意:一定要是全路径[包名+文件名])
步骤一截图:

完成之后生成.h文件如下(注意此自动生成的.h文件的命名是包名+文件名全路径的方式)

2.4.新建jni文件夹,并将.h文件拷贝到jni文件夹下面

2.5.新建一个.c文件,将.h文件中的内容复制到.c文件中,并实现内在的方法(注意,此处最好不要新建成.cpp文件,这个坑我趴过,哎....)



com_example_jianshui_firstndk_JniTest.h文件源码:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_jianshui_firstndk_JniTest */
#ifndef _Included_com_example_jianshui_firstndk_JniTest
#define _Included_com_example_jianshui_firstndk_JniTest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_jianshui_firstndk_JniTest
* Method: getJniString
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_jianshui_firstndk_JniTest_getJniString
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
aa.c文件源码:
//
// Created by jian.shui on 2018/5/9.
//
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_jianshui_firstndk_JniTest */
#ifndef _Included_com_example_jianshui_firstndk_JniTest
#define _Included_com_example_jianshui_firstndk_JniTest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_jianshui_firstndk_JniTest
* Method: getJniString
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_jianshui_firstndk_JniTest_getJniString
(JNIEnv *env, jclass object){
return (*env)->NewStringUTF(env,"hello umeng");
}
#ifdef __cplusplus
}
#endif
#endif
3.配置app molde下的bulid.gradle

apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.jianshui.firstndk"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk{
moduleName "JniTest"
// ldLibs "log", "z", "m"
// abiFilters "armeabi", "armeabi-v7a", "x86"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jni.srcDirs = ['src/main/jni']
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation ('com.android.support.test:runner:1.0.2',{
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation ('com.android.support.test.espresso:espresso-core:3.0.2',{
exclude group: 'com.android.support', module: 'support-annotations'
})
}
4.配置gradle.properties,同时确保local.properties配置了ndk.dir=F:\AndroidSdk\ndk-bundle


具体应该根据报错的提示进行配置(注意类似android.deprecatedNdk=true这种配置已经被废弃,不宜使用)
android.deprecatedNdkCompileLease=1525836367545
5.build---->rebulid project
出现该提示时,关掉工程,然后删除debug文件夹

出现该提示警告可忽略

构建成功之后,在app--bulid--intermediates--ndk下面会生成多个so文件
之所以会生成多个so,是因为在bulid.gradle中abiFilters没有进行过滤配置,默认生成多个版本的so
ndk{
moduleName "JniTest"
// ldLibs "log", "z", "m"
// abiFilters "armeabi", "armeabi-v7a", "x86"
}

6.在mainActivity中调用
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String testStr=new JniTest().getJniString();
Toast.makeText(getApplicationContext(),testStr,Toast.LENGTH_LONG).show();
}
}
至此so文件已经打包生成完毕
效果图:

7.在新建的AndroidLibrary中使用该so文件
7.1.新建的modle的包名一定要保证和调用so文件的JniTest所在的包名一致,因为该so是通过原生的android-project生成的,当你要把该so通过library引用的时候,必须保证library中JniTest.java的全路径是和android-project原生工程中JniTest.java的路径一样,否则,是无法引用的


7.2.jinlibrary中bulid.gradle的配置:
apply plugin: 'com.android.library'
android {
compileSdkVersion 26
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs =['libs']
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
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'
}
7.3.拷贝so文件至jnilibrary的libs文件夹下面

调用:
Toast.makeText(getApplicationContext(), JniTest.getInstatnce().getJniString(), Toast.LENGTH_SHORT).show();
效果:

demo地址:https://github.com/Asmewill/FirstNDK ;
https://github.com/Asmewill/FirstJni
网友评论