Search code examples
javaandroidkotlinandroid-gradle-pluginapk

“app-release.apk” how to change this default generated apk name that allows to install after?


answers for this question are all bad, because then you can't install application by resulting apk " app-release.apk" how to change this default generated apk name

for example

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig getSigningConfig()
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def date = new Date();
                def formattedDate = date.format('yyyyMMddHHmmss')
                output.outputFile = new File(output.outputFile.parent,
                        output.outputFile.name.replace("-release", "-" + formattedDate)
//for Debug use output.outputFile = new File(output.outputFile.parent,
//                             output.outputFile.name.replace("-debug", "-" + formattedDate)
                )
            }
        }
    }
}

Solution

  • Use below code and put it at outside of the android{ } closure:

    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // Redirect your apks to new defined location to outputDirPath
            def outputDirPath = new File("${project.rootDir.absolutePath}/apks/${variant.flavorName}/${variant.buildType.name}")
            variant.packageApplicationProvider.get().outputDirectory = outputDirPath
    
            def apkFileName = "${rootProject.name}_${android.defaultConfig.versionName}.apk"
            output.outputFileName = apkFileName // directly assign the new name back to outputFileName
        }
    }
    

    Your final apk name will be something like app_1.0.0.apk, if you want to make the name be more fancy, you can modify below line per your requirement.

    def apkFileName = "${rootProject.name}_${android.defaultConfig.versionName}.apk"