Gradle

基础设置

1.自定义打包版本号

格式: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.版本号自增长

//读取版本号
def getVersionCode() {
    def versionFile = file('version.properties')
    if (versionFile.canRead()) {
        Properties versionProps = new Properties()
        versionProps.load(new FileInputStream(versionFile))
        def versionCode = versionProps['versionCode'].toInteger()
        def runTasks = gradle.startParameter.taskNames        //仅在assembleRelease任务是增加版本号
        println 'runTasks:'+runTasks
        if(':WeiChat:assembleBaiduDebug'in runTasks){
            println 'runTasks:'+'debug模式打包apk'
        }
        if(':WeiChat:assembleBaiduRelease'in runTasks){
            println 'runTasks:'+'发布模式打包apk'
            versionProps['versionCode'] = (++versionCode).toString()
            versionProps.store(versionFile.newWriter(), null)
        }
        return versionCode
    } else {
        throw new GradleException("Could not find version.properties!")
    }
}
//声明变量
def currentVersionCode = getVersionCode()
//版本自增长
android{

    defaultConfig {
        applicationId project.applicationId
        minSdkVersion rootProject.ext.android.minSdkVersion
        targetSdkVersion rootProject.ext.android.targetSdkVersion
       // versionCode rootProject.ext.android.versionCode
       //关键代码
        versionCode currentVersionCode
        versionName rootProject.ext.android.versionName
        multiDexEnabled true
        signingConfig signingConfigs.config
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
        ndk {
            abiFilters "armeabi", "armeabi-v7a", "x86", "mips", "x86_64", "mips64"
        }
    }
}

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

问题原因:在修改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-disk

3.多渠道打包

在AndroidManifest中加入占位符

<meta-data
          android:name="UMENG_CHANNEL"
          android:value="${UMENG_CHANNEL}"/>

在module的build.gradle中加入

android {

  defaultConfig {
      applicationId "xxxxxxxxx"
      minSdkVersion 15
      targetSdkVersion 23
      versionCode 1
      versionName "1.0"
      manifestPlaceholders = [UMENG_CHANNEL: "example"]//默认渠道
    }  //自动多渠道打包
  productFlavors {
     xiaomi {}
     _360 {}
     baidu {}
     wandoujia {}     //...添加其它渠道
   }

  productFlavors.all {
      flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL: name]
   }
}

4.APK自动签名

5.Gradle语法

实质就是Groovy语法
  • 声明变量:

  • 日志打印:

  • 读取属性配置文件:

6.Gradle编译加速配置

Last updated

Was this helpful?