How can I adapt my build.gradle to change the final basename of the apk(s) ? (without changing the default suffixes)
问题:
回答1:
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).
回答2:
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";
回答3:
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.
回答4:
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.
回答5:
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)
}