一,build.gradle配置 1,簽名配置
signingConfigs {
config {
keyAlias 'sign'
keyPassword '123456'
storeFile file('/Users/gaoruishan/WebAndroid/sign.keystore')
storePassword '123456'
}
}
2,多渠道打包配置
productFlavors {
travel {
// 每個環境包名不同
applicationIdSuffix '.travel'
// 動態添加 string.xml 字段;
// 注意,這裏是添加,在 string.xml 不能有這個字段,會重名!!!
resValue "string", "app_name", "去哪兒"
// 動態修改 常量 字段
buildConfigField "String", "SPLASH", '"splash_travel"'
// 修改 AndroidManifest.xml 裏渠道變量
manifestPlaceholders = [BaiDu_Map_AK: "", app_icon: "@drawable/icon_travel"]
}
oldsland {
applicationIdSuffix '.oldsland'
resValue "string", "app_name", "小島"
buildConfigField "String", "SPLASH", '"splash_oldsland"'
manifestPlaceholders = [BaiDu_Map_AK: "Mfzm782DpGGKIRq2PH2LyPiSuxC3z9rD", app_icon: "@drawable/icon_oldsland"]
}
}
3,自定義APK輸出配置
applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 輸出apk名稱爲com.grs.xxx_v1.0.0_20171205_baidu.apk
def fileName = "${defaultConfig.applicationId}.${variant.productFlavors[0].name}_v${defaultConfig.versionName}_${getNowTime()}.apk"
outputFileName = fileName
}
}
}
//獲取時間戳
def getNowTime() {
def date = new Date()
def now = date.format('yyyyMMddHHmm')
return now
}
二,build_android.sh腳本配置
#!/usr/bin/env bash
#獲取當前腳本路徑
basedir=`cd $(dirname $0); pwd -P`
#=======================修改AndroidManifest.xml包名=========================
#臨時文件
temp=temp.txt
#文件目錄
rfile="$basedir/app/src/main/AndroidManifest.xml"
#指定內容寫入temp.txt
grep -Eo "package=\"com+[^\s]+[a-z]+\.+[a-z]*" $rfile > $temp
#讀取內容
tfile="$basedir/$temp"
olddata=$(cat $tfile)
echo "原來包名:$olddata"
#控制檯輸入
echo "請輸入包名:"
read input
#將某個文件中的jack字符串替換爲tom
newdata="package=\"$input"
s="s/$olddata/$newdata/g"
# s="s/oldsland/travel/g"
sed -i "" "$s" $rfile
# sed -i "s/oldsland/travel/g" `grep "oldsland" -rl ./`
#成功後刪除
rm -f $tfile
#=======================修改dcloud_control.xml包名=========================
temp2=temp2.txt
rfile2="$basedir/app/src/main/assets/data/dcloud_control.xml"
grep -Eo "appid=\"com+[^\s]+[a-z]+\.+[a-z]*" $rfile2 > $temp2
tfile2="$basedir/$temp2"
olddata2=$(cat $tfile2)
newdata2="appid=\"$input"
s2="s/$olddata2/$newdata2/g"
sed -i "" "$s2" $rfile2
rm -f $tfile2
#打包
echo "打包......"
gradle assembleRelease