cmake项目配置
# 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)#设置头文件路径,头文件路径相对Cmake文件来确定
include_directories(src/main/cpp/include)set(FF ${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI})add_library(avcodec SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${FF}/libavcodec.so)add_library(avformat SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${FF}/libavformat.so)add_library(avutil SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${FF}/libavutil.so)add_library(swscale SHARED IMPORTED)
set_target_properties(swscale PROPERTIES IMPORTED_LOCATION ${FF}/libswscale.so)add_library(swresample SHARED IMPORTED)
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${FF}/libswresample.so)add_library(avfilter SHARED IMPORTED)
set_target_properties(avfilter PROPERTIES IMPORTED_LOCATION ${FF}/libavfilter.so)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).提供到源文件的相对路径src/main/cpp/native-lib.cpp)find_library( # Sets the name of the path variable.log-liblog )target_link_libraries( # Specifies the target library.native-libandroid${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libavcodec.so${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libavfilter.so${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libavformat.so${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libavutil.so${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libswresample.so${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libswscale.so# Links the target library to the log library# included in the NDK.${log-lib} )
build.gradle
添加jni用到库所在的路径
plugins {id 'com.android.application'
}android {compileSdkVersion 30buildToolsVersion "30.0.2"defaultConfig {applicationId "com.example.ffmpegplayer"minSdkVersion 19targetSdkVersion 30versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"externalNativeBuild {cmake {cppFlags "-std=c++11"}ndk{abiFilters"armeabi-v7a"}}sourceSets{main{jniLibs.srcDirs=['libs']}}}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}externalNativeBuild {cmake {path "src/main/cpp/CMakeLists.txt"version "3.10.2"}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}
}dependencies {implementation 'androidx.appcompat:appcompat:1.2.0'implementation 'com.google.android.material:material:1.2.1'implementation 'androidx.constraintlayout:constraintlayout:2.0.4'testImplementation 'junit:junit:4.+'androidTestImplementation 'androidx.test.ext:junit:1.1.2'androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
读写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
权限(版本)