for the first time, using the FFmpeg framework, I compiled the source code on deepin and got the include and so files. Then copy to win10
, and create the project as shown in figure
then I added the following code to CMarkLists.txt
:
-sharp For more information about using CMake with Android Studio, read the
-sharp documentation: https://d.android.com/studio/projects/add-native-code.html
-sharp Sets the minimum version of CMake required to build the native library.
include_directories(libs/include)
set(DIR ../../../../libs)
cmake_minimum_required(VERSION 3.4.1)
-sharp Creates and names a library, sets it as either STATIC
-sharp or SHARED, and provides the relative paths to its source code.
-sharp You can define multiple libraries, and CMake builds them for you.
-sharp Gradle automatically packages shared libraries with your APK.
add_library( -sharp Sets the name of the library.
native-lib
-sharp Sets the library as a shared library.
SHARED
-sharp Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
add_library(avcodec SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${DIR}/armeabi-v7a/libavcodec.so)
add_library(avformat SHARED IMPORTED)
set_target_properties(avformat PROPERTIES IMPORTED_LOCATION ${DIR}/armeabi-v7a/libavformat.so)
add_library(avutil SHARED IMPORTED)
set_target_properties(avutil PROPERTIES IMPORTED_LOCATION ${DIR}/armeabi-v7a/libavutil.so)
add_library(swresample SHARED IMPORTED)
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${DIR}/armeabi-v7a/libswresample.so)
add_library(swscale SHARED IMPORTED)
set_target_properties(swscale PROPERTIES IMPORTED_LOCATION ${DIR}/armeabi-v7a/libswscale.so)
add_library(avfilter SHARED IMPORTED)
set_target_properties(avfilter PROPERTIES IMPORTED_LOCATION ${DIR}/armeabi-v7a/libavfilter.so)
-sharp Searches for a specified prebuilt library and stores the path as a
-sharp variable. Because CMake includes system libraries in the search path by
-sharp default, you only need to specify the name of the public NDK library
-sharp you want to add. CMake verifies that the library exists before
-sharp completing its build.
find_library( -sharp Sets the name of the path variable.
log-lib
-sharp Specifies the name of the NDK library that
-sharp you want CMake to locate.
log )
-sharp Specifies libraries CMake should link to your target library. You
-sharp can link multiple libraries, such as libraries you define in this
-sharp build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( -sharp Specifies the target library.
native-lib
avfilter
avcodec
avformat
avutil
swresample
swscale
-sharp Links the target library to the log library
-sharp included in the NDK.
${log-lib} )
the following code has been added to gradle.build
:
apply plugin: "com.android.application"
android {
compileSdkVersion 28
defaultConfig {
applicationId "cn.lxw.ffmpeg"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
sourceSets{
main{
jniLibs.srcDirs = ["libs"]
}
}
externalNativeBuild {
cmake {
cppFlags ""
abiFilters "armeabi-v7a"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:28.0.0"
implementation "com.android.support.constraint:constraint-layout:1.1.3"
testImplementation "junit:junit:4.12"
androidTestImplementation "com.android.support.test:runner:1.0.2"
androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.2"
}
then call the relevant method in one of the default cpp files of the project.
native-lib.cpp
-sharpinclude <jni.h>
-sharpinclude <string>
-sharpinclude <libavcodec/avcodec.h>
extern "C" JNIEXPORT jstring JNICALL
Java_cn_lxw_ffmpeg_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = avcodec_configuration();
return env->NewStringUTF(hello.c_str());
}
then I tried to run the project, but the following error occurred:
Build native-lib armeabi-v7a
ninja: error: "../../../../libs/armeabi-v7a/libavfilter.so", needed by "../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so", missing and no known rule to make it
:app:externalNativeBuildDebug FAILED
:app:buildInfoGeneratorDebug
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ":app:externalNativeBuildDebug".
> Build command failed.
Error while executing process D:\Dev\sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\Dev\WorkSpace\FFmpeg\app\.externalNativeBuild\cmake\debug\armeabi-v7a --target native-lib}
ninja: error: "../../../../libs/armeabi-v7a/libavfilter.so", needed by "../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so", missing and no known rule to make it
could you tell me how to fix it? thank you!