首页 > 系统 > Android > 正文

Android打包版本号设置方法

2019-10-22 18:26:46
字体:
来源:转载
供稿:网友

之前没有设置过打包的命名,每次打包都是默认的"app-realease.apk",之后手动修改名字来显示出它是一个新版本。

 晚上学习了如何配置打包名称,很简单,修改build.gradle里的代码就行。

 详细记录如下:

1、打开app这个directory下的build.gradle

2、定义打包时间:

//时间def releaseTime() {  return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))}

3、自定义发布时的版本号(return的返回值可自行修改,例如1.0、2.0):

//版本号def getVersionName(){  return "2.0"}

4、自定义打包名称(代码中的XYZ可修改为app名字):

//名称  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()          fileName = "XYZ_${variant.mergedFlavor.versionName}_release.apk"        } else if (variant.buildType.name.equals('debug')) {          variant.mergedFlavor.versionName = getVersionName()+"."+releaseTime()          fileName = "XYZ_${variant.mergedFlavor.versionName}_debug.apk"        }        output.outputFile = new File(outputFile.parent, fileName)      }    }  }

5、build.gradle的完整代码:

apply plugin: 'com.android.application'//定义时间def releaseTime() {  return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))}//设置发布时的版本号def getVersionName(){  return "2.0"}android {  compileSdkVersion 26  buildToolsVersion "26.0.0"  defaultConfig {    applicationId "***"    minSdkVersion 14    targetSdkVersion 23    versionCode 1    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"  }  buildTypes {    release {      buildConfigField("boolean","API_DEBUG","false")      minifyEnabled false      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'    }    debug {      buildConfigField("boolean","API_DEBUG","true")      minifyEnabled false      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'    }  }  //配置打包名称  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()          fileName = "XYZ_${variant.mergedFlavor.versionName}_release.apk"        } else if (variant.buildType.name.equals('debug')) {          variant.mergedFlavor.versionName = getVersionName()+"."+releaseTime()          fileName = "XYZ_${variant.mergedFlavor.versionName}_debug.apk"        }        output.outputFile = new File(outputFile.parent, fileName)      }    }  }}dependencies {  compile fileTree(dir: 'libs', include: ['*.jar'])  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {    exclude group: 'com.android.support', module: 'support-annotations'  })}

总结

以上所述是小编给大家介绍的Android打包版本号设置方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表