我正在 Gradle (Android Studio) 中构建不同产品风格的 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建立客户A(在这种情况下,财产客户应该是a"而不是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执行阶段有关?不知道如何/为什么,所以非常感谢任何帮助.
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.
更新:或者,它已经让我进一步确定产品风味的名称(没有附加构建类型名称)和构建类型(再次:没有附加产品风味名称到它)在 gradle 构建的执行阶段.
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.
在评估阶段,Gradle 会执行 android
块中的所有代码;它不只是执行与您要编译的风格相关的代码.事实上,在评估阶段,它甚至不知道你的口味是什么;它必须评估才能找出答案.
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.
所以您的所有三行 customer = "a"
、customer = "b"
和 customer = "c"
都会得到执行.
So all three of your lines customer = "a"
, customer = "b"
, and customer = "c"
will get executed.
这是 Gradle 的一个微妙之处,它使它有点难以学习.
This is one of the subtle things about Gradle that make it a little difficult to learn.
所以我已经解释了为什么你的代码没有按照你期望的方式工作,但是这个答案是不完整的,因为我没有说很多关于如何做才能让它正常工作,但很难说该怎么做这样做是因为我不确定您要完成什么.一般来说,我可以说你应该考虑尝试使用用户定义的任务来完成你想要的事情,并设置任务内的依赖关系以确保事情以正确的顺序执行.Android Gradle 构建的一个问题是,即使是这些任务也直到评估阶段才被定义(在评估构建文件并知道这些风格是什么之前,它无法知道构建所有风格所需的任务),所以做进行一些调查以了解如何将事物挂接到 Android Gradle 构建任务上——您必须在 Android 插件完成任务后在评估阶段结束时设置任务.
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.
这篇关于Android Studio:Gradle 产品风味:定义自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!