Android Perform only the Gradle task in the release version.

I am trying to configure the build.gradle file only to complete the gradle task when I select the release build option. Until now, my task has always been carried out, regardless of whether it is part of my debug or release types, and also signs configs. I tried to add my task to the applicationsVariants block and check if this is a release option, but it just looks through all the options.

applicationVariants.all { variant -> variant.outputs.each { output -> ... } } 

I know that both debugging and release tasks are always performed depending on which build option you choose. Is it possible to execute some code only when creating an assembly for release? If so, where does this code go? Thanks!

I read every Stackoverflow question about this, but none of the answers I really wanted. My ultimate goal is when I select the "release" build option for the Play Store build, the message is sent to our server. I do not want this to happen when I am just debugging.

+12
source share
2 answers

Add doFirst or doLast for the type of assembly 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") } } } 
+18
source

I needed to do something similar to check the 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) } } } } 
+1
source

Source: https://habr.com/ru/post/986077/


All Articles