1. 软件版本
Android Studio版本:4.2lame版本:3.100
ndk编译参考:centos7使用ndk编译lamefdk-aac版本:2.0.2
ndk编译参考:centos使用ndk编译fdk-acc-2.0.2x264版本:
ndk编译参考:centos7使用ndk编译x264FFmpeg版本:4.4.3
ndk编译参考:centos7使用ndk编译FFmpeg
2. 新建Android Native C++项目
2.1 新建项目



2.2 添加库文件和头文件

2.3 修改build.gradle文件
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.icebear.testappwithffmpeg"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
// 1. 添加参数
cppFlags "-frtti -fexceptions"
// 2. 设置支持的架构 "arm64-v8a", "armeabi-v7a", "x86_64", "x86"
abiFilters "armeabi-v7a", "x86"
}
}
// 3. 设置支持的架构 "arm64-v8a", "armeabi-v7a", "x86_64", "x86"
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.10.2'
}
}
buildFeatures {
viewBinding true
}
// 4. 设置ndk版本,可以不添加
// ndkVersion "21.0.6113669"
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
2.4 修改CmakeLists.txt文件
# 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.10.2)
# Declares and names the project.
project("testappwithffmpeg")
# 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.
# 设置文件位置的变量
file(GLOB SOURCE
${CMAKE_SOURCE_DIR}/*.cpp
${CMAKE_SOURCE_DIR}/*.h)
# 导入编译时的头文件
include_directories(${CMAKE_SOURCE_DIR}/include)
# 设置链接库目录
set(my_lib_path ${CMAKE_SOURCE_DIR}/libs/${CMAKE_ANDROID_ARCH_ABI})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${my_lib_path}")
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).
${SOURCE} )
# 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
# avformat必须在avcodec之前添加
avformat
avcodec
avfilter
avutil
swresample
swscale
x264
mp3lame
fdk-aac
android
z
m
# Links the target library to the log library
# included in the NDK.
${log-lib} )
2.5 修改native-lib.cpp
#include <jni.h>
#include <string>
extern "C" {
#include "include/libavcodec/avcodec.h"
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_icebear_testappwithffmpeg_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
// return env->NewStringUTF(hello.c_str());
return env->NewStringUTF(avcodec_configuration());
}
3. 测试结果
3.1 模拟器armeabi-v7a

3.2 模拟器x86

4. 遇到的问题:
4.1 运行x86版本时报错:

可能原因:
编译lame的时候没有使用--with-pic,需要使用动态库
解决方法:
使用动态库或者需要重新编译静态库
4.2 运行armeabi-v7a时报错:

可能原因:
编译lame的时候没有使用--with-pic,需要使用动态库
解决方法:
使用动态库或者需要重新编译静态库
4.3 对armeabi-v7a,x86,x86_64项目进行Build出现问题:


原因:

原因:
使用的是静态库,但编译fdk-aac和lame时没有配置--with-pic
解决方法:
在相应架构的lib目录添加动态库即可


5. 参考:
- 【Android NDK 开发】Android Studio 使用 CMake 导入静态库 ( CMake 简介 | 构建脚本路径配置 | 引入静态库 | 指定静态库路径 | 链接动态库 )
- ndk-r20b 编译 armv7报错 error:requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC
- Android FFmpeg集成
- FFmpeg bug记录:error: undefined reference to ‘avcodec_configuration()’
- requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC

文章详细描述了在AndroidStudio中使用NDK编译lame、fdk-aac、x264和FFmpeg的过程,以及创建AndroidNativeC++项目并配置CMakeLists.txt文件。在不同架构上运行时遇到了错误,包括对armeabi-v7a和x86的兼容性问题,这些问题涉及到静态库和动态库的使用,以及编译选项。解决方案包括添加动态库和重新编译库以支持-fPIC选项。

1521

被折叠的 条评论
为什么被折叠?



