获取变体属性BuildConfigField
android.applicationVariants.all { variant ->
variant.getVariantData().variantDslInfo.getBuildConfigFields().all{ field ->
println "测试buildConfig输出路径${field}"
}
}
获取BuildConfig.java所在文件路径
//Gradle5.4方式
File buildConfigGeneratedDir = this.variant.getVariantData().getScope().getBuildConfigSourceOutputDir()
//Gradle7.0方式
def appID = variant.getApplicationId()
File buildConfigGeneratedDir = new File("${project.buildDir}/generated/source/buildConfig/${variant.dirName}/${appID}")
创建Task
//Gradle5.4方式
def variantData = variant.variantData
def scope = variantData.scope
//host generate task
def generateHostConfigTaskName = scope.getTaskName(AppConstant.TASK_GENERATE, "HostConfig")
def generateHostConfigTask = project.task(generateHostConfigTaskName)
//Gradle7.0方式
def variantName = variant.name
def capitalizedVariantName = variantName.capitalize()
def generateHostConfigTask = project.tasks.create("${AppConstant.TASK_GENERATE}${capitalizedVariantName}HostConfig")