一、安装cywin + ndk
cywin和NDK网上下载最新的就好,安装cywin时最好是选择默认安装,不需要全部安装,有很大一部分是不需要用到的,比较浪费时间.
二、下载ffmpeg,新建一个jni目录放置代码,我的目录是E:\ffmpeg\ffmpeg2\jni
三、配置ffmpeg
复制下面的内容 保存为build_andriod.sh放到E:\ffmpeg\ffmpeg2\jni
\ffmpeg里面
注意 NDK,SYSROOT为自己本地的路径
NDK=D:/worktool/android-ndk-r10c
SYSROOT=$NDK/platforms/android-21/arch-arm
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
function build_one
{
./configure \
--prefix=$PREFIX \
--enable-shared \
--disable-static \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-avdevice \
--disable-doc \
--disable-symver \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--target-os=linux \
--arch=arm \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS -std=c99" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
}
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
build_one
cygwin进入这个目录,执行 ./
build_andriod.sh
可能会出现以下问题
. 提示
1、
./configure: line 861: gas-preprocessor.pl: command not found
解决方法如下:
2、D:/android-ndk-r5b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/wind
ows/bin/arm-linux-androideabi-gcc is unable to create an executable file.
C compiler test failed. 错误
是需要增加个tmp文件夹 放配置生成的各种文件 具体错误信息可以看jni下面的config.log文件
然后更改ffmpeg自带的configure文件
# set temporary file name
: ${TMPDIR:=$TEMPDIR}
: ${TMPDIR:=$TMP}
: ${TMPDIR:=tmp}改为
# set temporary file name
#: ${TMPDIR:=$TEMPDIR}
#: ${TMPDIR:=$TMP}
: ${TMPDIR:=C:/cygwin64/tmp}
这样应该能顺利编译通过了
提示 creating config.h config.mak 之类的 在看下jni下面 如果有这2个文件就是成功了
四、创建av.mk放在jni/ffmpeg下 复制下需的内容到av.mk里
# LOCAL_PATH is one of libavutil, libavcodec, libavformat, or libswscale
#include $(LOCAL_PATH)/../config-$(TARGET_ARCH).mak
include $(LOCAL_PATH)/../config.mak
OBJS :=
OBJS-yes :=
MMX-OBJS-yes :=
include $(LOCAL_PATH)/Makefile
# collect objects
OBJS-$(HAVE_MMX) += $(MMX-OBJS-yes)
OBJS += $(OBJS-yes)
FFNAME := lib$(NAME)
FFLIBS := $(foreach,NAME,$(FFLIBS),lib$(NAME))
FFCFLAGS = -DHAVE_AV_CONFIG_H -Wno-sign-compare -Wno-switch -Wno-pointer-sign
FFCFLAGS += -DTARGET_CONFIG=\"config-$(TARGET_ARCH).h\"
ALL_S_FILES := $(wildcard $(LOCAL_PATH)/$(TARGET_ARCH)/*.S)
ALL_S_FILES := $(addprefix $(TARGET_ARCH)/, $(notdir $(ALL_S_FILES)))
ifneq ($(ALL_S_FILES),)
ALL_S_OBJS := $(patsubst %.S,%.o,$(ALL_S_FILES))
C_OBJS := $(filter-out $(ALL_S_OBJS),$(OBJS))
S_OBJS := $(filter $(ALL_S_OBJS),$(OBJS))
else
C_OBJS := $(OBJS)
S_OBJS :=
endif
C_FILES := $(patsubst %.o,%.c,$(C_OBJS))
S_FILES := $(patsubst %.o,%.S,$(S_OBJS))
FFFILES := $(sort $(S_FILES)) $(sort $(C_FILES))
五、创建各种.mk文件
jni/ffmpeg根目录创建 Android.mk
写入:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_WHOLE_STATIC_LIBRARIES := libavcodec libavformat libpostproc libswscale libavutil
LOCAL_MODULE := ffmpeg
LOCAL_LDFLAGS += -fuse-ld=bfd
LOCAL_LDLIBS := \
-llog \
-lgcc \
-lz
LOCAL_ARM_MODE := arm
include $(BUILD_SHARED_LIBRARY)
include $(call all-makefiles-under,$(LOCAL_PATH))
在libavcodec中添加Andorid.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=\
$(LOCAL_PATH)\
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_ARM_MODE := arm
LOCAL_SHORT_COMMANDS := true
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
在libavformat中添加Andorid.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=\
$(LOCAL_PATH)\
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_CFLAGS += -include "string.h" -Dipv6mr_interface=ipv6mr_ifindex
LOCAL_LDLIBS := -lz
LOCAL_ARM_MODE := arm
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
在libavutil中添加Android.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=\
$(LOCAL_PATH)\
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_ARM_MODE := arm
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
在libpostproc中添加Android.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=\
$(LOCAL_PATH)\
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_ARM_MODE := arm
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
在libswscale中添加Android.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=\
$(LOCAL_PATH)\
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_ARM_MODE := arm
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
mk文件完成后
cygwin 进入到jni目录下,执行ndk-build
编译过程可能会出现错,详细请见
android ffmpeg移植编译错误汇总
六、Android NDK引用预编译的动态链接库
首先需要了解的关于共享库的背景知识,
http://www.cnblogs.com/GoAhead/p/4186638.html
LOCAL_PATH := $(call my-dir)
#===========================
include $(CLEAR_VARS)
LOCAL_MODULE := libffmpeg-prebuilt
LOCAL_SRC_FILES := prebuild/libffmpeg.so
include $(PREBUILT_SHARED_LIBRARY)
#===========================
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg_codec
LOCAL_ARM_MODE := arm
LOCAL_SRC_FILES :=com_android_fuxc1_ffmpegdemo_FfmpegNative.c
LOCAL_SHARED_LIBRARIES:= ffmpeg-prebuilt//注意是不需要加lib的
include $(BUILD_SHARED_LIBRARY)
#===========================
七、完成了以上的六步后基本上是大功告成了,只需要将生成的so库加入到jnilib里生成APK就行了(android jni调后ffmpeg部分目前先省略,后续的学习中会补上)