1.生成的apk名加上当前时间 + 修改apk的发布路径

在build.gradle配置文件下的android配置段下的buildTypes下找到你的编译配置项一般就是release
在release段下面加上如下代码


applicationVariants.all { variant ->
   if (variant.buildType.name.equals('release')) {  //如果是release版本
     variant.outputs.each { output -> def outputFile = output.outputFile
     if (outputFile != null && outputFile.name.endsWith('.apk')) {  //查找所有的apk
        def fileName = "${releaseTime()}_XXXXPorject_${defaultConfig.versionName}.apk"  //重新定义apk的名称
        output.outputFile = new File(outputFile.parent, fileName)  //outputFile.parent参数可以改成你你想要的发布路径
     }
   }
  }
}

然后在build.gradle配置文件的末尾加上一个方法用来获取当前时间


def releaseTime() {
   // return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC")) //年月日
    return new Date().format("yyyyMMdd HH-mm-ss", TimeZone.getTimeZone("GMT+8:00")) //年月日时分秒
}

2.实现自动化签名 别忘了jks文件也要放项目里

build.gradle的android段添加如下配置段 定义签名key


  signingConfigs {//签名的配置
   release {
      storeFile file("签名.jks")
      storePassword '密码'
      keyAlias '别名'
      keyPassword '密码'
   }
}

在android配置段下的buildTypes段的release段下添加一行 表示调用上面的签名配置
注意修改apk的名字后在android studio是无法开启调试模式,提示找不到apk

signingConfig signingConfigs.release

效果图

第一个是debug版本
第二个是没签名的release版本
第三个是签名后还改了名字的release版本
如果想编译某个版本的话 需要修改下执行gradle的命令行参数

参考例子

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '24.0.3'
    defaultConfig {
        applicationId "com.coderstory.Purify"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 90
        versionName "1.5.0"
        resConfigs "cn"
    }
    repositories {
        mavenCentral()
    }

    signingConfigs {//签名的配置
        release {
            storeFile file("mykey.jks")
            storePassword 'a1234'
            keyAlias 'coolapk'
            keyPassword 'b1234'
        }
    }


    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                if (variant.buildType.name.equals('release')) {  //如果是release版本
                    variant.outputs.each { output -> def outputFile = output.outputFile
                        if (outputFile != null && outputFile.name.endsWith('.apk')) {  //查找所有的apk
                            def fileName = "MIUI Purify_${releaseTime()}_${defaultConfig.versionName}.apk"  //重新定义apk的名称
                            output.outputFile = new File(outputFile.parent, fileName)  //outputFile.parent参数可以改成你你想要的发布路径
                        }
                    }
                }
            }
        }



    }
    productFlavors {
    }
     lintOptions {
        abortOnError false
    }


}

dependencies {
    provided fileTree(include: ['*.jar'], dir: 'libs')
    compile project(':library')
    compile project(':pull')
    compile 'com.android.support:support-v4:25.0.1'
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.android.support:design:25.0.1'
    testCompile 'junit:junit:4.12'
    provided 'de.robv.android.xposed:api:82'
}

def releaseTime() {
  // return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC")) //年月日
    return new Date().format("yyyyMMdd HH-mm-ss", TimeZone.getTimeZone("GMT+8:00")) //年月日时分秒
}


届ける言葉を今は育ててる
最后更新于 2017-10-13