0%

在 Android Studio 中创建 JNI 项目

第一步:进入到项目中的 java 目录

第二步:生成. h 文件

1
javah com.example.jni.MainActivity

.h 文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//JNI 头文件
#include <jni.h>

// 解决循环拷贝问题
#ifndef _Included_com_example_jni_MainActivity // 如果没有定义这个宏
#define _Included_com_example_jni_MainActivity// 就定义这个宏

#ifdef __cplusplus // 如果是 C++ 环境, 就使用 C 的环境
extern "C" {// 全部采用 C 的方式, 采用 C 是为了不准你函数重载 (解决函数名冲突的问题)
#endif
#undef com_example_jni_MainActivity_A

// 自动检测 MainActivity 生成宏
#define com_example_jni_MainActivity_A 234L

// 函数的声明
extern "C" JNIEXPORT jstring JNICALL Java_com_example_jni_MainActivity_getString
(JNIEnv *, jobject);

#ifdef __cplusplus// 如果是 C++ 什么事情都不干
}
#endif
#endif

函数实现文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//extern "C" JNIEXPORT jstring JNICALL Java_com_example_jni_MainActivity_getString
// (JNIEnv *, jobject){}
//================================= 请看如下注释 =================================


//JNIEnv 是 C++ 的最终会调用到 C 的 JNINativeInterface 方法 , 所以必须采用 C 的方式 (extern "C")
extern "C" // 采用 C 的编译方式

JNIEXPORT // 标记该方法会被外部调用 (VS 会报错, AS 不会报错)

jstring // java 中方法的返回值,
// 这里返回 jstring, 表示 java 中是 String 类型
// 如果是 jint 则表示 java 中是 int 类型

JNICALL// 表示是 JIN 的标记 (这个可以去掉)

// 函数名, 由 JDK 设计的 (JNI 是 java 的技术, 不是 native 的技术)
Java_com_example_jni_MainActivity_getString
(JNIEnv *env, jobject job) {
/**
* 参数一:(JNIEnv): 是 Java 与 C/C++ 通信最重要的东西 (精华)
* 参数二 : 情况一 (jobject) 非静态: 谁调用它, 就是谁的实例, 这里 MainActivity 调用, job 就是 MainActivity(this)
* : 情况二:(jclass) 静态: 谁调用它, 就是谁的 class, 这里 MainActivity 调用, jclass 就是 MainActivity.class
*/
}

第三步:

关键的几个文件:CMakeList.txt,build.gradle(对 CMake 和 NDK 进行配置)

CMake 和 NDK 的区别:

CMake 和 NDK 都是用于在 Android 项目中编译原生代码的工具,但它们的职责不同。

CMake 是一个跨平台的构建工具,可以使用 CMake 编写 C/C++ 代码的构建脚本,并生成相应的构建文件,如 Makefile、Visual Studio 项目等。CMake 的优势是可以跨平台使用,而且能够自动生成适合不同平台的构建文件,可以大大简化项目的构建过程。

NDK(Native Development Kit)则是一个 Android 平台的工具集,它包含了一系列工具和库文件,用于在 Android 平台上开发、编译和调试 C/C++ 代码。NDK 提供了一些基本的系统库和头文件,使得 C/C++ 代码可以访问 Android 系统的底层功能,例如 OpenGL ES、OpenSL ES、MediaCodec 等。同时 NDK 也提供了交叉编译工具链,可以将 C/C++ 代码编译成 ARM、x86 等不同平台的二进制文件。

在使用 NDK 时,CMake 可以作为一种构建工具来使用,它可以读取 CMake 编写的构建脚本,并根据需要生成相应的构建文件。使用 CMake 可以使得 NDK 开发更加便捷和灵活。

总结:NDK 利用 CMake 的配置文件来进行构建。

CMakeList.txt 文件示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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.18.1)

# Declares and names the project.

project("jniapplication")

# 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.
jniapplication

# 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.

target_link_libraries( # Specifies the target library.
jniapplication

# Links the target library to the log library
# included in the NDK.
${log-lib})

build.gradle 文件示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
plugins {
id 'com.android.application'
}

android {
namespace 'com.cmder.jniapplication'
compileSdk 33

defaultConfig {
applicationId "com.cmder.jniapplication"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
// 这里
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.18.1'
}
}
buildFeatures {
viewBinding true
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

#CPU 架构模式:

1.arm64-v8a 现在基本 Android 以这个为主

2.armeabi-v7a 2015 早期的手机

3.armeabi 2011 基本没人用

4.x86 5.x86_64 模拟器

如何知道自己手机 CUP 架构(终端输入):

adb shell getprop ro.product.cpu.abi