3

This time I have this problem, I am trying to get the current flavor in a gradle script. I have tried the answers given here How to get current flavor in gradle without luck.

I haven seen in some answers that they use

// Get all flavors
android.productFlavors.all { flavor ->
if (flavor.name.equals("flavorName")) {
    // do what you like
}
// ...
}

But also I didn't have any luck with that because i get the following error: > Could not get unknown property 'android' for task

So I don't know how to get the current flavor, any help will be very appreciated thanks!!!

EDIT: What I need to do is to execute a piece of code that is diferent for each flavor, my current idea is to know the selected build variant to do this in a task, but if there is any othe way to do this would be perfect.

Community
  • 1
  • 1
acabezas
  • 731
  • 1
  • 7
  • 20
  • "I am trying to get the current flavor in a gradle script" -- there is no "current flavor". A `build.gradle` file does not build your app. It builds an object model describing how to build your app. The script is run when you open your project or when you sync your project with your Gradle build files, and at that point, all flavors are "current". "because i get the following error" -- then perhaps you have that code in the wrong place. Edit your question and post the entire `build.gradle` file. – CommonsWare Nov 10 '16 at 13:41
  • Where did you put the lines of code? Which build.cradle file? – Code-Apprentice Nov 10 '16 at 13:43
  • I added the code lines in the wrong place and thats why i got that error, thanks! but i still have the question, how could i get the selected build variant or falvor selected for the project in a gradle task?? – acabezas Nov 10 '16 at 15:10
  • To my knowledge. Usually the tasks contains a variant name in their task names and only and we can identify the variant based on that. For example we have `debug` build type with `demo` flavor then there would be tasks like `assembleDevDebug` task where this task is of `DefaultTask` and we can get all tasks of type defaultTasks by `tasks.withType(DefaultTask){ if(getName() == 'assembleDevDebug'){ // do something } }` in this way we can verify the tasks of specific variant and can write conditions or dependencies for tasks to execute etc. – Tej Aug 10 '17 at 20:49

2 Answers2

3

I already posted a working solution here, that is:

The following function returns exactly the current flavor name:

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String  tskReqStr = gradle.getStartParameter().getTaskRequests().toString()

    Pattern pattern;

    if( tskReqStr.contains( "assemble" ) )
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    else
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")

    Matcher matcher = pattern.matcher( tskReqStr )

    if( matcher.find() )
        return matcher.group(1).toLowerCase()
    else
    {
        println "NO MATCH FOUND"
        return "";
    }
}

You need also

import java.util.regex.Matcher
import java.util.regex.Pattern

at the beginning or your script. In Android Studio this works by compiling with "Make Project" or "Debug App" button.

Poiana Apuana
  • 1,406
  • 1
  • 10
  • 16
0

You can get this error if your use it out of "android" closure at app level gradle script. Make sure that you use it inside

  • you were correct, i was using it outside the android closure, but the problem remains, how can i know the selected build variant for the project in a gradle task? – acabezas Nov 10 '16 at 15:03
  • If I understood correct current project flavor you can find in Build Variant tab. If you are using android studio open Build -> Select build variant... – Nikita Emelyanov Nov 11 '16 at 13:41
  • that is correct but i would like to know the build variant in a gradle task, because depending on the selected flavor i need to do different things – acabezas Nov 11 '16 at 15:03