概要
在日常开发中,android NDK的作用无外乎有两种:一种是通过调用底层C/C++的算法,提高app的运行效率;另一种则是通过C/C++的特性,或者和驱动交互等,实现一些功能性的需求。接下来将用详细的步骤,给大家演示一下android ndk调用三方so库的过程。
1.NDK环境准备
1.1.通过android studio在SDK Tools中安装LLDB、NDK、CMake,如下图:

1.2.安装好了NDK之后,将ndk-bundle目录设置到系统环境变量Path中去,如下图:

2.生成一个三方so库
2.1.创建一个jni目录,在该目录下创建四个文件(注意目录的名字一定要是jni):MyLib.h,MyLib.cpp,Android.mk,Application.mk
a.MyLib.h
#ifndef TESTNDK_MYLIB_H
#define TESTNDK_MYLIB_H
class MyLib {
public:
MyLib();
~MyLib();
int add(int a,int b);
};
#endif //TESTNDK_MYLIB_H
b.MyLib.cpp
#include "MyLib.h"
MyLib::MyLib()
{
}
MyLib::~MyLib()
{
}
int MyLib::add(int a,int b)
{
return a+b;
}
c.Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := MyLib.cpp
NDK_APP_DST_DIR := ./jniLibs/$(TARGET_ARCH_ABI)
include $(BUILD_SHARED_LIBRARY)
d.Application.mk
APP_STL:=c++_shared
APP_PLATFORM := android-16
APP_CPPFLAGS:=-frtti -fexceptions
APP_ABI := all
2.2.进入到CMD,执行ndk-build命令:

命令执行成功之后,就会在当前目录下生成jniLibs的目录,里面就是我们需要的so库文件了。

3.Android项目中引入三方so库
3.1.创建android项目,配置NDK环境


3.2.将jniLibs文件夹拷贝到项目中

3.3.配置CMakeLists.txt文件,将三方so配置进去

完整的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.
native-lib
# 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 )
add_library(
mylib
SHARED
IMPORTED)
set_target_properties(
mylib
PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libmylib.so
)
include_directories(src/main/cpp/include)
# 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.
native-lib
# Links the target library to the log library
# included in the NDK.
mylib
${log-lib} )
3.4.在模块级build.gradle中配置c++_shared的库,因为mylib.so依赖于它

arguments "-DANDROID_STL=c++_shared"
4.调用so库中的方法
4.1.将MyLib.h文件拷贝到cpp/include目录下(这个目录需要配置到CMakeLists.txt文件中,上面的操作已经配置过了)

4.2.在默认生成的native-lib.cpp中调用我们so库中的方法
#include <jni.h>
#include <string>
#include "./include/MyLib.h"
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_testndk_MainActivity_stringFromJNI(JNIEnv* env,jobject) {
MyLib myLib;
int result = myLib.add(10,10);
return env->NewStringUTF(std::to_string(result).c_str());
}
5.效果
