简体   繁体   English

Gradle获取特定任务的当前风格

[英]Gradle get current flavor for a specific task

I'm trying to deal with google-services.json and different flavors. 我正在尝试处理google-services.json和不同的口味。 The documentation says that we need the file in the root folder. 文档说我们需要根文件夹中的文件。

I got a task that can easily copy the file from the flavor folder to the root folder: 我有一个任务,可以轻松地将文件从flavor文件夹复制到根文件夹:

task CopyToRoot(type: Copy) {
    def appModuleRootFolder = '.'
    def srcDir = 'src'
    def googleServicesJson = 'google-services.json'

    outputs.upToDateWhen { false }
    def flavorName = android.productFlavors.flavor1.name

    description = "Switches to $flavorName $googleServicesJson"
    delete "$appModuleRootFolder/$googleServicesJson"
    from "${srcDir}/$flavorName/"
    include "$googleServicesJson"
    into "$appModuleRootFolder"
}

Then, in the afterEvaluate I force 然后,在afterEvaluate I force

afterEvaluate {
    processFlavor1DebugGoogleServices.dependsOn CopyToRoot
    processFlavor1ReleaseGoogleServices.dependsOn CopyToRoot
}

This works only for 1 flavor (defined statically). 这仅适用于1种风味(静态定义)。 How to do this for every flavor? 如何为每种口味做到这一点? I got 4 flavors. 我有4种口味。 How to get the current flavor that is being compiled? 如何获得正在编译的当前风味?

[ UPDATE 1 ] - Also tried: [ 更新1 ] - 也尝试过:

task CopyToRoot(type: Copy) {
    def appModuleRootFolder = '.'
    def srcDir = 'src'
    def googleServicesJson = 'google-services.json'

    outputs.upToDateWhen { false }
    def flavorName = android.productFlavors.flavor1.name

    android.applicationVariants.all { variant ->
        def flavorString = variant.getVariantData().getVariantConfiguration().getFlavorName()
        println('flavorString: ' + flavorString)

        description = "Switches to $flavorString $googleServicesJson"
        delete "$appModuleRootFolder/$googleServicesJson"
        from "${srcDir}/$flavorString/"
        include "$googleServicesJson"
        into "$appModuleRootFolder"
    }
 }

But this doesn't copy the correct file. 但这不会复制正确的文件。 Any help? 有帮助吗?

A way to go is to create a folder named "google-services" in each flavor, containing both the debug version and the release version of the json file : 一种方法是在每种风格中创建一个名为“google-services”的文件夹,其中包含调试版本和json文件的发行版本:

在此输入图像描述

In the buildTypes section of your gradle file, add this : 在gradle文件的buildTypes部分中,添加以下内容:

    applicationVariants.all { variant ->
        def buildTypeName = variant.buildType.name
        def flavorName = variant.productFlavors[0].name;

        def googleServicesJson = 'google-services.json'
        def originalPath = "src/$flavorName/google-services/$buildTypeName/$googleServicesJson"
        def destPath = "."

        copy {
            if (flavorName.equals(getCurrentFlavor()) && buildTypeName.equals(getCurrentBuildType())) {
                println originalPath
                from originalPath
                println destPath
                into destPath
            }
        }
    }

It will copy the right json file at the root of your app module automatically when you'll switch build variant. 当您切换构建变体时,它会自动在您的应用程序模块的根目录下复制正确的json文件。

Add the two methods called to get the current flavor and current build type at the root of your build.gradle 添加两个调用的方法,以获取build.gradle根目录下的当前flavor和当前构建类型

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() ) {
        println matcher.group(1).toLowerCase()
        return matcher.group(1).toLowerCase()
    }
    else
    {
        println "NO MATCH FOUND"
        return "";
    }
}

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

        if (tskReqStr.contains("Release")) {
            println "getCurrentBuildType release"
            return "release"
        }
        else if (tskReqStr.contains("generateDebug")) {
            println "getCurrentBuildType debug"
            return "debug"
        }

    println "NO MATCH FOUND"
    return "";
}

Based on this answer 基于这个答案

I found my answer. 我找到了答案。 Google finally supports different google-services.json per flavor. Google最终支持不同的google-services.json。 You just need to update the plugin to com.google.gms:google-services:2.0.0. 您只需将插件更新为com.google.gms:google-services:2.0.0即可。 There's no need to copy the json file to the app folder, just put your google-services.json different files inside your build flavors directories. 没有必要将json文件复制到app文件夹,只需将google-services.json不同的文件放在build flavor目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM