Android平台通用编译环境

1、Android NDK下载:(内含ndk-build)

https://developer.android.google.cn/ndk/downloads/
http://www.voidcn.com/article/p-fjfskgpc-bph.html
https://developer.android.google.cn/ndk/guides/stable_apis#purpose

2、Android平台通用编译环境

2.1、Linux下编译Android程序(shell脚本)

#!/bin/bash
if [ $# -ne 1]; then
    echo "./build.sh ndk_dir"
    exit
fi

# get current dir
PJTDIR=$(pwd)

#check if we are in the ndk directory
cd $1
if [ ! -f "ndk-build"]; then
    echo "Run this script under NDK directory\n"
    exit
fi
echo "------------------------clear output dir-------------------------"
find $PJTDIR/obj -name "*.[od]" | xargs rm -rf
find $PJTDIR/obj -name "*.so" | xargs rm -rf
find $PJTDIR/libs -name  "*.so" | xargs rm -rf
find $PJTDIR/out -name "*.so" | xargs rm -rf

find $PJTDIR/obj -name "*.[od]" | xargs rm -rf
find $PJTDIR/obj -name "*.a" | xargs rm -rf
find $PJTDIR/libs -name  "*.a" | xargs rm -rf
find $PJTDIR/out -name "*.a" | xargs rm -rf
echo "------------------------build static library----------------------"
#cp $PJRDOR/jni/Android_lib.mk $PJRLIB/jni/Android.mk
./ndk-build -B -C $PJTDIR APP_BUILD_SCRIPT:=Android_lib.mk
cp  $PJTDIR/obj/local/armeabi/libxxx.a  $PJTDIR/out/armeabi/
cp  $PJTDIR/obj/local/armeabi-v7a/libxxx.a  $PJTDIR/out/armeabi-v7a/
cp  $PJTDIR/obj/local/arm64-v8a/libxxx.a  $PJTDIR/out/arm64-v8a/
cp  $PJTDIR/obj/local/x86/libxxx.a  $PJTDIR/out/x86/
cp  $PJTDIR/obj/local/x86_64/libxxx.a  $PJTDIR/out/x86_64/
cp  $PJTDIR/obj/local/mips/libxxx.a  $PJTDIR/out/mips/
cp  $PJTDIR/obj/local/mips64/libxxx.a  $PJTDIR/out/mips64/

echo "------------------------build dynamic library----------------------"
#cp $PJRDOR/jni/Android_lib.mk $PJRLIB/jni/Android.mk
./ndk-build -B -C $PJTDIR APP_BUILD_SCRIPT:=Android_lib.mk
cp  $PJTDIR/obj/local/armeabi/libxxx.so $PJTDIR/out/armeabi/
cp  $PJTDIR/obj/local/armeabi-v7a/libxxx.so  $PJTDIR/out/armeabi-v7a/
cp  $PJTDIR/obj/local/arm64-v8a/libxxx.so  $PJTDIR/out/arm64-v8a/
cp  $PJTDIR/obj/local/x86/libxxx.so  $PJTDIR/out/x86/
cp  $PJTDIR/obj/local/x86_64/libxxx.so  $PJTDIR/out/x86_64/
cp  $PJTDIR/obj/local/mips/libxxx.so  $PJTDIR/out/mips/
cp  $PJTDIR/obj/local/mips64/libxxx.so  $PJTDIR/out/mips64/

echo "------------------------build dynamic executable----------------------"
#cp $PJRDOR/jni/Android_app.mk  $PJRLIB/jni/Android.mk
./ndk-build -B -C $PJTDIR APP_BUILD_SCRIPT:=Android_app.mk
cp  $PJTDIR/obj/local/armeabi/xxx $PJTDIR/out/armeabi/
cp  $PJTDIR/obj/local/armeabi-v7a/xxx  $PJTDIR/out/armeabi-v7a/
cp  $PJTDIR/obj/local/arm64-v8a/xxx  $PJTDIR/out/arm64-v8a/
cp  $PJTDIR/obj/local/x86/xxx  $PJTDIR/out/x86/
cp  $PJTDIR/obj/local/x86_64/xxx  $PJTDIR/out/x86_64/
cp  $PJTDIR/obj/local/mips/xxx  $PJTDIR/out/mips/
cp  $PJTDIR/obj/local/mips64/xxx  $PJTDIR/out/mips64/

echo "------------------------build static executable----------------------"
#cp $PJRDOR/jni/Android_app_static.mk  $PJRLIB/jni/Android.mk
./ndk-build -B -C $PJTDIR APP_BUILD_SCRIPT:=Android_app_static.mk
cp  $PJTDIR/obj/local/armeabi/xxx $PJTDIR/out/armeabi/
cp  $PJTDIR/obj/local/armeabi-v7a/xxx  $PJTDIR/out/armeabi-v7a/
cp  $PJTDIR/obj/local/arm64-v8a/xxx  $PJTDIR/out/arm64-v8a/
cp  $PJTDIR/obj/local/x86/xxx  $PJTDIR/out/x86/
cp  $PJTDIR/obj/local/x86_64/xxx  $PJTDIR/out/x86_64/
cp  $PJTDIR/obj/local/mips/xxx  $PJTDIR/out/mips/
cp  $PJTDIR/obj/local/mips64/xxx  $PJTDIR/out/mips64/

echo "------------------------tar---------------------"
tar -zcvf $PJTDIR/out/libxxx_android_armeabi.tar.gz  -C $PJTDIR/out/armeabi/
tar -zcvf $PJTDIR/out/libxxx_android_armeabi-v7a.tar.gz  -C $PJTDIR/out/armeabi-v7a/
tar -zcvf $PJTDIR/out/libxxx_android_arm64-v8a.tar.gz  -C $PJTDIR/out/arm64-v8a/
tar -zcvf $PJTDIR/out/libxxx_android_x86.tar.gz  -C $PJTDIR/out/x86/
tar -zcvf $PJTDIR/out/libxxx_android_x86_64.tar.gz  -C $PJTDIR/out/x86_64/
tar -zcvf $PJTDIR/out/libxxx_android_mips.tar.gz  -C $PJTDIR/out/mips/
tar -zcvf $PJTDIR/out/libxxx_android_mips64.tar.gz  -C $PJTDIR/out/mips64/
echo "------------------------check results---------------------"
ls -l $PJTDIR/out
ls -l $PJTDIR/armeabi
ls -l $PJTDIR/armeabi-v7a
ls -l $PJTDIR/arm64-v8a
ls -l $PJTDIR/x86
ls -l $PJTDIR/x86-64
ls -l $PJTDIR/mips
ls -l $PJTDIR/mips-64

其中:核心命令为:
./ndk-build -B -C $PJTDIR APP_BUILD_SCRIPT:=Android_lib.mk
其中,-B表示强制rebuild,-C表示Android.mk和Application.mk两个文件夹所在路径,就是指定makefile的路径,与linux中make -C dir是同一个含义,APP_BUILD_SCRIPT用于指定编译的makefile,同linux中的make -f makefile,NDK_PROJECT_PATH用于指定makefile的路径。

注意:
1、无论是编译动态库、静态库还是可执行文件,都需要各自对应的Android.mk和Application.mk这两个文件。
2、Application.mk通常用于指定需要编译的平台的个数,比如
APP_ABI := x86 x86_64 armeabi armeabi-v7a arm64-v8a
APP_PLATFORM := android-9
NDK_TOOLCHAIN_VERSION := 4.9
3、Android.mk相当于被Applicatioin.mk调用的,即每个平台都会调用一次Android.mk产生一个相应平台的编译出来的模块。

不同 CPU 和架构的 ABI 设置如下表所示:

CPU 和架构 设置
ARMv5TE armeabi
ARMv7 armeabi-v7a
ARMv8 AArch64 arm64-v8a
i686 x86
x86-64 x86_64
mips32 (r1) mips
mips64 (r6) mips64
全部 all

2.2、Window下编译Android程序(Python脚本)

在没有shell环境情况下,采用windows环境下的python脚本:

//cmd="ndk-build -B -C ./ NDK_PROJECT_PATH=./ APP_BUILD_SCRIPT=./Android.mk"
#coding=utf8
import os
import sys
import glob
import shutil
import platform
from sys import argv

share_lib_name = 'libxxx.so'
static_lib_name =  'libxxx.a'
static_exe = 'demo'
#检查是否为windows系统
def  isWindowsSys():
    return 'Windows' in platform.system()
#检查是否为Linux系统
def  isLinuxSys():
    return 'Linux' in platform.system()
#清空目录下的内容
def delDirFile(path):
    def i in os.listdir(path):
        file_path = os.path.join(path, i)
        if os.path.isfile(file_path):
            os.remove(file_path)
        else
            delDirFile(file_path)

def process(ndk_dir, android_dir, pure_c, delimiter):
    ndk_build = ndk_dir + delimiter + 'ndk-build'
    app_mk = android_dir + delimiter + 'jni' + delimiter + 'Application.mk'
    mk_dir = android_dir + delimiter + 'jni'
    static_lib_dir = android_dir + delimiter + 'obj' + delimiter + 'local' + delimiter
    out_sha_lib = android_dir + delimiter + 'out' + delimiter
    share_lib_dir = android_dir + delimiter + 'obj' + delimiter + 'local' + delimiter
    out_sta_lib = android_dir + delimiter + 'out' + delimiter
    if isWindowsSys():
        cpy = "copy "
    else:
        cpy = "cp "
# "=================CLEAR OUTPUT DIR========================="
outdir_files = glob.glob(android_dir + delimiter + 'out' + delimiter + '*')
for outdir in outdir_files:
    print(outdir)
    delDelFile(outdir)
    isExist = os.path.exists(ourdir)
    if(not isExist):
        os.mkdir(outdir)

pFile = open(app_mk, 'w')
cmd = 'APP_ABI := x86 x86_64 armeabi armeabi-v7a arm64-v8a \nAPP_PLATFORM := android-9\nNDK_TOOLCHAIN_VERSION := 4.9'
pFile.writeLines(cmd)
pFile.write('\n')
pFile.close()
# "==================BUILD SHARED LIBRARY===================="
cmd = ndk_build + " -B " + "NDK_PROJECT_PATH:=" + android_dir + "APP_BUILD_SCRIPT:=" + mk_dir + delimiter + "Android_lib.mk" + " V=1 SHARED=0"
print(cmd)
os.system(cmd)
cmd = cpy + share_lib_dir + "x86" + delimiter + share_lib_name + "  " + out_sha_lib + "x86" + delimiter
print(cmd)
os.system(cmd)
##其他平台依次类推
# "==================BUILD STATIC LIBRARY===================="
cmd = ndk_build + " -B " + "NDK_PROJECT_PATH:=" + android_dir + "APP_BUILD_SCRIPT:=" + mk_dir + delimiter + "Android_lib.mk"
print(cmd)
os.system(cmd)
cmd = cpy + static_lib_dir + "x86" + delimiter + static_lib_name + "  " + out_sta_lib + "x86" + delimiter
print(cmd)
os.system(cmd)
##其他平台依次类推
# "==================BUILD STATIC EXE ===================="
cmd = ndk_build + " -B " + "NDK_PROJECT_PATH:=" + android_dir + "APP_BUILD_SCRIPT:=" + mk_dir + delimiter + "Android_app.mk"
print(cmd)
os.system(cmd)
cmd = cpy + static_lib_dir + "x86" + delimiter + static_lib_name + "  " + out_sta_lib + "x86" + delimiter
print(cmd)
os.system(cmd)
##其他平台依次类推

##############################main函数入口######################
if __name__ == '__main__':
    if(len(argv) < 3):
        printf("Tips: build_android.py android_ndk_dir pure_c=0/1\n")
        exit()
    delimiter =os.path.sep
    ndk_dir = argv[1]
    pure_c = argv[2]
    android_dir = os.getcwd()
    process(ndkdir,android_dir,pure_c,delimiter)

3、Android平台真机测试方法

android真机测试方法

4、Android平台通用编译模板

Android平台通用编译模板

5、参考

关于Application.mk,Android.mk和JNI的介绍更多可参考:
https://developer.android.google.cn/ndk/guides/android_mk
https://developer.android.google.cn/ndk/guides/application_mk
https://developer.android.google.cn/ndk/guides/ndk-build
https://www.ibm.com/developerworks/cn/opensource/os-cn-android-build/
https://blog.csdn.net/yuanjize1996/article/details/54376228
https://blog.csdn.net/xyz_lmn/article/details/6966259/


THE END!


本博文只能阅读,谢绝转载,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 2963033731@qq.com

文章标题:Android平台通用编译环境

字数:1.6k

本文作者:Soaring Lee

发布时间:2018-11-13, 15:13:47

最后更新:2021-06-14, 12:13:44

原始链接:https://soaringleefighting.github.io/2018/11/13/【android】Android平台通用编译环境/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

×

喜欢就点赞,疼爱就打赏

相册