HBuilderX离线打包,Android的build.gradle配置,build_android.sh命令脚本

一,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

 

你可能感兴趣的:(shell脚本)