android ndk clang交叉编译ffmpeg动态库踩坑

1.ffmpeg默认使用gcc编译,在android上无法使用,否则各种报错,所以要用ndk的clang编译

2.下载ffmpeg源码

修改configure文件,增加命令 cross_prefix_clang

修改以下命令

java 复制代码
cc_default="${cross_prefix}${cc_default}"
cxx_default="${cross_prefix}${cxx_default}"

修改后

java 复制代码
cc_default="${cross_prefix_clang}${cc_default}"
cxx_default="${cross_prefix_clang}${cxx_default}"

3.新建脚本文件,ffmpeg目录下执行脚本文件即可。

编译32位,则ARCH改为arm,CPU改为armv7-a,TARGET改为armv7a-linux-androideabi

注意,r25版本的ndk移除了交叉编译工具,改成了llvm,所以要把交叉编译工具路径指向llvm的路径

java 复制代码
#!/bin/bash
set -x
# 目标Android版本
API=21
ARCH=arm64
CPU=armv8-a
TARGET=aarch64-linux-android
#so库输出目录
OUTPUT=/root/Desktop/compile/ffmpeg/build_$CPU
#NDK路径
NDK=/root/Desktop/compile/ndk/android-ndk-r25c
#NDK=/root/Desktop/compile/ndk/android-ndk-r20b
# 编译工具链路径
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
# 编译环境
SYSROOT=$TOOLCHAIN/sysroot

#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU -DBIONIC_IOCTL_NO_SIGNEDNESS_OVERLOAD"

#point build to llvm 
ln -s $TOOLCHAIN/bin/llvm-ar $TOOLCHAIN/bin/$TARGET-ar
ln -s $TOOLCHAIN/bin/llvm-nm $TOOLCHAIN/bin/$TARGET-nm
ln -s $TOOLCHAIN/bin/llvm-ranlib $TOOLCHAIN/bin/$TARGET-ranlib
ln -s $TOOLCHAIN/bin/llvm-strip $TOOLCHAIN/bin/$TARGET-strip

function build
{

  
   ./configure \
  --prefix=$OUTPUT \
  --target-os=android \
  --arch=$ARCH \
  --cpu=$CPU \
  --enable-cross-compile \
  --enable-shared \
  --disable-static \
  --disable-vulkan \
  --sysroot=$SYSROOT \
  --cross-prefix=$TOOLCHAIN/bin/$TARGET- \
  --cross-prefix-clang=$TOOLCHAIN/bin/$TARGET$API- \
  

  make clean all
  make install
}

build

4.android studio 导入

5.cmake配置

java 复制代码
cmake_minimum_required(VERSION 3.22.1)

# Declares and names the project.

project("teskndk")

# 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.

#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
#include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/")

add_library( # Sets the name of the library.
        teskndk

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

add_library(avcodec SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libavcodec.so)

add_library(avdevice SHARED IMPORTED)
set_target_properties(avdevice PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libavdevice.so)

add_library(avfilter SHARED IMPORTED)
set_target_properties(avfilter PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libavfilter.so)

add_library(avformat SHARED IMPORTED)
set_target_properties(avformat PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libavformat.so)

add_library(avutil SHARED IMPORTED)
set_target_properties(avutil PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libavutil.so)

add_library(swresample SHARED IMPORTED)
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libswresample.so)

add_library(swscale SHARED IMPORTED)
set_target_properties(swscale PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libswscale.so)



# 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.
        teskndk

        avcodec
        avdevice
        avfilter
        avformat
        avutil
        swresample
        swscale

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

成功运行

相关推荐
fundroid18 分钟前
Kotlin 协程:Channel 与 Flow 深度对比及 Channel 使用指南
android·kotlin·协程
草字27 分钟前
cocos 打包安卓
android
DeBuggggggg1 小时前
centos 7.6安装mysql8
android
浩浩测试一下2 小时前
渗透信息收集- Web应用漏洞与指纹信息收集以及情报收集
android·前端·安全·web安全·网络安全·安全架构
移动开发者1号3 小时前
深入理解原子类与CAS无锁编程:原理、实战与优化
android·kotlin
陈卓4103 小时前
MySQL-主从复制&分库分表
android·mysql·adb
移动开发者1号3 小时前
深入理解 ThreadLocal:原理、实战与优化指南
android·kotlin
zhangphil3 小时前
Android PNG/JPG图ARGB_8888/RGB_565‌解码形成Bitmap在物理内存占用大小的简单计算
android
厦门德仔4 小时前
【WPF】WPF(样式)
android·java·wpf
aqi005 小时前
FFmpeg开发笔记(七十二)Linux给FFmpeg集成MPEG-5视频编解码器EVC
android·ffmpeg·音视频·流媒体