版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yin1031468524/article/details/79763114
1、 在build.gradle定义函数,根据时间动态返回时间标签
def static releaseTime() {
return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}
2、根据时间生成versionname
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.jason.log.text"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0".concat("_").concat(releaseTime())//版本号加上时间
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
3、在android标签中的buildTypes配置动态生成apk名
android {
...
buildTypes {
debug {
minifyEnabled false
buildConfigField "boolean", "LOG_DEBUG", "false" //定义变量,区分debug 或release 状态,可直接在代码中BuildConfig.LOG_DEBUG使用,便于添加调试log
}
release {
//签名
minifyEnabled false
buildConfigField "boolean", "LOG_DEBUG", "true"
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def type = ""
if (variant.buildType.name == 'debug') {
type = "_debug"
}
def fileName = "LogTest_V${defaultConfig.versionName}${type}.apk" //定义apk名
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
另外,我们可以在debug或release 标签中定义一个变量,用于控制是否输出调试log,如下:
BuildConfig.LOG_DEBUG在debug状态为true,release 状态下为false
if(BuildConfig.LOG_DEBUG){
Log.d(TAG,"....");
}