Android项目自动编译脚本记录

Android项目自动编译脚本记录
下面是编译项目的shell脚本 ant.sh:

ANDROID_PROJECT_HOME=$PWD/frameworks/runtime-src/proj.android
PLUGIN_HOME=$ANDROID_PROJECT_HOME/assets/dex/

rm -rf $ANDROID_PROJECT_HOME/bin/ &&
rm -rf $ANDROID_PROJECT_HOME/gen/ &&
#assets目录下面均是生成文件,所以也在编译前清理掉
rm -rf $ANDROID_PROJECT_HOME/assets/ &&

#保存当前编译时间,最后并入到测试版本名中
DATE=$(date +%Y%m%d_%T)

#备份版本名,在编译前备份,编译后还原
version_bak=$(cat $ANDROID_PROJECT_HOME/AndroidManifest.xml | grep versionName)
version_bak=${version_bak#*versionName=\"}
version_bak=${version_bak%%\"*}
version_bak=${version_bak%%_*}
#修改版本名
sed -i "s#\(android:versionName=\)\"\(.*\)\"#\1\"$version_bak\_$DATE\"#" $ANDROID_PROJECT_HOME/AndroidManifest.xml  &&

#编译cocos2d项目
cocos compile -p android &&

#准备编插件
PATH_OUTPUT=$ANDROID_PROJECT_HOME/bin/plugin_dex_dir/classes.dex
PATH_SOURCE=$ANDROID_PROJECT_HOME/bin/plugin_dex_dir/plugin.jar

cd $ANDROID_SDK_HOME/build-tools/21.1.2/
dx --dex  --output=$PATH_OUTPUT $PATH_SOURCE
cd -

mkdir -p $PLUGIN_HOME
cd $ANDROID_PROJECT_HOME/bin/plugin_dex_dir/

#加密压缩插件包
PLUGIN_NAME="plugin_"$version_bak".zip"
zip $PLUGIN_NAME classes.dex
zip -q -r -P123456" plugin.zip $PLUGIN_NAME
mv plugin.zip $PLUGIN_HOME
cd -

#插件包完成后,做正式编译签名前的清理
rm -rf $ANDROID_PROJECT_HOME/bin/ &&
rm -rf $ANDROID_PROJECT_HOME/gen/ &&

echo "ant release with plugin"
ant release -buildfile $ANDROID_PROJECT_HOME/build.xml &&

#还原版本名
sed -i "s#\(android:versionName=\)\"\(.*\)\"#\1\"$version_bak\"#" $ANDROID_PROJECT_HOME/AndroidManifest.xml  &&

adb install -r $ANDROID_PROJECT_HOME/bin/GameCenter-release.apk

下面是自定义的ant脚本 custom_rules.xml :


<project name="PluginRules" default="help" >

    <target name="-post-compile" >

        

        
        <mkdir dir="${out.classes.absolute.dir}.plugin" />
        <copy todir="${out.classes.absolute.dir}.plugin" >
            <fileset dir="${out.classes.absolute.dir}" >
                <include name="com/brian/plugin/**" />
            fileset>
        copy>

        <delete dir="${out.classes.absolute.dir}/com/brian/plugin" verbose="${verbose}" />
        

        <mkdir dir="${out.absolute.dir}/plugin_dex_dir" />
        <echo message="... building @{input-dir}-cases  now ...." />
        
        <jar basedir="${out.classes.absolute.dir}.plugin"
            jarfile="${out.absolute.dir}/plugin_dex_dir/plugin.jar" />
    target>

project>

下面是编译打包release的脚本,包含加密,多渠道包等:

#!/bin/sh

MARKETS="APPSTORE ROM"
ANDROID_PROJECT_HOME="./frameworks/runtime-src/proj.android"
RESOURCE_VERSION="5.0"

#将代码中的debug修改为false
sed -i "s#DEBUG = 2#DEBUG = 1#" ./src/config.lua
sed -i "s#CC_SHOW_FPS = true#CC_SHOW_FPS = false#" ./src/config.lua
sed -i "s#isDebug = true#isDebug = false#" $ANDROID_PROJECT_HOME/src/com/brian/gamecenter/App.java

echo "backup the lua_src"
mv src/ src_back/ &&

echo "luacompile for encryption加密lua代码"
cocos luacompile -v -s ./src_back -d ./src -e -k brian -b brian &&

echo "build resource-md5-list file"
./build_md5.py -v $RESOURCE_VERSION -c &&

echo "clear project"
rm -rf $ANDROID_PROJECT_HOME/bin/
rm -rf $ANDROID_PROJECT_HOME/gen/
rm -rf $ANDROID_PROJECT_HOME/assets/
rm -rf release/
echo "clear project finished"

echo "compile project"
cocos compile -p android &&

#打包热更新文件
echo "create update package for 'Hot-Update Server'"
mkdir release/
mkdir -p update_pak/gamecenter
cp -r src/ update_pak/gamecenter
cp -r res/ update_pak/gamecenter
cp project.manifest update_pak/
tar -zcf release/update_pak.tar.gz update_pak/
rm -rf update_pak/
echo "create update package finished"

#ant打包签名
echo "sign the apk to release"
ant release -buildfile $ANDROID_PROJECT_HOME/build.xml &&
cp $ANDROID_PROJECT_HOME/libs/armeabi/libcocos2dcpp_gamecenter.so release/
cp $ANDROID_PROJECT_HOME/bin/GameCenter-release.apk release/GameCenter.apk

#生成渠道包
echo "get the original market name"
market_bak=$(cat $ANDROID_PROJECT_HOME/AndroidManifest.xml | grep UMENG_CHANNEL)
market_bak=${market_bak#*value=\"}
market_bak=${market_bak%%\"*}
for market in $MARKETS
do
    echo "packaging GameCenter_${market}.apk ..."
    #sed -i "s#\(android:value=\)\"\(.*\)\"\( android:name=\"UMENG_CHANNEL\"\)#\1\"$market\"\3#" $ANDROID_PROJECT_HOME/AndroidManifest.xml
    sed -i "s#\(android:name=\"UMENG_CHANNEL\" android:value=\)\"\(.*\)\"#\1\"$market\"#" $ANDROID_PROJECT_HOME/AndroidManifest.xml
    echo $(cat $ANDROID_PROJECT_HOME/AndroidManifest.xml | grep UMENG_CHANNEL)
    ant release -buildfile $ANDROID_PROJECT_HOME/build.xml &&
    cp $ANDROID_PROJECT_HOME/bin/GameCenter-release.apk release/GameCenter_$market.apk
done
echo "recover the original market name 还原默认渠道包"
#sed -i "s#\(android:value=\)\"\(.*\)\"\( android:name=\"UMENG_CHANNEL\"\)#\1\"$market_bak\"\3#" $ANDROID_PROJECT_HOME/AndroidManifest.xml
sed -i "s#\(android:name=\"UMENG_CHANNEL\" android:value=\)\"\(.*\)\"#\1\"$market_bak\"#" $ANDROID_PROJECT_HOME/AndroidManifest.xml

echo "make apk without .so"
rm $ANDROID_PROJECT_HOME/libs/armeabi/libcocos2dcpp_gamecenter.so
ant release -buildfile $ANDROID_PROJECT_HOME/build.xml &&
mv $ANDROID_PROJECT_HOME/bin/GameCenter-release.apk release/
echo "make apk finished"

echo "recovery the workspace"
rm -rf src/
mv src_back/ src/
rm project.manifest
sed -i "s#DEBUG = 1#DEBUG = 2#" ./src/config.lua
sed -i "s#CC_SHOW_FPS = false#CC_SHOW_FPS = true#" ./src/config.lua
sed -i "s#isDebug = false#isDebug = true#" $ANDROID_PROJECT_HOME/src/com/brian/gamecenter/App.java
echo "recovery finished"

# install apk
adb install -r ./release/GameCenter.apk

你可能感兴趣的:(Android小知识)