-1

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)
                )
            }
        }
    }
}
elixenide
  • 44,308
  • 16
  • 74
  • 100
Hex Hex Hex
  • 68
  • 1
  • 6

1 Answers1

1

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"
shizhen
  • 12,251
  • 9
  • 52
  • 88
  • Great solution, unfortunately the installation fails due to missing the `app-debug.apk` file in `app\build\outputs\debug\app-debug.apk`. Do you have know how this can be fixed? The **same behaviour** would it be with the `app-release.apk` file. Thank you very much! – BWappsAndmore Jan 24 '20 at 16:29