I am trying to configure my build.gradle file to only execute a gradle task when the release build variant is selected. So far, my task always gets executed, whether it is in my debug or release build types or signing configs. I have tried adding my task inside an applicationsVariants block and check if it is the release variant, but it just loops through all variants.
applicationVariants.all { variant ->
variant.outputs.each { output ->
...
}
}
I know that both the debug and release tasks always run for whichever build variant you choose. Is it possible to execute some code only when creating a build for release? If so, where does that code go? Thanks!
I have read through every Stackoverflow question on this, but none of the answers really did I am wanting. My end goal is when I select the "release" build variant for a Play Store build, a message is posted to our server. I do not want this to happen when just debugging.
Add doFirst or doLast for the build type you are interested in.
android.applicationVariants.all { variant ->
if ( variant.buildType.name == "release"){
variant.assemble.doLast { // Can also use doFirst here to run at the start.
logger.lifecycle("we have successfully built $v.name and can post a messaage to remote server")
}
}
}
I had to do something like this to check build version:
buildTypes {
applicationVariants.all { variant ->
variant.outputs.each {output ->
def project = "AppName"
def separator = "_"
/*def flavor = variant.productFlavors[0].name*/
def buildType = variant.variantData.variantConfiguration.buildType.name
def versionName = variant.versionName
def versionCode = variant.versionCode
def date = new Date();
def formattedDate = date.format('yyyyMMdd_HHmm')
if (variant.buildType.name == "release"){
def newApkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + separator + formattedDate + ".apk"
output.outputFile = new File(output.outputFile.parent, newApkName)
}
if (variant.buildType.name == "debug"){
def newApkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + ".apk"
output.outputFile = new File(output.outputFile.parent, newApkName)
}
}
} }
Related
I'm building APKs from command line as follows
./gradlew assembleProductionRelease
Are there any configuration params which will allow to add buildNumber and versionNumber to APK file name automatically? By default APK file will be named as app-production-release.
You can try/adapt this code (add it to your build.gradle):
applicationVariants.all { variant ->
variant.outputs.all { output ->
def sep = '_'
def version = variant.versionName
def build = variant.versionCode
outputFileName = "${rootProject.name}${sep}" +
"${variant.buildType.name}${sep}" +
"${version}${sep}" +
"build${sep}${build}.apk"
}
}
I was using the following code in my gradle script to rename the apks generated with AndroidStudio:
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, defaultConfig.versionCode + "_" + output.outputFile.name)
}
}
So it was generating apks with names like: 345-app-release.apk, where 345 is the versionCode.
But after updating to AndroidStudio 3.0 it returns the following error:
Cannot set the value of read-only property 'outputFile' for
ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug,
filters=[]}} of type
com.android.build.gradle.internal.api.ApkVariantOutputImpl.
How can I achieve a similar renaming with the new build tools.
Use output.outputFileName instead of output.outputFile
2019 - Simple Solution for Gradle 3.0+** and 3.1.0
To change the name of the APK in Android
android { //add inside the android {}
......
applicationVariants.all { variant ->
variant.outputs.all {
def flavor = variant.name
def versionName = variant.versionName
outputFileName = "prefix_${flavor}_${versionName}.apk"
}
}
}
prefix_release_1.0.1.apk
Try this code :
buildTypes {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def name = "myapp_v${variant.versionName}(${variant.versionCode}).apk"
output.outputFileName = name
}
}
}
In or After gradle 3.1.0
try below code
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = new File(
"release_build", // here you can change the name
output.outputFile.name)
}
}
In my case I resolved it by just refusing to update to Gradle 4.4 (in my case).So when Studio asks (when you open your project for the first time) you to update Gradle to support instant run,etc just simply refuse and you should be fine.
What worked for me was to
1. change each to "all"
2. change output.outputFile to "outputFileName"
3. Run $./gradlew clean build in terminal
For example, if your artifacts.gradle settings were this:
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def finalVersionCode = 10000 + versionCode
output.versionCodeOverride = finalVersionCode
output.outputFile = new File(
output.outputFile.parent, output.outputFile.name.replace(".apk","-${finalVersion}.apk"))
}
}
Then you would want to change it to this:
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def finalVersionCode = 10000 + versionCode
output.versionCodeOverride = finalVersionCode
outputFileName = new File(
output.outputFile.parent,
outputFileName.replace(".apk", "-${finalVersionCode}.apk"))
}
}
change your code into:-
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, "${variant.applicationId}-${variant.versionName}.apk")
}
}
I am trying to use android build tools "com.android.tools.build:gradle:3.0.0-alpha4" in my project. In my build script I rename the output apk which worked fine in the past but does not seem to be supported any more.
applicationVariants.all { variant ->
def filename = "foo-${variant.baseName}-${variant.versionName}-(${android.defaultConfig.versionCode}).apk"
variant.outputs.all { output ->
output.outputFile = new File(
output.outputFile.parent,
filename
)
}
}
Now the propery I am trying to change became immutable:
Error: Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=stageDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
Is there a new or an alternate way how to do this?
Change each -> to all
Change output.outputFile -> to outputFileName
before:
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def finalVersionCode =v10000 + versionCode
output.versionCodeOverride = finalVersionCode
output.outputFile = new File(
output.outputFile.parent, output.outputFile.name.replace(".apk","-${finalVersion}.apk"))
}
}
after:
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def finalVersionCode = 10000 + versionCode
output.versionCodeOverride = finalVersionCode
outputFileName = new File(
output.outputFile.parent,
outputFileName.replace(".apk", "-${finalVersionCode}.apk"))
}
}
Add e.g. versionName to apk by adding
setProperty("archivesBaseName", archivesBaseName + "-" + versionName)
in defaultConfig closure.
I'm composing .apk filename using current app version and flavor name. I'd like to add current ABI split name as well, but only if it's a universal apk.
My relevant build.gradle sections:
buildTypes {
release {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def flavor = .... // some code to parse flavor & determine an appropriate string from it
output.outputFile = new File(output.outputFile.parent, "app_" + flavor + "_0" + variant.versionCode + ".apk")
}
}
}
}
productFlavors {
deploy {
splits {
abi {
enable true
reset()
include 'armeabi-v7a' //select ABIs to build APKs for
universalApk true //generate an additional APK that contains all the ABIs
}
}
}
}
Currently this config generates two .apks, but they both have the same file name as I don't know how to get the ABI name, so the one generated later overwrites the one generated before.
So, what is the equivalent variant.productFlavors.get(0) for current ABI split?
That's very strange as flavor and ABI-name is automatically added to build name (if you make corresponding assemble)
can you try completely remove your custom made naming
applicationVariants.all { variant ->
variant.outputs.each { output ->
def flavor = .... // some code to parse flavor & determine an appropriate string from it
output.outputFile = new File(output.outputFile.parent, "app_" + flavor + "_0" + variant.versionCode + ".apk")
}
}
and instead of that try to add to defaultConfig this line
archivesBaseName = "app_${versionCode}"
If this is will not resolve you issues you can try to get abi from output
output.getFilter(com.android.build.OutputFile.ABI)
The equivalent is output.getFilter(com.android.build.OutputFile.ABI).
Note that, starting with Android Studio 3.0, you should use outputFileName and variant.outputs.all instead:
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "app_" + output.getFilter(com.android.build.OutputFile.ABI) + "_0" + variant.versionCode + ".apk"
}
}
I want the .apk to be built with the following name format (with timestamp).
How can I set it?
format : {app_name}{yyyymmddhis}.apk
Now, it is fixed with the name {app_name}-{release}.apk
in the build.gradle file, you should change/add buildTypes like this:
buildTypes {
release {
signingConfig signingConfigs.release
applicationVariants.all { variant ->
def file = variant.outputFile
def date = new Date();
def formattedDate = date.format('yyyyMMddHHmmss')
variant.outputFile = new File(
file.parent,
file.name.replace("-release", "-" + formattedDate)
)
}
}
}
====== EDIT with Android Studio 1.0 ======
If you are using Android Studio 1.0, you will get an error like this:
Error:(78, 0) Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated#67e7625f.
You should change the build.Types part to this:
buildTypes {
release {
signingConfig signingConfigs.releaseConfig
applicationVariants.all { variant ->
variant.outputs.each { output ->
def date = new Date();
def formattedDate = date.format('yyyyMMddHHmmss')
output.outputFile = new File(output.outputFile.parent,
output.outputFile.name.replace("-release", "-" + formattedDate)
)
}
}
}
}
As a side note, I would really like to know where people get the objects structures of gradle build objects.
To construct this I've used some trial and errors, a bit of Googling, and this (which is out of date)
android {
...
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def flavor = "default";
if (variant.productFlavors.size() > 0)
flavor = variant.productFlavors.get(0);
def initials = "DefaultFlavor";
if (flavor.name == "flavor1")
initials = "F1";
else if (flavor.name == "flavor2")
initials = "F2";
def build = "Debug";
if (variant.buildType.name == "release")
build = "Release"
def finalName = variant.versionCode + "-" + initials + "-" + build + "-v" + flavor.versionName + "-MyAppName.apk";
output.outputFile = new File(output.outputFile.parent, finalName)
}
}
}
...
}
Ok, So as a side note, if you would like to determine the names of your libs project here is a quick script, make sure you put it in each of your library gradle build files.
android {
...
defaultConfig {
minSdkVersion 10
targetSdkVersion 16
versionCode 1
versionName "1.0.4"
project.version = versionName
}
libraryVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.aar')) {
def fileName = "${archivesBaseName}-v${version}.aar"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
...
}
You should change something like this. Doing dynamically.
project.archivesBaseName = {app_name}{yyyymmddhis};
But I have read that this is going to be deprecated.
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Deprecated dynamic property: "archivesBaseName" on "root project 'myapp'", value: "AnotherName".