Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Only execute Gradle task on release build variant

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.

like image 326
Mark Avatar asked Apr 27 '15 16:04

Mark


People also ask

What is Buildtype in Gradle Android?

Build types define certain properties that Gradle uses when building and packaging your app, and are typically configured for different stages of your development lifecycle.

How do I change a variant in build?

NOTE: By default, the Android Studio will generate "debug" and "release" Build Types for your project. So, to change a Build Type, all you need to do is just select your Build Type from the Build Variant and after the project sync, you are good to go.

What is BuildConfig in Android?

BuildConfig is a class containing static constants that is included in every Android application. BuildConfig includes some default fields such as DEBUG and FLAVOR but you can also add custom values via build. gradle .

What is flavorDimensions?

Flavor Dimensions is a way to group flavors by a name. For now, we're using just a single group. Add the following line in your defaultConfig block: flavorDimensions "default" Now syncing the gradle would give you the following product flavors: Android Build Variants combine build types and product flavors.


2 Answers

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")
        }
    }
}
like image 67
Korniltsev Anatoly Avatar answered Sep 19 '22 14:09

Korniltsev Anatoly


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)
            }
        }
    } }
like image 37
Michał Janowicz Avatar answered Sep 18 '22 14:09

Michał Janowicz