plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.anniljing.ffmpegnative"
minSdk 25
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags '-std=c++11'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.18.1'
}
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
cmake_minimum_required(VERSION 3.18.1)
project("ffmpegnative")
add_library( # Sets the name of the library.
ffmpegnative
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.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.
ffmpegnative
# Links the target library to the log library
# included in the NDK.
${log-lib})
cmake_minimum_required
cmake最低版本要求
project
设置项目名称
add_library
添加库并设置库的源文件
1、 Normal Libraries
add_library(<name> [STATIC | SHARED | MODULE]
[EXCLUDE_FROM_ALL]
[<source>...])
name:库名称
STATIC|SHARED|MODULE:库类型(静态、动态、模块)
source:源文件
2、Imported Libraries
add_library(<name> <type> IMPORTED [GLOBAL])
导入已经生成的库,通常情况搭配set_target_properties,指定库的相关配置信息
set_target_properties(target1 target2 ...
PROPERTIES prop1 value1
prop2 value2 ...)
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(
ffmpegnative
${log-lib})
将log-lib库链接到ffmpegnative中
#设置头文件路径
include_directories(${CMAKE_SOURCE_DIR}/include)
#声明ffmpeg_lib_dir变量,设置统一的库文件路径
set(ffmpeg_lib_dir ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})
#添加ffmpeg相关的库
#avutil库
add_library( avutil
SHARED
IMPORTED )
set_target_properties( avutil
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libavutil.so )
#swresample库
add_library( swresample
SHARED
IMPORTED )
set_target_properties( swresample
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libswresample.so )
#avcodec库
add_library( avcodec
SHARED
IMPORTED )
set_target_properties( avcodec
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libavcodec.so )
#avfilter库
add_library( avfilter
SHARED
IMPORTED)
set_target_properties( avfilter
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libavfilter.so )
#swscale库
add_library( swscale
SHARED
IMPORTED)
set_target_properties( swscale
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libswscale.so )
#avformat库
add_library( avformat
SHARED
IMPORTED)
set_target_properties( avformat
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libavformat.so )
#avdevice库
add_library( avdevice
SHARED
IMPORTED)
set_target_properties( avdevice
PROPERTIES IMPORTED_LOCATION
${ffmpeg_lib_dir}/libavdevice.so )
target_link_libraries(
ffmpegnative
#链接ffmpeg相关库
avutil
swresample
avcodec
avfilter
swscale
avformat
avdevice
#链接本地日志库
${log-lib})
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
public native String getFFmpegVersion();
#include
extern "C"
JNIEXPORT jstring JNICALL
Java_com_anniljing_ffmpegnative_MainActivity_getFFmpegVersion(JNIEnv *env, jobject thiz) {
const char* version = av_version_info();
return env->NewStringUTF(version);
}