上一篇说了ndk-build+动态注册的方式编译JNI,这篇文章来讲一下CMakeList+静态注册的方式来编译JNI。Android官方目前也是推荐使用CMakeList的方式来配置编译JNI
- 既然是CMakeList,那先来看一下:
#指定需要CMake的最小版本
cmake_minimum_required(VERSION 3.4.1)
#指定编译参数,可选 C的编译选项是CMAKE_C_FLAGS C++的编译选项是CMAKE_CPP_FLAGS
#SET(CMAKE_CXX_FLAGS "-Wno-error=format-security -Wno-error=pointer-sign")
#设置生成so动态库最终的输出路径
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})
#设置头文件搜索路径(和此txt同个路径的头文件无需设置),可选
#INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/common)
#指定用到的系统库或者NDK库或者第三方库的搜索路径,可选。
#LINK_DIRECTORIES(/usr/local/lib)
add_library( # Sets the name of the library.
#设置编译后库的名称
userInfo
# Sets the library as a shared library.
#设置库的类型
SHARED
# Provides a relative path to your source file(s).
# 设置源文件
UserInfo.cpp
)
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)
target_link_libraries( # Specifies the target library.
userInfo
# Links the target library to the log library
# included in the NDK.
${log-lib})
上面有注释,写的都很清楚
-
在jni文件夹下新建UserInfo.java类:
image.png
public class UserInfo {
static {
System.loadLibrary("userInfo");
}
public static native String geUserInfo();
}
-
使用AS创建一个C++的工程,在cpp文件夹下创建一个cpp文件,名叫UserInfo.cpp
image.png
在UserInfo.cpp中写UserInfo就会出现如下提示:
image.png
选择第一个就会自动完成getUserInfo的方法静态注册 - 在build.gradle里边添加配置:
android{
...
deafultConfig{
...
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
//abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}
...
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
...
}
- 在activity中调用:
TextView tv = findViewById(R.id.sample_text);
tv.setText(UserInfo.geUserInfo());
最终生成的so动态库会输出到src/main/jniLibs下的各个平台文件夹下
image.png
以上是一次编译生成单个动态库,下面介绍下一次编译生成多个动态库的方法
新建如下的文件:
image.png
one.cpp的内容:
extern "C"
JNIEXPORT jstring
JNICALL
Java_com_example_cmakeapplication_jni_One_getOne(JNIEnv *env, jclass clazz) {
std::string a = "这是one中的信息";
const char *m = a.c_str();
return env->NewStringUTF(m);
}
one文件夹下的CMakeList内容:
add_library( # Sets the name of the library.
#设置编译后库的名称
one
# Sets the library as a shared library.
#设置库的类型
SHARED
# Provides a relative path to your source file(s).
# 设置源文件
one.cpp
)
target_link_libraries( # Specifies the target library.
one
# Links the target library to the log library
# included in the NDK.
${log-lib})
two.cpp的内容:
extern "C" JNIEXPORT
jstring JNICALL
Java_com_example_cmakeapplication_jni_Two_getTwo(JNIEnv *env, jclass clazz) {
std::string a = "这是two中的信息";
const char *m = a.c_str();
return env->NewStringUTF(m);
}
two文件夹下的CMakeList内容:
add_library( # Sets the name of the library.
#设置编译后库的名称
two
# Sets the library as a shared library.
#设置库的类型
SHARED
# Provides a relative path to your source file(s).
# 设置源文件
two.cpp
)
target_link_libraries( # Specifies the target library.
two
# Links the target library to the log library
# included in the NDK.
${log-lib})
另外还有两个One和Two的java类,内容基本相同:
public class One {
static {
System.loadLibrary("one");
}
public static native String getOne();
}
public class Two {
static {
System.loadLibrary("two");
}
public static native String getTwo();
}
-
然后Build/ReBuild Project后就会在jniLibs下输出相应的动态库
image.png - 最后就可以在activity中调用测试结果:
注意:直接运行会报下面这种错误
More than one file was found with OS independent path 'lib/arm64-v8a/libone.so'
因为build.gradle引用CMakeList会让项目链接默认输出路径的动态库,而在jniLibs下又有一份相同的动态库,所以会报动态库重复的错误。这种情况只需要将build.gradle中的CMakeList的引用注销再运行即可
CMakeList默认的动态库输出路径.png-
运行结果:
image.png
CMakeList的编译Jni的介绍到此结束!
参考文章:https://blog.csdn.net/b2259909/article/details/58591898
网友评论