Android Gradle 批量修改生成的apk文件名

一、简介

平时开发都知道,我们要上线的时候需要在Android studio打包apk文件,可是默认的打包名是app-release.apk或者app-debug.apk这样的名字,太没有辨识度了。

下面我们就来看看Android Studio是怎样来批量修改生成的apk文件名的。

二、代码实现

1、 Gradle 3.0以下版本

在app的build.gradle文件添加如下代码:

android {
	...

	//签名
    signingConfigs{
        release{
            storeFile file("xxxx.jks")
            storePassword '123456'
            keyAlias 'xxxx'
            keyPassword '123456'
        }
        debug{
            storeFile file("xxxx.jks")
            storePassword '123456'
            keyAlias 'xxxx'
            keyPassword '123456'
        }
    }
    
	productFlavors{
        google{
        	manifestPlaceholders = [
                    SDKChannel: "google",
                    app_name : "@string/app_name"
            ]
        }
        baidu{
        	manifestPlaceholders = [
                    SDKChannel: "baidu",
                    app_name : "@string/app_name"
            ]
        }
    }

	buildTypes {
        release {
            signingConfig signingConfigs.release
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //整理优化apk文件的工具,它能提高系统和应用的运行效率,更快的读写apk中的资源,降低内存的使用
            zipAlignEnabled true
        }
        debug{
            signingConfig signingConfigs.debug
        }
    }

	applicationVariants.all { variant ->
        variant.outputs.each{ output ->
            if(output.outputFile != null
                   && output.outputFile.name.endsWith('.apk')){
                def apkFile = new File(
                        output.outputFile.getParent(),
                        "SDK_${variant.flavorName}_${variant.buildType.name}_V${variant.versionName}_${buildTime()}.apk"
                )
                output.outputFile = apkFile
            }
        }
    }
}

def buildTime(){
    def date = new Date()
    def formattedDate = date.format('yyyyMMddHHmm')
    return formattedDate
}

2、Gradle 3.0以上版本

android {
	...

	//签名
    signingConfigs{
        release{
            storeFile file("xxxx.jks")
            storePassword '123456'
            keyAlias 'xxxx'
            keyPassword '123456'
        }
        debug{
            storeFile file("xxxx.jks")
            storePassword '123456'
            keyAlias 'xxxx'
            keyPassword '123456'
        }
    }
    
	productFlavors{
        google{
        	manifestPlaceholders = [
                    SDKChannel: "google",
                    app_name : "@string/app_name"
            ]
        }
        baidu{
        	manifestPlaceholders = [
                    SDKChannel: "baidu",
                    app_name : "@string/app_name"
            ]
        }
    }

	buildTypes {
        release {
            signingConfig signingConfigs.release
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //整理优化apk文件的工具,它能提高系统和应用的运行效率,更快的读写apk中的资源,降低内存的使用
            zipAlignEnabled true
        }
        debug{
            signingConfig signingConfigs.debug
        }
    }

	android.applicationVariants.all {variant ->
        variant.outputs.all {
            //在这里修改apk文件名,引号内的字符串都可以随便定义
            outputFileName = "SDK_${variant.flavorName}_${variant.buildType.name}_V${variant.versionName}_${buildTime()}.apk"
        }
	}
}

def buildTime(){
    def date = new Date()
    def formattedDate = date.format('yyyyMMddHHmm')
    return formattedDate
}

说明:

  • {variant.flavorName}:渠道名称;

  • {variant.buildType.name}:release或debug包;

  • {variant.versionName}:版本名称;

  • {buildTime()}:当前时间;

打包结果如下:
在这里插入图片描述

发布了100 篇原创文章 · 获赞 45 · 访问量 64万+

猜你喜欢

转载自blog.csdn.net/wangzhongshun/article/details/104847176