問題描述
我的應用有兩種產(chǎn)品風格:
I have two product flavors for my app:
productFlavors {
europe {
buildConfigField("Boolean", "BEACON_ENABLED", "false")
}
usa {
buildConfigField("Boolean", "BEACON_ENABLED", "true")
}
}
現(xiàn)在我想在任務中獲取當前風味名稱(我在 Android Studio 中選擇的名稱)以更改路徑:
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/')
}
如何在 Gradle 中獲取 FLAVOR_NAME?
How can I obtain FLAVOR_NAME in Gradle?
謝謝
推薦答案
如何獲取當前風味名稱
我開發(fā)了以下函數(shù),準確返回當前風味名稱:
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 ""
}
}
你也需要
import java.util.regex.Matcher
import java.util.regex.Pattern
在開頭或您的腳本中.在 Android Studio 中,這通過使用Make Project"或Debug App"按鈕進行編譯來工作.
at the beginning or your script. In Android Studio this works by compiling with "Make Project" or "Debug App" button.
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 ""
}
}
如何獲取當前風味 applicationId
類似的問題可能是:如何獲取 applicationId?同樣在這種情況下,沒有直接的方法來獲取當前的風味 applicationId.然后我使用上面定義的getCurrentFlavor函數(shù)開發(fā)了一個gradle函數(shù),如下:
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
}
瞧.
這篇關于如何在 gradle 中獲取當前風味的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!