How to get current flavor in gradle

你。 提交于 2019-11-26 05:31:11

问题


I have two product flavors for my app:

productFlavors {
    europe {
        buildConfigField(\"Boolean\", \"BEACON_ENABLED\", \"false\")
    }

    usa {
        buildConfigField(\"Boolean\", \"BEACON_ENABLED\", \"true\")
    }
}

Now I want to get the current flavor name (which one I selected in Android Studio) inside a task to change the path:

task copyJar(type: Copy) {
    from(\'build/intermediates/bundles/\' + FLAVOR_NAME + \'/release/\')
}

How can I obtain FLAVOR_NAME in Gradle?

Thanks


回答1:


How to get current flavor name

I have developed the following function, returning 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.

How to get current build variant

def getCurrentVariant() {
    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(2).toLowerCase()
    }else{
        println "NO MATCH FOUND"
        return "";
    }
}

How to get current flavor applicationId

A similar question could be: how to get the applicationId? Also in this case, there is no direct way to get the current flavor applicationId. Then I have developed a gradle function using the above defined getCurrentFlavor function as follows:

def getCurrentApplicationId() {
    def currFlavor = getCurrentFlavor()

    def outStr = ''
    android.productFlavors.all{ flavor ->

        if( flavor.name==currFlavor )
            outStr=flavor.applicationId
    }

    return outStr
}

Voilà.




回答2:


I use this

${variant.getFlavorName()}.apk

to format file name output




回答3:


you should use this,${variant.productFlavors[0].name},it will get productFlavors both IDE and command line.




回答4:


This is what I used some time ago. I hope it's still working with the latest Gradle plugin. I was basically iterating through all flavours and setting a new output file which looks similar to what you are trying to achieve.

applicationVariants.all { com.android.build.gradle.api.ApplicationVariant variant ->
    for (flavor in variant.productFlavors) {
        variant.outputs[0].outputFile = file("$project.buildDir/${YourNewPath}/${YourNewApkName}.apk")
    }
}



回答5:


my solution was in that to parse gradle input parameters.

Gradle gradle = getGradle()

Pattern pattern = Pattern.compile(":assemble(.*?)(Release|Debug)");
Matcher matcher = pattern.matcher(gradle.getStartParameter().getTaskRequests().toString());
println(matcher.group(1))



回答6:


get SELECTED_BUILD_VARIANT from the .iml file after gradle sync completes You can either load it using an xml parser, or less desireable, but probably faster to implement would be to use a regex to find it.

<facet type="android" name="Android">
  <configuration>
    <option name="SELECTED_BUILD_VARIANT" value="your_build_flavorDebug" />
        ...

(not tested, something like this:)

/(?=<name="SELECTED_BUILD_VARIANT".*value=")[^"]?/



回答7:


I slightly changed Poiana Apuana's answer since my flavor has some capital character.

REASON
gradle.getStartParameter().getTaskRequests().toString() contains your current flavor name but the first character is capital.
However, usually flavor name starts with lowercase. So I forced to change first character to lowercase.

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String taskReqStr = gradle.getStartParameter().getTaskRequests().toString()
    Pattern pattern
    if (taskReqStr.contains("assemble")) {
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    } else {
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
    }
    Matcher matcher = pattern.matcher(taskReqStr)
    if (matcher.find()) {
        String flavor = matcher.group(1)
        // This makes first character to lowercase.
        char[] c = flavor.toCharArray()
        c[0] = Character.toLowerCase(c[0])
        flavor = new String(c)
        println "getCurrentFlavor:" + flavor
        return flavor
    } else {
        println "getCurrentFlavor:cannot_find_current_flavor"
        return ""
    }
}



回答8:


You can use gradle.startParameter.taskNames[0]




回答9:


Use:

${variant.baseName}.apk"

This return current flavor name

Full Answer

android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.baseName}.apk"
    }
}


来源:https://stackoverflow.com/questions/30621183/how-to-get-current-flavor-in-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!