Android Gradle bulk modify apk file name generation

I. Introduction

Usually develop all know that we need to be on-line package apk file in Android studio, but the default package name or app-release.apk names like app-debug.apk, so there is no recognizable of.

Here we take a look at how Android Studio bulk edit the apk file names generated.

Second, code implementation

1, Gradle 3.0 the following version

Add the following code in the app build.gradle file:

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 or later

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
}

Description:

  • {variant.flavorName}: Channel name;

  • {variant.buildType.name}: Release or debug packets;

  • {variant.versionName}: Version name;

  • {buildTime()}:current time;

Packaging results are as follows:
Here Insert Picture Description

Published 100 original articles · won praise 45 · views 640 000 +

Guess you like

Origin blog.csdn.net/wangzhongshun/article/details/104847176