#!/bin/bash
# 游戏APK名字,注意不带后缀
gameApk="五寨分院"
#icon名称
icon_name="icon.png"
#res路径长度
apk_length=`expr ${#gameApk} + 5`
#icon长度
icon_length=${#icon_name}
# ---签名信息---
keystoreFile="csgame.jks"
keyName="csgame"
keyPassword="123456"
# 最终打包生成的发布目录
apkReleaseDir="release"
#资源路径
respath="./res"
# ---APK命名信息---
# APK最终包的名字
apkReleaseName=${gameApk}-${channelName}-signed.apk
# APK未签名的名字
apkUnsignName=${gameApk}-unsigned.apk
# ---需要复制的资源文件名,一般不需要改---
# 创建目录
createDir(){
if [[ ! -d $1 ]]; then
mkdir $1
fi
}
# 反编译APK
decompileApk(){
apktool d -s -f $1
}
# 生成APK
buildApk(){
apktool b $1
}
# 签名
apkSigner(){
jarsigner -verbose -keystore $1 -storepass $3 -signedjar $4 $5 $2 -digestalg SHA1 -sigalg MD5withRSA
}
# 安装
installApk(){
adb install -r $1
}
# ---------METHOD END------------
# ----------CMD-----------
decompileApk "./${gameApk}.apk"
#遍历res文件夹下的所有文件夹
for dir in $gameApk/res/*
do
dirpath=$dir
drawable=${dirpath:$apk_length:8}
mipmap=${dirpath:$apk_length:6}
if [ $drawable = 'drawable' -o $mipmap = 'mipmap' ]
then
for file in $dir/*
do
#截取apk包的icon名
file_length=${#file}
from_num=`expr $file_length - $icon_length`
file_path=$file
file_tag=${file_path:$from_num:$icon_length}
#判断匹配icon名进行替换
if [ $file_tag = $icon_name ]
then
echo "替换成功"
cp ./$icon_name $file
fi
done
fi
done
buildApk "./$gameApk"
cp ./$gameApk/dist/${gameApk}.apk ./$apkUnsignName
apkSigner "$keystoreFile" "$keyName" "$keyPassword" "$apkReleaseName" "$apkUnsignName"
createDir "$apkReleaseDir"
mv -f ./$apkReleaseName ./$apkReleaseDir
echo "build success"
echo "start install apk. waiting..."
installApk "./${apkReleaseDir}/${apkReleaseName}"
rm -rf ./$apkUnsignName
rm -rf ./$gameApk