1、創建一個NDK工程
2、安裝CMake
3、配置NDK
4、導入ffmpeg編譯結果
# 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)
# 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( # 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).
src/main/cpp/native-lib.cpp )
#增加頭文件搜索目錄
include_directories(src/main/cpp/ffmpeg/include/)
# 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
#增加ffmpeg庫,注意順序(具體看lib/pkgconfig的依賴關係)
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavfilter.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavformat.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavcodec.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libswresample.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libswscale.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavutil.a
#增加ffmpeg所需要的依賴庫
-lm -lz
# Links the target library to the log library
# included in the NDK.
${log-lib}
)
- 指定ffmpeg頭文件的搜索路徑;
- 將ffmpeg的靜態庫添加到構建中。
6、編寫代碼
extern "C"
JNIEXPORT jstring JNICALL
Java_cn_bassy_ffmpeg01_MainActivity_stringFromJNI(JNIEnv* env, jobject /* this */) {
return env->NewStringUTF(avcodec_configuration());
}
7、編譯程序
8、錯誤問題及解決方法
Error:(15) undefined reference to 'avcodec_register_all()'
Error:(15) undefined reference to 'avcodec_configuration()'
extern "C" {
JNIEXPORT jstring JNICALL
Java_cn_bassy_ffmpeg01_MainActivity_stringFromJNI(JNIEnv* env, jobject /* this */) {
return env->NewStringUTF(avcodec_configuration());
}
};
Error:error: undefined reference to 'av_log'
Error:error: undefined reference to 'av_log'
Error:error: undefined reference to 'av_log'
Error:error: undefined reference to 'av_log'
Error:error: undefined reference to 'av_get_bytes_per_sample'
Error:error: undefined reference to 'av_crc'
Error:error: undefined reference to 'av_crc'
Error:error: undefined reference to 'av_crc'
target_link_libraries( # Specifies the target library.
native-lib
#增加ffmpeg庫,注意順序(具體看lib/pkgconfig的依賴關係)
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavfilter.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavformat.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavcodec.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libswresample.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libswscale.a
${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg/lib/libavutil.a
#增加ffmpeg所需要的依賴庫
-lm -lz
# Links the target library to the log library
# included in the NDK.
${log-lib}
)
defaultConfig {
applicationId "cn.bassy.ffmpeg01"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
APP_STL := gnustl_static
APP_PLATFORM := android-17
APP_ABI := armeabi
/.:30:2: : -
/.:205:28: : use 'UINT64_C'
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
#undef _STDINT_H
#endif
# include "stdint.h"
#endif
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif