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

Android studio 如何通过Android studio项目同步自定义编译参数?,android-studio,gradle,gradle-plugin,Android Studio,Gradle,Gradle Plugin,我的项目中的一些参数需要在编译时传入 所以在我的build.gradle文件中有这样一个片段: assemble { description 'Check the necessary parameters and terminate compilation if they do not exist.' if (!project.hasProperty('customArgs')) { throw new RuntimeException("Parameter cu

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

所以在我的
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")
    }
}
编译时,我将使用命令
gradlebuild-PcustomArgs=1234

在我将项目导入Android studio之前,一切都很好

Gradle项目同步始终失败

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


换句话说,如何在Android Studio中设置同步参数?

您应该将此自定义行为从配置阶段移动到执行阶段。否则,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")
        }
    }
}
更多信息