我们新建一个支持c++的安卓项目的时候,会自动生成一个native-lib的cpp文件,里面自动给我们实现了一个返回字符串的方法
如果我们自己手写多个native方法再用cpp实现应该怎么做,请看下文:
流程和以前用ndk-build工具的时候一样,只不过最终生成so的时候我们用的是cmak
新建一个jutils.java文件,定义两个native方法,如图
鼠标右键点击该java文件,选择External Tools,Generate Header File 头文件代码如下
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_cloud_baishan_cmaketest_JniUtils */
#ifndef _Included_com_cloud_baishan_cmaketest_JniUtils
#define _Included_com_cloud_baishan_cmaketest_JniUtils
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_cloud_baishan_cmaketest_JniUtils
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_cloud_baishan_cmaketest_JniUtils_add
(JNIEnv *, jclass, jint, jint);
/*
* Class: com_cloud_baishan_cmaketest_JniUtils
* Method: stringFromJNI
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_cloud_baishan_cmaketest_JniUtils_stringFromJNI
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
把头文件复制到cpp目录下,同时新建jniutil.cpp文件,把头文件里面的两个方法实现,代码如下
//
// Created by 49346 on 2020/4/30.
//
#include <jni.h>
#include <string>
#include "com_cloud_baishan_cmaketest_JniUtils.h"
/*
JNIEnv * : 表示一个指向 JNI 环境的指针,可以通过它来访问 JNI 提供的接口方法
jobject : 表示 Java 对象中的 this
JNIEXPORT 和 JNICALL : 都是 JNI 中所定义的宏,可以在 jni.h 这个头文件中查找到
( 上面的代码中虽然没有包含这个头文件,但是如果你细心的话会发现在之前生成的头文件中已经包含这个头文件了,
所以这里就只包含自己生成的头文件。当然还需要哪些头文件需要根据具体情况而定)
*/
JNIEXPORT jint JNICALL Java_com_cloud_baishan_cmaketest_JniUtils_add
(JNIEnv *env, jclass, jint a,jint b) {
return a + b;
}
JNIEXPORT jstring JNICALL Java_com_cloud_baishan_cmaketest_JniUtils_stringFromJNI
(JNIEnv *env, jclass) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
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.
add_library( # Sets the name of the library.
jniutil-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/jniutil.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.
jniutil-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
改完之后,点击AS的make project,最终就能在app/build/intermediates/cmak/debug下找到对应的so文件。
最后来测试一下
运行然后打印日志后查看,结果没问题,编译通过