一、JNI和NDK的关系
https://blog.csdn.net/carson_ho/article/details/73250163
二、实现一个简单的示例
1、先创建项目,bulid一下项目,进入到F:\SelfDemo\ndk\app\build\intermediates\javac\debug\compileDebugJavaWithJavac\classes\com\hzy\ndk目录,可以到项目的.class文件(gradle:3.4.1有些版本会在其他的目录),如下图所示:
示例图1
2、创建HelloWorld.java类,声明一个本地方法静态方法,然后make project一下,然后就可以看到HelloWorld.class类。
package com.hzy.exampledemo.ui.ndk;
public class HelloWorld {
public static native String sayHello();
}
示例图2
3、点击底部Terminal,执行:
cd app/src/main/java
javah -d ../jni com.hzy.ndk.HelloWorld
命令刷新目录即可看到在main的目录下有一个jni目录
4、进入jni,创建HelloWorld.cpp
//
// Created by Administrator on 2019/5/28.
//
#include "com_hzy_ndk_HelloWorld.h"
JNIEXPORT jstring JNICALL Java_com_hzy_ndk_HelloWorld_sayHello(JNIEnv * env, jclass jc){
return env->NewStringUTF("say HelloWorld from c++");
}
5、在app下build.gradle下添加如下代码:
ndk {
moduleName "HelloWorld"
}
在gradle.properties内添加
android.useDeprecatedNdk=true
6、在HelloWorld.java内添加加载本地so的方法,最后在MainActivity.java内进行调用。
HelloWorld.java
package com.hzy.ndk;
public class HelloWorld {
static {
System.loadLibrary("HelloWorld");
}
public static native String sayHello();
}
MainActivity.java
package com.hzy.ndk;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
tv.setText(HelloWorld.sayHello());
}
}
ok点击运行;按之前的逻辑是可以运行成功的,但是确莫名其妙的报错了
示例图4https://d.android.com/r/studio-ui/add-native-code.html#ndkCompile
最后根据第一个链接点击进去需要我们创建一个CMakeLists.txt,我们在jni文件夹下创建CMakeLists.txt
可参考:https://github.com/googlesamples/android-ndk/blob/master/hello-jni/app/src/main/cpp/CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
add_library(HelloWorld SHARED
HelloWorld.cpp)
# Include libraries needed for hello-jni lib
target_link_libraries(HelloWorld
android
log)
然后把CMakeLists.txt目录加入build.gradle内:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.hzy.ndk"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
// 配置CMakeLists.txt路径
externalNativeBuild {
cmake {
path "src/main/jni/CMakeLists.txt"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
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'
}
make project一下:
示例图5
最后执行成功:
示例图6deom地址:https://github.com/hnhzy/ndk
参考文章:
https://www.jianshu.com/p/87ce6f565d37
https://developer.android.google.cn/ndk
https://blog.csdn.net/xiaozhu0922/article/details/78835144#commentsedit
网友评论