本文将带你了解Android应用开发[build.gradle配置系列(一)]android studio根据版本号动态生成apk名,希望本文对大家学Android有所帮助。
<
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,"....");}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!