Andriod studio3.5搭建ffmpeg helloworld(静态库文件)

环境

win7、jdk8、Andriod studio3.5、 NDK21


写在前

  1. 采用编译好的ffmpeg静态文件(多个静态文件,需要添加 -lz)
  2. Android studio3.5创建navtive c++工程已经配置好jni demo,非常方便

步骤

  1. 编译ffmpeg(参考 win7下ffmpeg交叉编译android版)。得到以下文件

    image.png

  2. AS添加C++工程


    image.png
  3. 把编译好ffmpeg文件拷贝到项目


    image.png
  4. 修改 app下 app/src/main/cpp的 CMakeList.txt
    提示:配置CMAKE_CXX_FLAGS参数,-L是编译时寻找路径,即提示编译器从armeabi-v7a下寻找avformat avcodec avfilter avutil swresample swscale

cmake_minimum_required(VERSION 3.4.1)

add_library(
        native-lib
        SHARED
        native-lib.cpp)

include_directories(include)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/../../../libs/${CMAKE_ANDROID_ARCH_ABI}")

find_library(
        log-lib
        log)

target_link_libraries(
        native-lib
        avformat avcodec avfilter avutil swresample swscale
        andriod
        z
        ${log-lib})
  1. 修改 app的 build.gradle
apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    defaultConfig {
        applicationId "com.example.mmffndk"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
                abiFilters 'armeabi-v7a'    //修改点1
            }
        }

        ndk{                //修改点2
            abiFilters 'armeabi-v7a'
        }

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

  1. 修改app\src\main\cpp\native-lib.cpp,测试ffmpeg集成。
    其中avutil_configuration函数是获取ffmpeg配置信息
#include 
#include 

extern "C" {
#include "include/libavutil/avutil.h"
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_mmffndk_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    return env->NewStringUTF(avutil_configuration());
}
  1. 启动模拟。


    image.png

参考:
android全平台编译ffmpeg合并为单个库实践

你可能感兴趣的:(Andriod studio3.5搭建ffmpeg helloworld(静态库文件))