我有:
myData.properties
project/config/{FLAVOR_NAME}
个文件
gradle
文件,其中包含将字段添加到BuildConfig.java
文件的方法。我的目标:
在BuildConfig.java
我的问题:
我可以将flavor名称传递给我的方法以获取正确的文件,但在这种情况下找不到buildConfigField
方法。
此外,我可以将ApplicationVariant variant
变量传递给我的方法并在我的自定义gradle文件中调用variant.buildConfigField
方法,但我无法在variant
中获得productFlavors
变量} build.gradle(module app)
的部分。我可以在循环applicationVariants.all { variant ->
时将其传递到外面,但是它会迭代所有风格,如果我不能错过myData.properties
的某种风味。我希望能够只构建我有文件的风格并抛出我没有此文件的自定义异常。
TL / DR
如何在自定义buildConfigField
文件中调用gradle
或如何在ApplicationVariant variant
中获取productFlavors
我的自定义gradle.file
:
marketSkusCreator.gradle
ext {
fillArrays = { variant ->
def pathToFileWithData = "config/" + variant.productFlavors[0].name + "/marketSkus.properties"
if (rootProject.file(pathToFileWithData).exists()) {
def marketSkusPropertiesFile = rootProject.file(pathToFileWithData);
def properties = new Properties()
properties.load(new FileInputStream(marketSkusPropertiesFile))
//buildConfigField - method not found, so I cant pass only flavor name as string to fillArrays method
//buildConfigField('String[]', 'OLD_SKUS', properties.get("marketSkusOld"))
variant.buildConfigField('String[]', 'OLD_SKUS', properties.get("marketSkusOld"))
variant.buildConfigField('String[]', 'VER_2_SKUS', properties.get("marketSkusVer2"))
variant.buildConfigField('String[]', 'INAPP_SKUS', properties.get("marketSkusInapp"))
} else {
logger.lifecycle('pathToFileWithData: {}', pathToFileWithData)
throw new GradleException("You need to have marketSkus.properties file in root of project with " +
"marketSkusOld," +
"marketSkusVer2," +
" variables to build project")
}
}
}
build.gradle(模块应用)
import com.android.build.gradle.api.ApplicationVariant
apply plugin: 'com.android.application'
apply from: '../marketSkusCreator.gradle'
android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
productFlavors {
es {
fillArrays(curVariant) //how to get it - {curVariant} (type: ApplicationVariant)
}
en {
fillArrays(curVariant) //how to get it - {curVariant} (type: ApplicationVariant)
}
}
//this works, but I do not want to call method for each flavor during each build
//applicationVariants.all {ApplicationVariant variant ->
// fillArrays(variant)
//}
}
俄语中的同样问题:buildConfigField метод не виден в отдельном gradle файле