一、 参考文章:
http://www.cnblogs.com/baopu/p/4733029.html
http://www.cnblogs.com/wainiwann/p/3785485.html
二、 编译环境:
CentOS 10
工具包:
NDK: https://dl.google.com/android/ndk/android-ndk32-r10b-linux-x86_64.tar.bz2
SDK:https://dl.google.com/android/adt/adt-bundle-linux-x86_64-20140702.zip
Ffmpeg: http://ffmpeg.org/releases/ffmpeg-2.7.2.tar.bz2
三、 NDK的安装
l 下载: https://dl.google.com/android/ndk/android-ndk32-r10b-linux-x86_64.tar.bz2
l 解压
//复制文件
cp android-ndk-r8b-linux-x86.tar.bz2 /usr/local/lib
//解压文件
tar jxvf android-ndk-r8b-linux-x86.tar.bz2
l 修改环境变量
终端运行:gedit
~/.bashrc
将下面的语句加入
#NDK setting
NDK_ROOT=/usr/local/lib/android-ndk-r8b
export NDK_ROOT
保存,终端运行:source ~/.bashrc
使之立即生效。
在终端运行:echo $NDK_ROOT
若正确打印了NDK的路径,说明环境变量修改成功。
l 测试
cd 进入android-ndk-r8b/samples/hello-jni,然后在终端运行ndk的编译脚本:
sudo
/usr/local/lib/android-ndk-r8b/ndk-build
若结果如下图,则ndk安装成功。
四、
编译FFMPEG
l 解压
tar -xvjf ffmpeg-2.7.2.tar.bz2
cd ffmpeg-2.7.2
l 配置文件my-make.sh
gedit my-make.sh
将下面的代码拷贝到my-make.sh中
#!/bin/bash
NDK=/opt/adrd-stuff/android-ndk-r10b
SYSROOT=$NDK/platforms/android-19/arch-arm
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64
function build_one
{
./configure \
--prefix=$PREFIX \
--enable-shared \
--disable-static \
--disable-doc \
--disable-ffserver \
--enable-cross-compile \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--target-os=linux \
--arch=arm \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
}
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
build_one
make -j8
make install
## end of my-make.sh
l 修改文件configure
很多ffmpeg编译资料上面说要修改configure文件 ,说的原因是 如果不修改的话,在android上
System.loadLibrary("xxx"); 的时候个别so库无法加载的问题。
打开 ffmpeg源码内 configure 文件进行如下修改:
SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR)$(SLIBNAME)'
替换为:
SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
SLIB_INSTALL_LINKS='$(SLIBNAME)'
l 编译
chmod +x my-make.sh
mkdir android/arm -p
./my-make.sh
最后就可以在android/arm下面:
~~~~~~~~~~~/android/arm$ ls
bin include lib share
五、 Android使用编译好的so库
l 项目目录
l Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := avcodec-56-prebuilt
LOCAL_SRC_FILES := ffmpeg/lib/libavcodec-56.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE :=avdevice-56-prebuilt
LOCAL_SRC_FILES :=ffmpeg/lib/libavdevice-56.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE :=avfilter-5-prebuilt
LOCAL_SRC_FILES :=ffmpeg/lib/libavfilter-5.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE :=avformat-56-prebuilt
LOCAL_SRC_FILES :=ffmpeg/lib/libavformat-56.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avutil-54-prebuilt
LOCAL_SRC_FILES :=ffmpeg/lib/libavutil-54.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avswresample-1-prebuilt
LOCAL_SRC_FILES :=ffmpeg/lib/libswresample-1.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := swscale-3-prebuilt
LOCAL_SRC_FILES :=ffmpeg/lib/libswscale-3.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE :=ffmpeg_codec
LOCAL_SRC_FILES :=FFmpegNative.c
LOCAL_LDLIBS := -llog #-ljnigraphics -lz -landroid
LOCAL_C_INCLUDES += $(LOCAL_PATH)/ffmpeg/include
LOCAL_SHARED_LIBRARIES:= \
avcodec-56-prebuilt \
avdevice-56-prebuilt \
avfilter-5-prebuilt \
avformat-56-prebuilt \
avutil-54-prebuilt
include $(BUILD_SHARED_LIBRARY)
l Application.mk
APP_PLATFORM := android-17
APP_ABI := armeabi-v7a
APP_OPTIM := debug
APP_STL := gnustl_static
APP_CPPFLAGS += -fexceptions
APP_CPPFLAGS += -frtti
l FFmpegNative.c
#include <jni.h>
#include "libavcodec/avcodec.h"
#include "libavutil/imgutils.h"
#include "libavutil/samplefmt.h"
#include "libavutil/timestamp.h"
#include "libavformat/avformat.h"
jstring
Java_com_example_ffmpegtst_MainActivity_stringFromJNI( JNIEnv* env,
jobject thiz )
{
char str[25];
sprintf(str, "%d", avcodec_version());
return (*env)->NewStringUTF(env, str);
}
l MainActivity.java
package com.example.ffmpegtst;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((TextView)findViewById(R.id.hello_jni)).setText(stringFromJNI());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
static{
System.loadLibrary("avutil-54");
System.loadLibrary("avcodec-56");
System.loadLibrary("swresample-1");
System.loadLibrary("avformat-56");
System.loadLibrary("swscale-3");
System.loadLibrary("avfilter-5");
System.loadLibrary("ffmpeg_codec");
}
public native String stringFromJNI();
}
六、注意:
NDK版本:
32bits小机用:android-ndk32-r10b-linux-x86_64.tar.bz2
64bits小机用:android-ndk64-r10b-linux-x86_64.tar.bz2
64bits编译出来的APK在S3上面运行,会有闪退的现象,
E/dalvikvm(31393): dlopen("/data/app-lib/com.az.ffmpegapp-1/libavformat-56.so") failed: dlopen failed: cannot locate symbol "atof" referenced by "libavformat-56.so"...
W/dalvikvm(31393): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/az/ffmpegapp/FFmpegNative;
E/AndroidRuntime(31393): Process: com.az.ffmpegapp, PID: 31393
E/AndroidRuntime(31393): at com.az.ffmpegapp.FFmpegNative.<clinit>(FFmpegNative.java:7)
E/AndroidRuntime(31393): at com.az.ffmpegapp.MainActivity.onCreate(MainActivity.java:15)
W/ActivityManager( 834): Force finishing activity com.az.ffmpegapp/.MainActivity