.so 依赖目录 cmake_Andorid FFmpeg 学习02 AS导入生成的so动态库

  • Post author:
  • Post category:其他


经过 Andorid FFmpeg 学习01 源码生成动态库 第一步生成了动态库,现在导入AS并显示基本的版本号。

当前电脑环境:Win7 64位

AS版本:3.5.3

1、新建工程FFMpeg0

选择Native C++,因为要用jni。一路Next创建工程

4a48ff651134df2fa687daeb6288c74b.png

2、在appsrcmain中新建jniLibs(AS默认的so库存放路径),并将前面生成的so库和头文件全部复制到该目录下,我这里只导入了armeabi-v7a

7076d32a95e9006a3c8aab0b34273692.png

jniLibs目录文件

armeabi-v7a 文件夹下包含的文件

6c76358543f318c2d6e4c6f82c479769.png

armeabi-v7a 文件夹文件

include 文件夹下包含的文件

b6a7d2cf3e7ecd6a870b575683d41479.png

include 文件夹文件

3、配置cmake

设置so库路径

set(ffmpeg_path ${CMAKE_SOURCE_DIR}/../jniLibs)

设置头文件目录

include_directories (${ffmpeg_path}/include)

添加so库

#将第三方库作为动态库引用add_library(avcodec             SHARED            IMPORTED)#指定第三方库的绝对路径set_target_properties(avcodec                      PROPERTIES IMPORTED_LOCATION                      ${ffmpeg_path}/${ANDROID_ABI}/libavcodec.so)

链接到生成的native-lib.so库中

target_link_libraries( # Specifies the target library.                      native-lib                                            avcodec                                            # Links the target library to the log library                       # included in the NDK.                      ${log-lib})

CMakeLists完整代码

# 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)#设置so库路径set(ffmpeg_path ${CMAKE_SOURCE_DIR}/../jniLibs)#设置头文件目录include_directories (${ffmpeg_path}/include)# Creates and names a library, sets it as either STATIC# or SHARED, and provides the relative paths to its source code.# You can define multiple libraries, and CMake builds them for you.# Gradle automatically packages shared libraries with your APK.#将第三方库作为动态库引用add_library(avcodec        SHARED        IMPORTED)#指定第三方库的绝对路径set_target_properties(avcodec        PROPERTIES IMPORTED_LOCATION        ${ffmpeg_path}/${ANDROID_ABI}/libavcodec.so)#将第三方库作为动态库引用add_library(avdevice        SHARED        IMPORTED)#指定第三方库的绝对路径set_target_properties(avdevice        PROPERTIES IMPORTED_LOCATION        ${ffmpeg_path}/${ANDROID_ABI}/libavdevice.so)#将第三方库作为动态库引用add_library(avfilter        SHARED        IMPORTED)#指定第三方库的绝对路径set_target_properties(avfilter        PROPERTIES IMPORTED_LOCATION        ${ffmpeg_path}/${ANDROID_ABI}/libavfilter.so)#将第三方库作为动态库引用add_library(avformat        SHARED        IMPORTED)#指定第三方库的绝对路径set_target_properties(avformat        PROPERTIES IMPORTED_LOCATION        ${ffmpeg_path}/${ANDROID_ABI}/libavformat.so)#将第三方库作为动态库引用add_library(avutil        SHARED        IMPORTED)#指定第三方库的绝对路径set_target_properties(avutil        PROPERTIES IMPORTED_LOCATION        ${ffmpeg_path}/${ANDROID_ABI}/libavutil.so)#将第三方库作为动态库引用add_library(swresample        SHARED        IMPORTED)#指定第三方库的绝对路径set_target_properties(swresample        PROPERTIES IMPORTED_LOCATION        ${ffmpeg_path}/${ANDROID_ABI}/libswresample.so)#将第三方库作为动态库引用add_library(swscale        SHARED        IMPORTED)#指定第三方库的绝对路径set_target_properties(swscale        PROPERTIES IMPORTED_LOCATION        ${ffmpeg_path}/${ANDROID_ABI}/libswscale.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).        native-lib.cpp)# Searches for a specified prebuilt library and stores the path as a# variable. Because CMake includes system libraries in the search path by# default, you only need to specify the name of the public NDK library# you want to add. CMake verifies that the library exists before# completing its build.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)# Specifies libraries CMake should link to your target library. You# can link multiple libraries, such as libraries you define in this# build script, prebuilt third-party libraries, or system libraries.target_link_libraries( # Specifies the target library.        native-lib        avcodec        avdevice        avfilter        avformat        avutil        swresample        swscale        # Links the target library to the log library        # included in the NDK.        ${log-lib})

4、native-lib.cpp中调用显示FFmpeg版本号

#include #include extern "C" {//util工具库#include "../jniLibs/include/libavutil/avutil.h"//编解码#include "../jniLibs/include/libavcodec/avcodec.h"//封装格式生成解析#include "../jniLibs/include/libavformat/avformat.h"//视频场景比例缩放 , 色彩映射转换#include "../jniLibs/include/libswscale/swscale.h"}extern "C" JNIEXPORT jstring JNICALLJava_com_xohn_ffmpeg0_MainActivity_stringFromJNI(        JNIEnv *env,        jobject /* this */) {    std::string hello = "Hello from C++";    //return env->NewStringUTF(hello.c_str());    return env->NewStringUTF(av_version_info());}

显示效果图

aa5b72fe407972deead328f75302f6e8.png

项目git地址:

https://gitee.com/xohn/FFmpeg.git

中的FFMpeg0