本文介绍在android studio上配置jni并引入第三方c++库的方法。
这也是笔者的第一篇博文 笔者自己也只是大四在校生才疏学浅,如有错误可以留言。
由于毕设原因,后期笔者有时间的话还会更新一些音视频相关的文章。
-首先new projection 注意选择类型为native c++类型的projection,等待构建,能自动生成jni。
-在编译时笔者的Android studio有版本的坑,注意build.gradle版本底下的工具版本为32时编译器会corrupt,要把所有32改成30,等待编译器下载低版本的api,这样才能编译运行。
-接下来要引入第三方库,这里可以直接将包含库文件的文件夹放在和cMakeList.txt的同级目录底下(笔者引入的是librtmp库,这里以此为例)。
-在做这些配置之前,这里还是要先配置gradle文件,否则将会出现类似如下错误:
Build command failed.
Error while executing process D:\Android\SDK\android-sdk-windows\cmake\3.6.4111459\bin\cmake.exe with arguments {--build E:\demo\20200427\2\GB28181Android-master\gb28181library\.externalNativeBuild\cmake\debug\arm64-v8a --target gb_native}
[1/4] Building CXX object CMakeFiles/gb_native.dir/src/main/cpp/gb28181_muxer.cpp.o
[2/4] Building CXX object CMakeFiles/gb_native.dir/src/main/cpp/gb_native.cpp.o
[3/4] Building CXX object CMakeFiles/gb_native.dir/src/main/cpp/GB28181_sender.cpp.o
In file included from E:\demo\20200427\2\GB28181Android-master\gb28181library\src\main\cpp\gb_native.cpp:5:In file included from E:\demo\20200427\2\GB28181Android-master\gb28181library\src\main\cpp/gb28181_muxer.h:10:
-这里要在DefaultConfig下面写上:
externalNativeBuild {
cmake {
cppFlags ""
abiFilters "armeabi-v7a"
}
}
ndk{
// 打包生成的 APK 文件指挥包含 ARM 指令集的动态库
abiFilters "armeabi-v7a" /*, "arm64-v8a", "x86", "x86_64"*/
}
注意这段代码如果放错位置会报错:No signature of method:*******。
接下来开始编写CMakeList.txt文件。
编写前先认识几个cmake语法操作,已经存在的CMakeList底下已经有几个操作:
Add_library find_library 和target_link_libraries
这些是将native-lib.cpp添加和链接的操作,代码上面的英文注释解释很清楚,这里不多做解释。这里我们要添加rmtp目录底下的动态库(.so)文件:
set(rtmp_dir ${CMAKE_SOURCE_DIR}/rtmp)
set(rtmp_lib_dir ${rtmp_dir}/lib)
#添加头文件目录
include_directories(${rtmp_dir}/include/librtmp)
先设置好路径,这里其实就是绝对路径,set函数就是把${CMAKE_SOURCE_DIR}/rtmp 这个路径赋值给rtmp_dir,这里${CMAKE_SOURCE_DIR}就是CMakeList.txt的绝对路径。
这里我的头文件都是存储在include/librtmp目录底下,所以添加头文件目录就设置该路径就行了。接下来要添加动态库进去:
add_library(librtmp SHARED IMPORTED)
set_target_properties(librtmp PROPERTIES IMPORTED_LOCATION
${rtmp_lib_dir}/armeabi-v7a/librtmp.so)
这个地方librtmp就是自己设置的一个变量名了,可以根据需求更改和第二句的第一个参数一样,然后后面那个变量就是动态库的路径,中间乱七八糟的我也是照抄的,讲不清楚原理。
所以这两句就是设置librtmp对应的库路径。然后下面这句链接时要加上librtmp:
target_link_libraries( # Specifies the target library.
native-lib
librtmp
# Links the target library to the log library
# included in the NDK.
${log-lib} )
这样整个操作就搞完了,在native-lib文件中包含头文件,试着printf一下库里面的东西,调用几个函数,然后build一下看看能不能创建成功即可。
p上CMakeList.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. # 设置构建本地库所需的最小版本的cbuild。 cmake_minimum_required(VERSION 3.10.2) # Declares and names the project. project("testmedias") # 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. #Currently,we begin to set rtmp.so dll file. #首先先通过CMAKE_SOURCE_DIR获取cmakeList.txt的路径,然后rtmp的库文件我们放在和cmakeList.txt的同级目录底下 set(rtmp_dir ${CMAKE_SOURCE_DIR}/rtmp) set(rtmp_lib_dir ${rtmp_dir}/lib) #添加头文件目录 include_directories(${rtmp_dir}/include/librtmp) #file(GLOB RTMPSources ${CMAKE_SOURCE_DIR}/rtmp/include/librtmp/*.h) add_library(librtmp SHARED IMPORTED) set_target_properties(librtmp PROPERTIES IMPORTED_LOCATION ${rtmp_lib_dir}/armeabi-v7a/librtmp.so) #target_include_directories(native-lib PRIVATE ${CMAKE_SOURCE_DIR}/rtmp/include) # # 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. # 创建并命名一个库,将其设置为静态 # 或者共享,并提供其源代码的相对路径。 # 您可以定义多个库,而cbuild为您构建它们。 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 ) target_link_libraries( # Specifies the target library. native-lib librtmp # Links the target library to the log library # included in the NDK. ${log-lib} )