我想使用NDK工具链为Android编译一些库。我在编译libiconv
,libtiff
,libxml2
时遇到问题,并验证它们是否针对每个目标架构正确构建。我怎样才能编译像libiconv这样的普通项目并验证它是否构建正确?
答案 0 :(得分:4)
您可以在GitHub
上找到这些文件使用Automake进行编译时,您需要一个独立的工具链,它体现了一个目标架构和一个特定的平台版本。以下脚本概述了该过程。
#!/bin/bash
ANDROID_NDK_DIR=/opt/android-ndk-r13b
ANDROID_API=19
ANDROID_STL=gnustl
INSTALL_PREFIX=/opt/standalone-r13b
declare -a COMPILE_ARCHITECTURES=("arm" "x86")
pushd ${ANDROID_NDK_DIR}
for ARCH in "${COMPILE_ARCHITECTURES[@]}"
do
INSTALL_DIR=${INSTALL_PREFIX}-${ARCH}
build/tools/make_standalone_toolchain.py \
--arch ${ARCH} \
--api ${ANDROID_API} \
--stl ${ANDROID_STL} \
--install-dir ${INSTALL_DIR}
done
popd
在一些较旧的项目中,您会发现config.guess和config.sub文件无法识别Android NDK工具链,因此您需要从automake获取更新的工具链。在这个项目中,我复制了Automake 1.15中的文件,并覆盖了libiconv项目中的文件。
为架构选择的标志来自Google的ABI Management和Standalone Toolchain页面。
注意:在此示例中,我还没有编译64位目标。我需要将Android API提升到21以添加ARM64和x86_64。
ANDROID_NDK_DIR=/opt/standalone-r13b
LIBICONV_INSTALL_DIR=${HOME}/Development/libiconv
declare -a COMPILE_ARCHITECTURES=("arm" "armv7a" "x86")
SAVED_PATH="${PATH}"
for ARCH in "${COMPILE_ARCHITECTURES[@]}"
do
COMPILER_GROUP=""
COMPILER_PREFIX=""
case ${ARCH} in
"arm" )
COMPILER_GROUP=arm
;;
"armv7a" )
COMPILER_GROUP=arm
;;
"x86" )
COMPILER_GROUP=x86
;;
esac
export ANDROID_NDK_ROOT="${ANDROID_NDK_DIR}-${COMPILER_GROUP}"
ANDROID_NDK_BIN="${ANDROID_NDK_ROOT}/bin"
ANDROID_SYSROOT_DIR="${ANDROID_NDK_ROOT}/sysroot"
export PATH="${ANDROID_NDK_BIN}:${SAVED_PATH}"
export CFLAGS="--sysroot=${ANDROID_SYSROOT_DIR}"
export LDFLAGS=""
case ${ARCH} in
"arm" )
ABI_NAME=armeabi
COMPILER_PREFIX=arm-linux-androideabi
;;
"armv7a" )
ABI_NAME=armeabi-v7a
COMPILER_PREFIX=arm-linux-androideabi
CFLAGS="${CFLAGS} -march=armv7-a -mfpu=neon -mfloat-abi=softfp -mthumb"
LDFLAGS="${LDFLAGS} -march=armv7-a -Wl,--fix-cortex-a8"
;;
"x86" )
ABI_NAME=x86
COMPILER_PREFIX=i686-linux-android
CFLAGS="${CFLAGS} -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"
;;
esac
export CC=${ANDROID_NDK_BIN}/${COMPILER_PREFIX}-gcc
export CPP=${ANDROID_NDK_BIN}/${COMPILER_PREFIX}-cpp
export CXX=${ANDROID_NDK_BIN}/${COMPILER_PREFIX}-g++
export LD=${ANDROID_NDK_BIN}/${COMPILER_PREFIX}-ld
export AR=${ANDROID_NDK_BIN}/${COMPILER_PREFIX}-ar
export RANLIB=${ANDROID_NDK_BIN}/${COMPILER_PREFIX}-ranlib
export STRIP=${ANDROID_NDK_BIN}/${COMPILER_PREFIX}-strip
echo "---- Compiling for ${ARCH}"
./configure --enable-static --host="${COMPILER_PREFIX}" --prefix="${LIBICONV_INSTALL_DIR}/${ABI_NAME}"
make clean
make -j4
make install
done
export PATH="${SAVED_PATH}"
你可以在NDK中找到arm-linux-androideabi-readelf
,以确定我是否分别在armeabi和armeabi-v7a中使用Thumb-1或Thumb-2指令。
arm-linux-androideabi-readelf -A libiconv.so.2.5.1.so
Attribute Section: aeabi
File Attributes
Tag_CPU_name: "5TE"
Tag_CPU_arch: v5TE
Tag_ARM_ISA_use: Yes
Tag_THUMB_ISA_use: Thumb-1
Tag_FP_arch: VFPv2
Tag_ABI_PCS_wchar_t: 4
Tag_ABI_FP_denormal: Needed
Tag_ABI_FP_exceptions: Needed
Tag_ABI_FP_number_model: IEEE 754
Tag_ABI_align_needed: 8-byte
Tag_ABI_enum_size: int
Tag_ABI_optimization_goals: Aggressive Speed
如果您的项目支持NEON等高级功能,那么您肯定希望验证您是否使用正确的选项编译了库。
Attribute Section: aeabi
File Attributes
Tag_CPU_name: "ARM v7"
Tag_CPU_arch: v7
Tag_CPU_arch_profile: Application
Tag_ARM_ISA_use: Yes
Tag_THUMB_ISA_use: Thumb-2
Tag_FP_arch: VFPv3
Tag_Advanced_SIMD_arch: NEONv1
Tag_ABI_PCS_wchar_t: 4
Tag_ABI_FP_denormal: Needed
Tag_ABI_FP_exceptions: Needed
Tag_ABI_FP_number_model: IEEE 754
Tag_ABI_align_needed: 8-byte
Tag_ABI_enum_size: int
Tag_ABI_HardFP_use: Deprecated
Tag_ABI_optimization_goals: Aggressive Speed
Tag_CPU_unaligned_access: v6