4

Is it possible to specify the filename of the generated *.apk through gradle? So that I automatically could get MyApp-1.0.0.apk instead of the default app-release.apk.

reVerse
  • 35,075
  • 22
  • 89
  • 84
just_user
  • 11,769
  • 19
  • 90
  • 135

2 Answers2

4

You can do this by adding the following line to your build.gradle file inside the android{...} part:

android.applicationVariants.all { variant ->
    variant.outputFile = file("$project.buildDir/${defaultConfig.versionName}-${defaultConfig.versionCode}.apk")
}

Notes:

  1. Everything inside the file(...) is arbitrary, of course
  2. This code will output the *.apk to your project/module/build directory, no matter what path you've specified when Building the Signed APK.
  3. The output-file will take the Name and Version-Code from your build.gradle file.
  4. Android Studio may complain that it "cannot resolve symbol applicationVariants/defaultConfig" - ignore it. ;)
reVerse
  • 35,075
  • 22
  • 89
  • 84
1

Got it working! Used this answer

android {
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File(
                output.outputFile.parent,
                output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
        }
    }
}
Community
  • 1
  • 1
Mike Rapadas
  • 4,613
  • 2
  • 28
  • 21