Android 打包修改名称

586 阅读1分钟

为什么要修改名称

  1. 直接打包的名称都是 app-release.apk/app-debug.apk
  2. 这些都不是我们想要的名称 我们需要[项目名][时间][版本号][正式版/测试版].apk

我们要怎么做

  1. 当前的gradle 是3.0以下的打包方法 在app下的build.gradle里
plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 31
    buildToolsVersion "31.0.0"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 23
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    
    // 打包 用来打包修改名字的 跟defaultConfig是同一级的
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            def fileName;
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                File outputDirectory = new File(outputFile.parent);
                if (variant.buildType.name.equals('release')) {
                    //[项目名][时间][版本号][正式版/测试版].apk
                    fileName = "[项目名]${releaseTime()}_${defaultConfig.versionName}_release.apk"
                } else if (variant.buildType.name.equals('debug')) {
                    //[项目名][时间][版本号][正式版/测试版].apk
                    fileName = "[项目名]${releaseTime()}_${defaultConfig.versionName}_debug.apk"
                }
                output.outputFile = new File(outputDirectory, fileName)
            }
        }
    }

}
//打包 打包版本需要的当前时间名字 跟 Android 是同一级的
def releaseTime() {
    //注意时间不对 请注意时区问题 时区在小时分钟的时候不一样
    return new Date().format("yyyy-MM-dd-HH-mm", TimeZone.getTimeZone("Asia/Shanghai"))
}


dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
  1. 当前的gradle 是3.0以上的打包方法 在app下的build.gradle里
plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 31
    buildToolsVersion "31.0.0"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 23
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    
    // 打包 用来打包修改名字的 跟defaultConfig是同一级的
    applicationVariants.all { variant ->
        variant.outputs.all { output -> // 这里和2.0的不一样 2.0是each 3.0是all
            def outputFile = output.outputFile
            def fileName;
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                if (variant.buildType.name.equals('release')) {
                    fileName = "[项目名]${releaseTime()}_${defaultConfig.versionName}_release.apk"
                } else if (variant.buildType.name.equals('debug')) {
                    fileName = "[项目名]${releaseTime()}_${defaultConfig.versionName}_debug.apk"
                }
                outputFileName = fileName // 这里和2.0不一样
            }
        }
    }

}
//打包 打包版本需要的当前时间名字 跟 Android 是同一级的
def releaseTime() {
    //注意时间不对 请注意时区问题 时区在小时分钟的时候不一样
    return new Date().format("yyyy-MM-dd-HH-mm", TimeZone.getTimeZone("Asia/Shanghai"))
}


dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}