問題描述
我正在 Gradle (Android Studio) 中構(gòu)建不同產(chǎn)品風(fēng)格的 Android 應(yīng)用.
I am building different product flavors of an Android App in Gradle (Android Studio).
因此我定義了以下產(chǎn)品風(fēng)味:
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"
}
}
}
但是,稍后,當(dāng)我在我的一個(gè)構(gòu)建任務(wù)中訪問定義的項(xiàng)目屬性客戶"(其值在我當(dāng)前正在構(gòu)建的產(chǎn)品風(fēng)格中設(shè)置)時(shí),它始終具有值c",即使 iam建立客戶A(在這種情況下,財(cái)產(chǎn)客戶應(yīng)該是a"而不是c").例如,我稍后執(zhí)行以下任務(wù):
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
所以我猜有一些覆蓋發(fā)生?可能與配置VS執(zhí)行階段有關(guān)?不知道如何/為什么,所以非常感謝任何幫助.
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.
更新:或者,它已經(jīng)讓我進(jìn)一步確定產(chǎn)品風(fēng)味的名稱(沒有附加構(gòu)建類型名稱)和構(gòu)建類型(再次:沒有附加產(chǎn)品風(fēng)味名稱到它)在 gradle 構(gòu)建的執(zhí)行階段.
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.
考慮到上述配置,預(yù)期的產(chǎn)品風(fēng)味名稱將是:customerA、customerB 和 customerC.
Considering the above configuration the expected product flavor names would be: customerA, customerB and customerC.
推薦答案
在評估階段,Gradle 會(huì)執(zhí)行 android
塊中的所有代碼;它不只是執(zhí)行與您要編譯的風(fēng)格相關(guān)的代碼.事實(shí)上,在評估階段,它甚至不知道你的口味是什么;它必須評估才能找出答案.
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"
都會(huì)得到執(zhí)行.
So all three of your lines customer = "a"
, customer = "b"
, and customer = "c"
will get executed.
這是 Gradle 的一個(gè)微妙之處,它使它有點(diǎn)難以學(xué)習(xí).
This is one of the subtle things about Gradle that make it a little difficult to learn.
所以我已經(jīng)解釋了為什么你的代碼沒有按照你期望的方式工作,但是這個(gè)答案是不完整的,因?yàn)槲覜]有說很多關(guān)于如何做才能讓它正常工作,但很難說該怎么做這樣做是因?yàn)槲也淮_定您要完成什么.一般來說,我可以說你應(yīng)該考慮嘗試使用用戶定義的任務(wù)來完成你想要的事情,并設(shè)置任務(wù)內(nèi)的依賴關(guān)系以確保事情以正確的順序執(zhí)行.Android Gradle 構(gòu)建的一個(gè)問題是,即使是這些任務(wù)也直到評估階段才被定義(在評估構(gòu)建文件并知道這些風(fēng)格是什么之前,它無法知道構(gòu)建所有風(fēng)格所需的任務(wù)),所以做進(jìn)行一些調(diào)查以了解如何將事物掛接到 Android Gradle 構(gòu)建任務(wù)上——您必須在 Android 插件完成任務(wù)后在評估階段結(jié)束時(shí)設(shè)置任務(wù).
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.
這篇關(guān)于Android Studio:Gradle 產(chǎn)品風(fēng)味:定義自定義屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!