Android Stdio 如何自定义生成APK的名称

时间:2022-07-22 05:23:23

    Android Stdio自动默认生成的app的名称都是app-release或者app-debug,生成完后还要手动更改apk的名称,很是麻烦。

   自定义生成APK的名称的方法:在\app\build.gradle这个文件里添加如下内容

apply plugin: 'com.android.application'

def getTime() {
    return new Date().format("yyyyMMdd", TimeZone.getDefault());
}

android {
    compileSdkVersion 22
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    // 打包后应用名称
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            def fileName
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                if (variant.buildType.name.equals('release')) {
                    fileName = "LoverHeart_Release${defaultConfig.versionName}.${getTime()}.apk"
                } else if (variant.buildType.name.equals('debug')) {
                    fileName = "LoverHeart_Debug${defaultConfig.versionName}.${getTime()}.apk"
                }
                output.outputFile = new File(outputFile.parent, fileName)
            }

        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:design:22.2.1'
}

Android Stdio 如何自定义生成APK的名称

 

生成的apk包名为:LoverHeart_Release1.0.20170417.apk