How can I adapt my build.gradle to change the final basename of the apk(s) ? (without changing the default suffixes)
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).
-
-
@Marco: not sure, I haven't done that myself. You could check if these questions help: Change apk name with Gradle & How to set versionName in APK filename using gradle?. If not, ask a new specific question about that! – Jonik Dec 12 '14 at 8:58
-
1For some reason this stopped working with version
1.1.0-rc1of 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
archivesBaseNameworks fine. – Jonik May 5 '15 at 15:22
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)
}
-
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
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.
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.
-
1There was a bug with
archivesBaseNamein Android Gradle plugin that was fixed in version 1.1.2.project.ext.setnow works again. – Jonik Apr 22 '15 at 20:20 -
4And it is now completely broken in version 1.3.0 of the Gradle plugin. See code.google.com/p/android/issues/detail?id=182016. – Fabian Frank Aug 7 '15 at 5:21
-
3
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";
-
1
-
I found it somewhere in gradle source code... but I don't remember precisely where. sorry – ben75 Aug 14 '14 at 18:15
-
1project.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 6 '17 at 3:10 -
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 4:00