19

How can I adapt my build.gradle to change the final basename of the apk(s) ? (without changing the default suffixes)

Jonik
  • 80,077
  • 70
  • 264
  • 372
ben75
  • 29,217
  • 10
  • 88
  • 134

5 Answers5

26

On Gradle 1.8, project.archivesBaseName does work, but it gives this complaint:

Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties. Deprecated dynamic property: "archivesBaseName" on "root project 'myapp'", value: "AnotherName".

To get rid of the warning (and to avoid your build breaking on Gradle 2.0), use this:

project.ext.set("archivesBaseName", "AnotherName");

...as suggested in the ExtraPropertiesExtension documentation.

Update (Gradle plugin 1.1.0)

Android Gradle plugin version 1.1.0 introduced a bug which broke archivesBaseName, as friederbluemle noted. But fortunately this has since been fixed (in version 1.1.2).

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
  • Can this be combined with version code and version name ? – Marco RS Dec 12 '14 at 02:45
  • @Marco: not sure, I haven't done that myself. You could check if these questions help: [Change apk name with Gradle](http://stackoverflow.com/q/22126299/56285) & [How to set versionName in APK filename using gradle?](http://stackoverflow.com/q/18332474/56285). If not, ask a new specific question about that! – Jonik Dec 12 '14 at 08:58
  • 1
    For some reason this stopped working with version `1.1.0-rc1` of the Android Gradle plugin. Does anyone know why? – friederbluemle Feb 12 '15 at 10:24
  • 1
    @friederbluemle That was a bug in the plugin, but it was fixed in version 1.1.2. – Jonik Apr 22 '15 at 12:39
  • 1
    @kagali-san: Why? I just verified with latest Android Gradle plugin (1.2.2) and latest Gradle (2.4) that `archivesBaseName` works fine. – Jonik May 05 '15 at 15:22
23

I found a very simple solution. The apk base name is the property archivesBaseName defined on the project. So the following line is enough to rename the apks:

project.archivesBaseName = "AnotherName";
ben75
  • 29,217
  • 10
  • 88
  • 134
  • 2
    how did you found this property? I mean, where? – deadfish Aug 13 '14 at 08:55
  • I found it *somewhere* in gradle source code... but I don't remember precisely where. sorry – ben75 Aug 14 '14 at 18:15
  • 2
    project.archivesBaseName = "${applicationId}-${versionName}-${versionCode}".toString() – danik Apr 25 '16 at 11:01
  • @danik I use your snip, but got error, how to fix? error shows: `Could not get unknown property 'versionName' for project ':app' of type org.gradle.api.Project.` – Ninja Apr 06 '17 at 03:10
  • @Ninja, I use next code: android { defaultConfig { project.archivesBaseName = "${applicationId}-${versionName}-${versionCode}".toString() ` It works for me – danik Apr 08 '17 at 19:14
  • 1
    @danik Thanks for your reply.It is my misstake,use the ${versionName} in the `project.ext.set("archivesBaseName", "${applicationId}-${versionName}-${versionCode}".toString());` Wrong scope, – Ninja Apr 10 '17 at 04:00
  • @deadfish https://docs.gradle.org/4.4.1/dsl/org.gradle.api.Project.html#org.gradle.api.Project:archivesBaseName – CrashCodes Jul 22 '20 at 19:00
11

Starting with version 1.1.0 of the Android Gradle plugin you have to use

archivesBaseName = "yourOtherName"

instead of project.ext.set("archivesBaseName", "yourOtherName") in your build.gradle to make it work.

Fabian Frank
  • 1,115
  • 13
  • 14
  • 1
    There was a bug with `archivesBaseName` in Android Gradle plugin that was [fixed in version 1.1.2](https://developer.android.com/tools/revisions/gradle-plugin.html). `project.ext.set` now works again. – Jonik Apr 22 '15 at 20:20
  • 4
    And it is now completely broken in version 1.3.0 of the Gradle plugin. See https://code.google.com/p/android/issues/detail?id=182016. – Fabian Frank Aug 07 '15 at 05:21
  • 3
    @FabianFrank android gradle tools released in 1.3.1 fix this – Hugo Gresse Aug 27 '15 at 19:16
2

The below codeworked great for me on v1.3.1 and with APKs only:

android {
    defaultConfig {
        archivesBaseName = "AnotherName"

Again, this works with android plugin 1.3.1 (not 1.3.0) but we were still having trouble renaming both our apps (APK) and libs (AAR). A big problem since we have a 20+ project setup where we are locking down where deployed binaries come from and not duplicate code in every sub project build.gradle.

The following worked for us:

Define the following method (and plugin) in root build.gradle:

ext.snapshotSuffix = "<sha>-<count>-SNAPSHOT"
ext.nextVersion = "patch"
apply plugin: 'com.cinnober.gradle.semver-git'

def renameArtifact(variant) {
    variant.outputs.each { output ->
        def fullName = output.outputFile.name
        def projectName = fullName.substring(0, fullName.indexOf('-'))
        output.outputFile = new File(
                (String) output.outputFile.parent,
                (String) output.outputFile.name.replace(projectName, "drivecx-${projectName}-${project.version}"))
    }
}

For applications use:

apply plugin: 'com.android.application'
android {
    ...
    // Rename all output artifacts to include version information
    applicationVariants.all { variant ->
        renameArtifact(variant)
    }
}

For libraries use:

apply plugin: 'com.android.library'
android {
    ...
    // Rename all output artifacts to include version information
    libraryVariants.all { variant ->
        renameArtifact(variant)
    }
}

Now you get some really nice files with a common prefix, project name, git-describe-wth-sha1-buildflavor, and extension.

OLD example:

androidauto-debug.apk

NEW example:

drivecx-androidauto-0.2.1-d1436b7-193-SNAPSHOT-debug.apk

A bonus is that it doesn't rename the unaligned apks.

MattiasF
  • 1,273
  • 1
  • 12
  • 15
0

You can also rename the sub-project (without renaming the directory), by putting the following in the root settings.gradle:

rootProject.children.each {
    it.name = ('app' == it.name ? 'MyAppName' : it.name)
}
Alex Dommasch
  • 211
  • 2
  • 4
  • This is true, although it has the unfortunate side effect that your build commands must conform to the modified names. In the example above, you must type './gradlew :MyAppName:build' instead of './gradlew :app:build'. – John Michelau Oct 23 '19 at 13:32