前提条件
笔者假想着:能看到这篇博客的读者 NDK环境 已经配置好了哈
开发步骤
第一步:新建工程
工程建完之后在真机或模拟器上运行一遍,确保工程建的没问题
第二步:配置NDK
第三步:在Java中添加 native 函数 并 调用
我们这里就借助 MainActivity 来写一个 native 函数并调用
package com.wust.jnitest3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
tv.setText(jniTest()); //我们在这里调用一下
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native String jniTest(); //这里是我们自己定义的 原生函数
}
第四步:在cpp 中创建c/c++ 源文件
第五步:C++代码的编写
//
// Created by yiqi on 2021/4/1.
//
// jni 开发必须的 头文件
#include "../../../../../../sfDownload/sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/jni.h"
JNICALL extern "C" JNIEXPORT // 不加好像会报错
jstring // native 方法的返回值
// c/c++ native 方法命名规则。 Java_包名_native方法所在的类名_native的方法名
// 后面那两个参数是必须要有的。 *env。 是java 和 c/c++ 互调常用的方法 thiz。 是nation 方法所在类的对象
Java_com_wust_jnitest3_MainActivity_jniTest(JNIEnv* env,jobject thiz){
char * str = "我来自C++";
jstring text = (*env).NewStringUTF(str);
return text;
}
第六步:添加编译配置
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
# 设置生成.so 的文件名,也是你在 java 代码里调用的名字,填一个就好了,记住!!!
native-lib
# Sets the library as a shared library.
#设置库的类型 一种静态文件 STATIC .a 一种动态文件 SHARED .so
SHARED
# Provides a relative path to your source file(s).
# 需要编译的c/c++ 文件,这里填相对路径
native-lib.cpp
myFirstJni.cpp)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
# 指定链接的目标库
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )