本文2017-3-11
参考了:
http://blog.csdn.net/huweijian5/article/details/52883969
基本环境:
1. 操作系统:Windows10 64位;
2. Android Studio 2.3,64位。
3. Android NDK开发包:直接使用Android Studio安装NDK。(需要梯子)
4.按照上述三编译好了ffmpeg相关的.so共享库文件和按照上述一编译好的NDK Hello World程序
第一步:拷贝so文件
将“一、环境搭建,NDK Hello World”的工程中项目下的\app\src\main建立jniLibs目录,再在该目录下建立armeabi目录,将avcodec-57.so、avformat-57.so、avutil-55.so、swresample-2.so、swscale-4.so拷贝到该目录下。
第二步:修改CmakeList.txt文件
cmake_minimum_required(VERSION 3.4.1)
set(lib_src_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI})
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include)
add_library(avcodec-57_lib SHARED IMPORTED)
set_target_properties(avcodec-57_lib PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/libavcodec-57.so)
add_library(avformat-57_lib SHARED IMPORTED)
set_target_properties(avformat-57_lib PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/libavformat-57.so)
add_library(avutil-55_lib SHARED IMPORTED)
set_target_properties(avutil-55_lib PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/libavutil-55.so)
add_library(swresample-2_lib SHARED IMPORTED)
set_target_properties(swresample-2_lib PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/libswresample-2.so)
add_library(swscale-4_lib SHARED IMPORTED)
set_target_properties(swscale-4_lib PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/libswscale-4.so)
add_library( native-lib SHARED
src/main/cpp/native-lib.cpp )
find_library(log-lib log )
target_link_libraries( native-lib
${log-lib}
avcodec-57_lib
avformat-57_lib
avutil-55_lib
swresample-2_lib
swscale-4_lib
)
第三步:修改native-lib.cpp文件
#include <jni.h>
#include <string>
extern "C"{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
#include "libavutil/avutil.h"
};
extern "C"
JNIEXPORT jstring JNICALL
Java_com_szboot_helloffmpeg_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
av_register_all();
return env->NewStringUTF(hello.c_str());
}
【坑】这样以后进行同步和编译,将出现如下错误:
Error:error: '../../../../src/main/jniLibs/mips64/libavcodec-57.so', needed by '../../../../build/intermediates/cmake/debug/obj/mips64/libnative-lib.so', missing and no known rule to make it
解决办法,见第四步
第四步:修改build.gradle文件
在andriod studio面板的Gradle Script -> build.gradle(Module:app),在defaultConfig {……}的段里面,加上这段
ndk {
abiFilters 'armeabi'
}
这样就可以编译运行了。