build.gradle

  • gradle 基本文件
  • Android自定义输出APK名称

gradle 基本文件

  • 修改
    项目build.gradle
	 // 版本号相关
	versionCode = 1
    versionName = "1.0.0"
    // 相关依赖库版本
    compileSdkVersion = 28
    buildToolsVersion = "29.0.0"
    minSdkVersion = 17
    targetSdkVersion = 28
    
    supportLibraryVersion = '27.1.1'
buildscript {
    repositories {
        google()
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        maven {
            url 'https://dl.bintray.com/cjt/maven'
        }
        google()

    }
}
  • 修改gradle文件夹下的配置
    distributionUrl=https://services.gradle.org/distributions/gradle-4.6-all.zip

Android自定义输出APK名称

两种写法
1 Android plugin 3.0 前

        applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    def fileName
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        // 输出apk名称为XXapp1.4.0_2017-02-14.apk
                        if (variant.buildType.name.equals('release')) {
                            fileName = "XXapp${defaultConfig.versionName}_${releaseTime()}.apk"
                        } else if (variant.buildType.name.equals('debug')) {
                            fileName = "XXapp${defaultConfig.versionName}_${releaseTime()}_debug.apk"
                        }
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }

2 3.0后

//配置自定义打包名称
    applicationVariants.all { variant ->
        variant.outputs.all {
            def fileName
            def date = new Date()
            def formattedDate = date.format('yyyyMMdd')
                if (variant.buildType.name.equals('release')) {
                    fileName = "${variant.mergedFlavor.versionName}_release_${formattedDate}.apk"
                } else if (variant.buildType.name.equals('debug')) {
                    fileName = "${variant.mergedFlavor.versionName}_debug_${formattedDate}.apk"
                }
                outputFileName = fileName;
        }
    }

Android plugin 3.0 建议使用
写法变化一:Use all() instead of each()
写法变化二:Use outputFileName instead of output.outputFile if you change only file name (that is your case)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值