Add C and C++ code to the project, refer to the official website documentation.
https://developer.android.google.cn/studio/projects/add-native-code
app/build.gradle adding abiFilters
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags " -v "
abiFilters "armeabi-v7a"
abiFilters "arm64-v8a"
}
}
}
The SO library is placed under jniLibs and will be automatically packaged into APK. The header file is placed under src/main/ffmpeg/include
app/src/main/jinLibs/
arm64-v8a/libffmpeg.so
armeabi-v7a/libffmpeg.so
Add the ffmpeg library in app/CMakeLists.txt, specify the path to the library and the header file path
# 由于库已经预先构建,需要使用 IMPORTED 标志告知 CMake 只希望将库导入到项目中
add_library(ffmpeg
SHARED
IMPORTED)
# 指定库的路径
set_target_properties(ffmpeg
PROPERTIES IMPORTED_LOCATION
src/main/jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libffmpeg.so)
# 指定头文件路径
include_directories(src/main/ffmpeg/include )
#查找系统的动态库:
find_library(log-lib log)
find_library(opensles-lib OpenSLES)
#链接
target_link_libraries(
native-lib
ffmpeg ${opensles-lib} ${log-lib} )
Notice
When calling code from an external C language library, use .c as the file suffix for your own business code. If it is called in *.cpp, the function will be found according to the C++ naming method, and eventually an undefined reference error will be reported during linking.
It is not a convenient practice to declare C functions with extern "C" {} in C++ code.