关于NDK和JNI的相关文档,网上有很多,在这里就不多做说明了。
下面进行第一个 NDK demo 的实现:
在这里我使用的是Android Studio 3.0进行下面的环境搭建。
第一步:在项目目录main下新建我们存放c/c++代码的目录文件夹 cpp ;在cpp目录下新建我们的c/c++文件;
文件命名JniUtils.c(文件名可以随意)
#include <jni.h>
JNIEXPORT jstring JNICALL Java_com_union400_ndkdemo_JniUtils_loadCString(JNIEnv *env, jobject obj) {
return (*env)->NewStringUTF(env, "I am form Native C");
}
第二步:在app目录下新建 CMakeLists.txt 文件
CMakeLists.txt 中主要定义了哪些文件需要编译,以及和其他库的关系等。
# 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.
# 编译出一个动态库 JniLibrary,源文件只有 src/main/cpp/JniUtils.c
add_library( # Sets the name of the library.
JniLibrary
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/JniUtils.c
# src/main/cpp/JniUtils.c # 注:多个源文件可以像这样直接加在后面
)
# 头文件目录
include_directories(src/main/cpp/include/)
# 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.
# 找到预编译库 log_lib
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.
# log_lib link到我们的动态库 JniLibrary 中
target_link_libraries( # Specifies the target library.
JniLibrary
# Links the target library to the log library
# included in the NDK.
${log-lib} )
第三步:在 build.gradle 中配置 CMake
在 defaultConfig 中添加配置:
externalNativeBuild {
cmake {
// -DANDROID_PLATFORM 代表编译的 android 平台。文档建议直接设置 minSdkVersion 就行了,所以这个参数可忽略。
// -DANDROID_TOOLCHAIN=clang,CMake 一共有2种编译工具链 - clang 和 gcc,gcc 已经废弃,clang 是默认的,所以这个参数也可忽略。
// arguments '-DANDROID_PLATFORM=android-16', '-DANDROID_TOOLCHAIN=clang'
}
}
在 android 中添加配置:
externalNativeBuild {
cmake {
path "CMakeLists.txt" // 指明了 CMakeList.txt 的路径
}
}
第四步:Java 代码中调用我们的c/c++ 代码:
public class MainActivity extends AppCompatActivity {
private final String TAG = this.getClass().getSimpleName();
// 加载Library
static {
System.loadLibrary("JniLibrary");
}
// 定义 native 方法
public native String loadCString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvHello = findViewById(R.id.tvHello);
tvHello.setText(loadCString());
}
}
至此,第一个NDK demo 完成;里面有很多的知识点需要大家仔细去挖掘学习。
本文详细介绍了如何在Android Studio 3.0环境下搭建NDK环境,通过创建C/C++代码并使用CMake进行编译,最终在Java代码中调用本地方法。通过实例演示了JNI的使用流程。
1183

被折叠的 条评论
为什么被折叠?



