简介
项目需要涉及到JNI调用C++ 代码所以所以学习了一了一下Android Studio 工具下 Cmake方式的 Jni 调用的一些知识特此记录一下
环境版本
- Android Studio 4.1.3
- Gradle 4.1.3
- Cmake 3.10.2
- ndk 21.1.6352462
- buildToolsVersion 30.0.3
1.项目配置
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 31
// 31.0.0 版本有异常不能用
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.wkq.cdemo"
minSdkVersion 21
targetSdkVersion 31
versionCode 1
versionName "1.0"
//设置NDK版本(生成动态so库的时候要用)
ndkVersion "21.1.6352462"
// gradle 执行的任务名字
externalNativeBuild {
cmake {
cppFlags ""
// 过滤生成的库类型
// abiFilters 'armeabi-v7a'
// 库格式'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
//生成指定版本的 CMakeLists.txt 文件
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
}
2.CMakeLists 文件介绍
# 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的最低版本
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("cdemo")
#这些 native-lib.cpp资源打包设置成一个名为hellow 的共享库(SHARED 替换为STATIC则设置为静态库)
# native-lib.cpp
add_library( # Sets the name of the library.
hellow
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.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.
#(设置要链接的库文件的名称 hellow)
target_link_libraries( # Specifies the target library.
hellow
# Links the target library to the log library
# included in the NDK.
${log-lib} )
3.cpp 文件简介
// include 是导包
#include <jni.h>
#include <string>
// extern "C" :C++兼容C代码的
// JNIEXPORT 类似于public修饰符
// JNICALL 用来表示函数的调用规范
// jstring 返回数据类型 这里是String
// Java_<包名>_<类名>_<方法名>
extern "C"
JNIEXPORT jstring JNICALL Java_com_wkq_cdemo_MyJniUtil_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "1122211";
return env->NewStringUTF(hello.c_str());
}
4. JNI 工具类
package com.wkq.cdemo;
/**
* @author wkq
* @date 2022年06月28日 16:46
* @des
*/
public class MyJniUtil {
//加载 so库
static{
System.loadLibrary("hellow");
}
// jni 调用
public static native String stringFromJNI();
}
5.调用
String content=MyJniUtil.stringFromJNI();
6.生成动态So库
生成动态So库有两种方式
- make project
-
Gradle任务中执行 externalNativeBuildRelease/externalNativeBuildRelease或者externalNativeBuildDebug 任务
总结
简单的梳理了C++类型项目中一些配置,然后生成了动态的So库.
那些打不死我的终将使我强大,继续死磕JNI