首页 > 解决方案 > 如何通过Android studio项目同步自定义编译参数?

问题描述

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

所以我的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 项目同步总是失败。

在此处输入图像描述

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

也就是说,如何在Android Studio中设置sync的参数?

标签: android-studiogradlegradle-plugin

解决方案


您应该将此自定义行为从配置阶段移至执行阶段。否则 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")
        }
    }
}

更多信息在这里


推荐阅读