温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android打包版本号设置方法

发布时间:2020-10-03 09:41:17 来源:脚本之家 阅读:170 作者:Shengjie 栏目:移动开发

之前没有设置过打包的命名,每次打包都是默认的"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打包版本号设置方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI