#!/usr/bin/sh
# @Author:agui
# 本文件的主要作用是快速打 debug 包
# 然后安装到设备上 再启动
echo 'We will start'
echo "This sh file is run for Release !!"
debugMode=""
currentVersion=""
currentVersionCode=""
#打印错误
printError()
{
errorWord=$1
echo ""
echo ""
echo "\033[31m $errorWord \033[0m"
echo ""
echo ""
exit 110
}
#传入参数 文件名
#返回值 0,合法;其他值非法或出错
function check_syntax()
{
if [ ! -f $1 ];then
return 1
fi
ret=$(awk -F= 'BEGIN{valid=1}
{
#已经找到非法行,则一直略过处理
if(valid == 0) next
#忽略空行
if(length($0) == 0) next
#消除所有的空格
gsub(" |\t","",$0)
#检测是否是注释行
head_char=substr($0,1,1)
if (head_char != "#"){
#不是字段=值 形式的检测是否是块名
if( NF == 1){
b=substr($0,1,1)
len=length($0)
e=substr($0,len,1)
if (b != "[" || e != "]"){
valid=0
}
}else if( NF == 2){
#检测字段=值 的字段开头是否是[
b=substr($0,1,1)
if (b == "["){
valid=0
}
}else{
#存在多个=号分割的都非法
valid=0
}
}
}
END{print valid}' $1)
if [ $ret -eq 1 ];then
return 0
else
return 2
fi
}
#参数1 文件名
#参数2 块名
#参数3 字段名
#返回0,表示正确,且能输出字符串表示找到对应字段的值
#否则其他情况都表示未找到对应的字段或者是出错
function get_field_value()
{
if [ ! -f $1 ] || [ $# -ne 3 ];then
return 1
fi
blockname=$2
fieldname=$3
begin_block=0
end_block=0
cat $1 | while read line
do
if [ "X$line" = "X[$blockname]" ];then
begin_block=1
continue
fi
if [ $begin_block -eq 1 ];then
end_block=$(echo $line | awk 'BEGIN{ret=0} /^\[.*\]$/{ret=1} END{print ret}')
if [ $end_block -eq 1 ];then
#echo "end block"
break
fi
need_ignore=$(echo $line | awk 'BEGIN{ret=0} /^#/{ret=1} /^$/{ret=1} END{print ret}')
if [ $need_ignore -eq 1 ];then
#echo "ignored line:" $line
continue
fi
field=$(echo $line | awk -F= '{gsub(" |\t","",$1); print $1}')
value=$(echo $line | awk -F= '{gsub(" |\t","",$2); print $2}')
#echo "'$field':'$value'"
if [ "X$fieldname" = "X$field" ];then
#echo "result value:'$result'"
echo $value
break
fi
fi
done
return 0
}
##########
#### 编译执行所有渠道
#参数1 文件名
#参数2 渠道名
#返回0,表示正确
#否则其他情况都表示失败
function buildChannel()
{
if [ ! -f $1 ] || [ $# -ne 2 ];then
return 1
fi
initFileName=$1
channelName=$2
#echo $initFileName
#echo $channelName
apkPreName=$(get_field_value $initFileName $channelName apkPreName)
packageName=$(get_field_value $initFileName $channelName packageName)
wiiPayKey=$(get_field_value $initFileName $channelName wiiPayKey)
WIIPAY_CHANNEL_CODE=$(get_field_value $initFileName $channelName WIIPAY_CHANNEL_CODE)
PAY_CHANNEL_ID=$(get_field_value $initFileName $channelName PAY_CHANNEL_ID)
appInstalledShowName=$(get_field_value $initFileName $channelName appInstalledShowName)
echo "initFileName="$initFileName
echo "apkPreName="$apkPreName
echo "packageName="$packageName
echo "channelName="$channelName
echo "wiiPayKey"=$wiiPayKey
echo "WIIPAY_CHANNEL_CODE="$WIIPAY_CHANNEL_CODE
echo "appInstalledShowName="$appInstalledShowName
echo "PAY_CHANNEL_ID="$PAY_CHANNEL_ID
#echo "$outputpath,$apkPreName,$packageName,$wiiPayKey"
echo "当前加载渠道$channelName"
echo "----apkPreName=$apkPreName-----packageName=$packageName-----wiiPayKey=$wiiPayKey----PAY_CHANNEL_ID=$PAY_CHANNEL_ID----"
if [ -z $apkPreName ] || [ -z $packageName ] || [ -z $PAY_CHANNEL_ID ] || [ -z $wiiPayKey ]; then
echo "Please check your ini file , lose value for or apkPreName or packageName or wiiPayKey "
printError "1.检查渠道名是否和渠道列表一致。2.检查是否都填写了 apkPreName packageName wiiPayKey PAY_CHANNEL_ID"
return 1
fi
if [ -z $WIIPAY_CHANNEL_CODE ]; then
echo "Please check your ini file , lose value for WIIPAY_CHANNEL_CODE "
printError "请检查是否有 WIIPAY_CHANNEL_CODE 参数"
return 1
fi
if [ -z $appInstalledShowName ]; then
echo "Please check your ini file , lose value for appInstalledShowName "
printError "请检查是否有 appInstalledShowName 参数"
return 1
fi
# 修改 App memfest 文件
# 获取 ini 文件的路径,然后根据这个路径去构造 output 路径和 manifest 文件路径
projectPath=${initFileName%/*}
echo "projectPath=$projectPath"
appManifestFilePath="$projectPath/AndroidManifest.xml"
#获取 manifest 文件的版本号
currentVersion=$(sed -ne 's/\(.*\)android:versionName="\(.*\)"\(.*\)/\2/p' $appManifestFilePath)
echo "currentVersion=$currentVersion"
currentVersionCode=$(sed -ne 's/\(.*\)android:versionCode="\(.*\)"\(.*\)/\2/p' $appManifestFilePath)
echo currentVersionCode=$currentVersionCode
currentDate=$(date +%Y%m%d)
echo currentDate=$currentDate
output_package_file_path="$fullOutPutPath/${apkPreName}_${channelName}_v${currentVersion}_c${currentVersionCode}_${currentDate}.apk"
echo $output_package_file_path
# 一系列替换
# 注意 括号也要带上转义符号
# 替换渠道
sed -e "s/android:name=\"UMENG_CHANNEL\" android:value=\"\(.*\)\"/android:name=\"UMENG_CHANNEL\" android:value=\"$channelName\"/g"\
-e "s/package=\"\(.*\)\"/package=\"$packageName\"/g"\
-e "s/android:value=\"\(.*\)\" android:name=\"WIIPAY_APP_ID\"/android:value=\"$wiiPayKey\" android:name=\"WIIPAY_APP_ID\"/g"\
-e "s/android:name=\"WIIPAY_APP_ID\" android:value=\"\(.*\)\"/android:name=\"WIIPAY_APP_ID\" android:value=\"$wiiPayKey\"/g"\
-e "s/android:debuggable=\"true\"/android:debuggable=\"false\"/g"\
-e "s/android:value=\"\(.*\)\" android:name=\"WIIPAY_CHANNEL_CODE\"/android:value=\"$WIIPAY_CHANNEL_CODE\" android:name=\"WIIPAY_CHANNEL_CODE\"/g"\
-e "s/android:name=\"WIIPAY_CHANNEL_CODE\" android:value=\"\(.*\)\"/android:name=\"WIIPAY_CHANNEL_CODE\" android:value=\"$WIIPAY_CHANNEL_CODE\"/g"\
-e "s/android:name=\"PAY_CHANNEL_ID\" android:value=\"\(.*\)\"/android:name=\"PAY_CHANNEL_ID\" android:value=\"$PAY_CHANNEL_ID\"/g"\
$appManifestFilePath > "$appManifestFilePath.tmp"
#备份
cp $appManifestFilePath "$appManifestFilePath.bkup"
#覆盖
cp "$appManifestFilePath.tmp" $appManifestFilePath
rm "$appManifestFilePath.tmp"
#临时文件
buildtempEn="$projectPath/tmpbuild1"
buildtempZh="$projectPath/tmpbuild2"
# 替换应用名称
appNameFileValueEn="$projectPath/res/values/strings.xml"
sed -e "s/<string name=\"app_name\">\(.*\)<\/string>/<string name=\"app_name\">$appInstalledShowName<\/string>/g"\
$appNameFileValueEn > "$appNameFileValueEn.tmp"
appNameFileValueEnBkup
#备份
cp $appNameFileValueEn "$buildtempEn"
#覆盖
cp "$appNameFileValueEn.tmp" $appNameFileValueEn
rm "$appNameFileValueEn.tmp"
# zh
appNameFileValueZh="$projectPath/res/values-zh/strings.xml"
sed -e "s/<string name=\"app_name\">\(.*\)<\/string>/<string name=\"app_name\">$appInstalledShowName<\/string>/g"\
$appNameFileValueZh > "$appNameFileValueZh.tmp"
#备份
cp $appNameFileValueZh "$buildtempZh"
#覆盖
cp "$appNameFileValueZh.tmp" $appNameFileValueZh
rm "$appNameFileValueZh.tmp"
cat $appNameFileValueZh
#开始编译
# 变换完之后就可以编译了
buildXmlPath="$projectPath/build.xml"
cd $projectPath
echo "ant iniDeploy -Doutput_package_file_path=$output_package_file_path"
ant iniDeploy -Doutput_package_file_path=$output_package_file_path
isSuccess=$?
#恢复 applicationManifest.xml 文件
rm "$appManifestFilePath"
mv "$appManifestFilePath.bkup" $appManifestFilePath
rm "$appNameFileValueEn"
mv "$buildtempEn" $appNameFileValueEn
rm "$appNameFileValueZh"
mv "$buildtempZh" $appNameFileValueZh
echo "isSuccess=$isSuccess"
if [ $isSuccess != 0 ]; then
printError "编译失败,请查看错误提示"
fi
echo "打包成功!!!"
return 0
}
#获取.sh文件的路径
#shFilePath="noPathYet"
cd `dirname $0`
shFilePath=$(pwd)
echo $shFilePath
#cd -
# 从参数里读取配置文件名字(包含路径)
iniFilePath=$1
echo $iniFilePath
#调试模式,需要打开调试宏
debugMode=$2
echo "debugMode=$debugMode"
curprojectPath=${iniFilePath%/*}
if [ -z $iniFilePath ]; then
printError "请传入 ini 文件的路径,作为SH文件的第一个参数"
fi
# 检查 ini 文件格式是否正确
check_syntax $iniFilePath
checkResult=$?
echo "check syntax status:$checkResult"
if [[ checkResult == "0" ]]; then
#教验失败
printError "$iniFilePath 文件写法错误,请检查之!"
fi
echo "Checked:Init file's format is alright"
# 加载渠道
allChannels=$(get_field_value $iniFilePath channelList channelList)
echo "load all allChannels=$allChannels"
# 准备遍历所有渠道
# 把所有逗号都换成空格
allChannels=$(echo $allChannels | sed 's/,/ /g')
echo "space=$allChannels"
# 设置 #-DCOCOS2D_DEBUG
applicationMkFilePath="${iniFilePath%/*}/jni/Application.mk"
if [[ debugMode == "debug" ]]; then
echo "in debug mode. set DCOCOS2D_DEBUG"
sed "s/#-DCOCOS2D_DEBUG/-DCOCOS2D_DEBUG/g" $applicationMkFilePath > $applicationMkFilePath.tmp
sed "s/-DCOCOS2D_DEBUG/#-DCOCOS2D_DEBUG/g" $applicationMkFilePath.tmp > $applicationMkFilePath
else
echo "in release mode. close DCOCOS2D_DEBUG"
sed "s/#-DCOCOS2D_DEBUG/-DCOCOS2D_DEBUG/g" $applicationMkFilePath > $applicationMkFilePath.tmp
sed "s/-DCOCOS2D_DEBUG/#-DCOCOS2D_DEBUG/g" $applicationMkFilePath.tmp > $applicationMkFilePath
fi
#输出路径
outputpath=$(get_field_value $iniFilePath channelList outputpath)
if [ -z $outputpath ]; then
printError "请在channelList模块下配置 outputpath ,作为输出路径"
fi
fullOutPutPath="$curprojectPath/$outputpath"
echo $fullOutPutPath
rm -rf $fullOutPutPath
mkdir $fullOutPutPath
#先编译C代码
#申明 NDK 目录
NDK_ROOT="$curprojectPath/../../CommomProject/developTools/android-ndk-r9d"
export NDK_ROOT=$NDK_ROOT
echo $NDK_ROOT
if [ ! -d "$NDK_ROOT" ]; then
printError "ERROR !!!! NDK 文件夹不存在,请检查路径!!!!"
printError "NDK_ROOT=$NDK_ROOT"
exit
fi
######echo "$curprojectPath/build_native.sh"
######sh $curprojectPath/build_native.sh -j16
#编译 c++ 代码
#python $curprojectPath/build_native.py -b release
if [[ $? != 0 ]]; then
printError "编译错误,请查看编译详情"
fi
###### 检查android 工程 ##### 创建签名文件
#检查游戏工程,然后再检查commom工程
currentProjLocalPropertiesFile="$curprojectPath/local.properties"
if [ ! -d "$currentProjLocalPropertiesFile" ]; then
echo "第一次执行本脚本。脚本会自动为你的安卓工程更新 ant 等环境"
#android update
android update project -p "$curprojectPath"
android update project -p "$curprojectPath/../../CommomProject/proj.android"
#copy android key
cp -r "$curprojectPath/../../CommomProject/developTools/build_scripts/android_build_sh/android_key" "$curprojectPath/../"
#copy ant.properties
cp -f "$curprojectPath/../../CommomProject/developTools/build_scripts/android_build_sh/ant.properties" "$curprojectPath/"
fi
for channelName in $allChannels
do
# echo "channelName=$channelName"
buildChannel $iniFilePath $channelName
# if [[ $? != 0 ]]; then
# printError "生成 dex 以及之后的打包期间发生错误,请检查 android 工程以及编译配置文件. 其中,ant.properties 填写正确的证书名和密码;"
# fi
done