如何通过Android工作室项目同步自定义编译参数?

How can custom compilation parameters be synchronized through the Android studio project?

我项目中的一些参数需要在编译时传入。

所以我的build.gradle文件中有这样一个片段:

assemble {
    description 'Check the necessary parameters and terminate compilation if they do not exist.'
    if (!project.hasProperty('customArgs')) {
        throw new RuntimeException("Parameter customArgs is not defined")
    }
}

我编译的时候会用命令gradle build -PcustomArgs=1234

一切都很好,直到我将项目导入 Android studio。

Gradle 项目同步总是失败。

也就是说在项目同步的过程中,找不到可以设置参数的地方

也就是说AndroidStudio中同步的参数怎么设置?

您应该将此自定义行为从配置阶段移至执行阶段。否则 Android Studio 将在配置项目时执行它。

assemble {
    doLast {
        description 'Check the necessary parameters and terminate compilation if they do not exist.'
        if (!project.hasProperty('customArgs')) {
            throw new RuntimeException("Parameter customArgs is not defined")
        }
    }
}

更多信息here