Authors
:Panda
本次使用的ndk版本是r20(r17之后是不支持armeabi架构的,可以用armeabi-v7a代替)
先回顾下使用NDK到生成使用so库的整个流程:
- 生成CMakeLists.txt文件,并关联
- 在java文件里创建一个native方法
- 对该native方法,在Terminal里面命令生成.h文件
- 创建一个.cpp(.c)文件实现.h文件里的方法
- 配置CMakeLists文件
- 静态代码块加载
- 代码调用jni
- 生成指定的abis架构so库
一般.h和.cpp文件会有c工程师写
1,生成CMakeLists.txt文件,并关联
在app目录下新建该文件,然后写入相应的源码
# 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.
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})
add_library( # Sets the name of the library.
a
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/jni/abc.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.
a
# Links the target library to the log library
# included in the NDK.
${log-lib})
其中可以选择设置so库文件生成存放的路径:
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})
其次需要修改下面代码块:
最后关联:
2,在java文件里创建一个native方法
public static native String resultStr();
3,生成.h文件
在java目录下输入如下指令:
javah com.zkxl.a.AA
com.zkxl.a是包名,AA是该native方法所在的类名。生成.h文件如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_zkxl_a_AA */
#ifndef _Included_com_zkxl_a_AA
#define _Included_com_zkxl_a_AA
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_zkxl_a_AA
* Method: resultStr
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_zkxl_a_AA_resultStr
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
在main目录下新建一个JNI Folder,然后将该com_zkxl_a_AA.h文件(生成后名字可以随便取)放入
4,创建.cpp文件实现.h的方法
#include "aa.h"
JNIEXPORT jstring JNICALL Java_com_zkxl_a_AA_resultStr(JNIEnv *env, jclass js){
return env->NewStringUTF("test");
}
5,配置CMakeLists文件
设置so名字及c文件路径:
6,静态代码块加载
加载的名字就是CMakeLists命名的:
7,代码调用jni
-----------以上就是本地库了-------------
8,生成指定的abis架构so库
在app目录下的build.gradle的defaultConfig节点下,加入下面代码:
externalNativeBuild {
cmake {
cppFlags ''
abiFilters 'armeabi-v7a'
}
}
就能生成对应架构的so库了
-------------------使用该so库--------------------
在app目录下的build.gradle的android节点下,加入下面代码(要注释掉cmake的配置):
sourceSets{
main{
jniLibs.srcDirs = ['libs']
}
}
------------------打包生成多个so库----------------------
编译多个so库,文件目录如下:
在one文件夹下的CMakeLists.txt配置如下:
add_library( one SHARED one.cpp)
target_link_libraries(one ${log-lib})
在two文件夹下的CMakeLists.txt配置如下:
add_library( two SHARED two.cpp)
target_link_libraries(two ${log-lib})
在app目录下的CMakeLists.txt配置如下:(在完成这步前,cpp文件会报红,配置完就ok了)
add_subdirectory(src/main/jni/one)
add_subdirectory(src/main/jni/two)
CmakeLists.txt文件支持继承,所以我们只需要在子配置文件中写不同的配置项就可以完成相应的配置.最后需要在项目的CmakeLists.txt文件中增加子配置文件的路径.
到这就大功告成了