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

  • 内容
  • 评论
  • 相关

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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之间可以使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)

 

 

评论

9条评论

发表评论