魅力程序猿

  • 首页
  • Java
  • Android
  • APP
    • 扑克计分器
    • Video Wallpaper
  • 联系我
  • 关于我
  • 资助
道子
向阳而生
  1. 首页
  2. Android
  3. 正文

对Android Studio 生成的包自动重命名

2016年1月24日 12852点热度 0人点赞 9条评论

在使用Android  Studio打包的时候,每次生成的包都是app-release.apk,app-debug.apk或者是项目名称-release.apk,项目名称--debug.apk。这样,每次都要手动再修改成自已需要的包名,是不是很麻烦呢。那有没有一种打包出来就是自己想要的名称呢,肯定的回答有,那么强的的Google怎么会不实现这个功能呢。

下面就是在项目的build.gradle中写几行代码就可以了。

def getBuildTime() {
    return new Date().format("yyyy-MM-dd_HH-mm-ss", TimeZone.getTimeZone("GMT+8:00"))
}

android {
    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion //16
        targetSdkVersion rootProject.ext.targetSdkVersion //27
        applicationId "com.aoaoyi.poker"
        multiDexEnabled true
        versionCode 7000700
        versionName "7.0.7"
        flavorDimensions "7.0.0"

        resValue "string", "build_time", getBuildTime()
    }
    
    applicationVariants.all { variant ->
        def isRelease = variant.buildType.name.equalsIgnoreCase('release')
        if (isRelease) {
            variant.outputs.all { output ->
                def releaseApkName = "aoaoyi-v${defaultConfig.versionName}-" + variant.flavorName.replace('_', '') + "-${getBuildTime()}" + '.apk'
                outputFileName = releaseApkName
            }
        } else {
            variant.outputs.all { output ->
                def debugApkName = "aoaoyi-v${defaultConfig.versionName}-" + variant.flavorName.replace('_', '') + '-debug.apk'
                outputFileName = debugApkName
            }
        }
    }
}

代码的的“aoaoyi”换成你想输出的项目名称就好了,你可以试试,是不是很简单。

注,下面是Gradle3.0之间可以使用。

buildTypes {
    release {
        minifyEnabled true
        signingConfig  signingConfigs.myConfig
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace("app-release.apk",
 "xiaoyu" + versionName + ".apk"))
            }
        }
    }

    debug {
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace("app-debug.apk",
 "xiaoyu-debug" + versionName + ".apk"))
            }
        }
    }
}

 

推荐阅读: Android面试题16–代码混淆(Proguard)

 

 

标签: AndroidStudio包重命名
最后更新:2016年1月24日

daozi

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

  • zz

    可以

    2016年12月2日
    回复
    • Daveigh

      @zz What a pleuasre to find someone who thinks through the issues

      2016年12月19日
      回复
  • Starly

    Thanks guys, I just about lost it lonkiog for this.

    2016年12月19日
    回复
  • sneakyquota7189.jimdo.com

    I visited multiple websites however the audio feature for audio songs existing at this
    web page is really excellent.

    2017年5月27日
    回复
  • teen patti gold free

    I enjoying, will read more. Thanks!

    2017年9月26日
    回复
  • razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
    取消回复
    搜索
    联系方式

    QQ群:179730949
    QQ群:114559024
    欢迎您加入Android大家庭
    本人QQ:136049925

    赐我一丝安慰
    给我一点鼓励

    COPYRIGHT © 2023 魅力程序猿. ALL RIGHTS RESERVED.

    Theme Kratos Made By Seaton Jiang

    豫ICP备15000477号