Tom McFarlin
Tom McFarlin

Reputation: 829

Change name of apk depending on buildType

I'd like to change the name of my output .apk files. However, I want to have "-debug" or "-release" appended depending on the build type.

Here are the output names I want: MyApp-0.0.1-debug.apk MyApp-0.0.1-release.apk

I'm unfamiliar with Gradle and haven't found how to do this, I know I just need to access the buildType within the following code but can't find how to do this.

Currently my output is "MyApp-0.0.1.apk" regardless of buildType. How can I change the code below to alter this?

android.libraryVariants.all { variant ->
variant.outputs.each { output ->
    def outputFile = output.outputFile
    if (outputFile != null && outputFile.name.endsWith('.aar')) {

        def fileName
        def bType = ""
        // bType = "-" + something.buildType

        fileName = "${archivesBaseName}-${project.version}${bType}.aar"
        output.outputFile = new File(outputFile.parent, fileName)
    }
}
}

Upvotes: 0

Views: 1722

Answers (2)

Panos Gr
Panos Gr

Reputation: 677

For anyone arriving here late, this version works for me in April 2020.

Just place this code inside your buildTypes element ( android or defaultConfig work just as well).

applicationVariants.all { variant ->
      def customBuildName = ""
      if(variant.buildType.name == "release") customBuildName = "release-name.apk"
      else customBuildName = "build-name.apk"
      variant.outputs.all {
          outputFileName = customBuildName
      println("name of ${variant.buildType.name} build is ${outputFileName}")
    }
}

Upvotes: 1

Scott Barta
Scott Barta

Reputation: 80030

The build type is part of the variant; you're iterating over all the variants in your loop. You can get the build type name via:

variant.buildType.name

You can find (partially complete) API docs at http://tools.android.com/tech-docs/new-build-system/user-guide

Upvotes: 4

Related Questions