Android的工作室:摇篮产品口味:定义自定义属性自定义、摇篮、口味、属性

2023-09-06 15:53:47 作者:夏清眸-°

我建立的摇篮(Android的一室公寓)一个Android应用程序的不同的产品口味。

I am building different product flavors of an Android App in Gradle (Android Studio).

因此​​,我定义了以下产品口味:

Hence I defined the following product flavors:

android {

    project.ext.set("customer", "")
    project.ext.set("server", "")

    //Configuration happens here - code removed for readability

    buildTypes {

        debug {
            server = "test"
        }

        release {
            server = "release"
        }
    }

    //Available product flavors
    productFlavors {
        customerA{
            customer = "a"
        }
        customerB{
            customer = "b"
        }
        customerC{
            customer = "c"
        }
    }
}

不过,后来,当我访问定义的项目属性客户(其值设置在产品的味道我目前正在建设),在我构建的任务之一,它的值总是C,尽管IAM建筑customerA(在这种情况下,属性的客户应为一个,而不是C)。比如我执行以下任务以后:

However, later on, when I access the defined project property "customer" (whose value is set in the product flavor i am currently building) in one of my build tasks, it always has the value "c" even though iam building customerA (in which case the property customer should be "a" rather than "c"). For instance I execute the following task later on:

preBuild << {
    println "Building customer: " + customer
}

和它总是输出:

建立客户:C

所以,我猜测有一定的覆盖怎么回事?可能与VS执行阶段的配置?不知道如何/为什么了,所以任何帮助是大大AP preciated。

So i am guessing there is some overwriting happening? Possibly related to the configuration VS execution phase? Not sure how/why though, so any help is be greatly appreciated.

更新:另外,它应该已经让我再重新确定的产品风味的名称(不连接到它生成类型名称),并生成类型(:无产品的风味名$ p $在摇篮构建的执行阶段ppended的话)。

UPDATE: Alternatively it would already get me further to determine the name of the product flavor (without the build type name attached to it) and the build type (again: without the product flavor name prepended to it) during execution phase of the gradle build.

考虑到上述结构的预期产物风味的名称将是:customerA,customerB和customerC。

Considering the above configuration the expected product flavor names would be: customerA, customerB and customerC.

推荐答案

在评估阶段,摇篮执行所有的code在机器人块;它不只是执行与您要编译的口味code。事实上,在评估阶段,它并不真正了解你的口味;它有评估,要了解一下。

During evaluation phase, Gradle executes all of the code in your android block; it doesn't just execute the code relevant to the flavors you want to compile. In fact, during evaluation phase, it doesn't even really know what your flavors are; it has to evaluate that to find out.

因此​​,所有三个你行顾客=一个顾客=B顾客=C将得到执行。

So all three of your lines customer = "a", customer = "b", and customer = "c" will get executed.

这是关于摇篮的微妙的东西,使一个有点困难,以了解它的。

This is one of the subtle things about Gradle that make it a little difficult to learn.

所以,我已经解释了为什么你的code不工作,你期望的方式,但这个答案是不完全的,因为我没有说了很多关于怎样做才能使它工作的权利,但很难说什么这样做,因为我不知道你要完成的任务。总的来说,我可以说,你应该想努力完成你想要使用的用户定义的任务是什么,以及建立内部任务相关性,以确保事情按正确的顺序被执行。与Android摇篮构建了一个疑难杂症的是,即使这些任务没有得到确定之前,评估阶段(它不知道它需要建立所有口味的任务,直到它评估了构建文件,并知道这些口味的),所以做一些SO侦探,看看如何挂钩的东西到Android的摇篮构建任务 - 你必须设置你的任务,在评估阶段结束后的And​​r​​oid插件已经做了的事情

So I've explained why your code isn't working the way you expect, but this answer is incomplete because I haven't said a lot about what to do to make it work right, but it's hard to say what to do because I'm not sure what you're trying to accomplish. In general I can say that you should think of trying to accomplish what you want using user-defined tasks, and setting up intra-task dependencies to make sure things get executed in the right order. A gotcha with Android Gradle builds is that even those tasks don't get defined until evaluation phase (it can't know what tasks it needs to build all your flavors until it's evaluated the build file and knows what those flavors are), so do some SO sleuthing to see how to hook things onto Android Gradle build tasks -- you have to set up your tasks at the end of evaluation phase after the Android plugin has done its thing.