Gradle

基础设置

1.自定义打包版本号arrow-up-right

格式:xxxx_版本号_debug.apk (版本号添加时间戳) 示例:APK名称_6.0.5.20171113161821_debug.apk

apply plugin: 'com.android.application'
//定义时间
def releaseTime() {
    return new Date().format("yyyyMMddHHmmss")
}
//设置发布的显示的版本号
def getVersionName(){
    return rootProject.ext.android.versionName
}

android{
//给apk添加对应的版本号:这里暂时注释但保留
//配置自定义打包名称
applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def outputFile = output.outputFile
        def fileName
        if (outputFile != null && outputFile.name.endsWith('.apk')) {
            if (variant.buildType.name.equals('release')) {
                variant.mergedFlavor.versionName = getVersionName()+"."+releaseTime()
                fileName = "APK名称_${variant.mergedFlavor.versionName}_release.apk"
            } else if (variant.buildType.name.equals('debug')) {
                variant.mergedFlavor.versionName = getVersionName()+"."+releaseTime()
                fileName = "APK名称_${variant.mergedFlavor.versionName}_debug.apk"
            }
            output.outputFile = new File(outputFile.parent, fileName)
        }
    }
}
}

2.版本号自增长arrow-up-right

自增长设置之后带来的一个问题:项目编译的时候发生异常: The APK file does not exist on diskarrow-up-right

问题原因:在修改apk后缀名时发现当设置版本号时间标识在秒级别时,最初使用defaultConfig.versionName来配置apk后缀,发现运行会提示*.apk does not exist on disk. 原因是在编译和安装时调用了两次buildTime(),使得得到的apk路径不同。在AnroidStudio上选择运行会执行build和install,两者的时间标签不一致导致无法找到apk文件

解决方案一:时间戳格式精确到分,不要精确到秒;

解决方案二https://stackoverflow.com/questions/34039834/the-apk-file-does-not-exist-on-diskarrow-up-right

3.多渠道打包arrow-up-right

在AndroidManifest中加入占位符

在module的build.gradle中加入

4.APK自动签名

5.Gradle语法

  • 声明变量:

  • 日志打印:

  • 读取属性配置文件:

6.Gradle编译加速配置

Last updated

Was this helpful?