如何在 Gradle 内正确设置 Extra Properties?

How to correctly set Extra Properties within Gradle?

在我的顶级 build.gradle 项目中,我尝试使用以下方法添加 extra properties

project.ext.libraries = [

        junit: 'junit:junit:4.10'

]

然后我尝试在较低级别引用它们 build.gradle 但出现此错误:

testCompile([
                        project.libraries.junit
]) 

但是我得到这个错误:

"Could not find property "libraries" for project

我是否需要在我的 Gradle 代码中添加任何其他内容才能使其正常工作?

假设您已经使用 include 在根 settings.gradle 中正确设置子项目关系,在根级别 build.gradle

ext.libraries = [junit: 'junit:junit:4.10']

在您的子项目中:

dependencies{
  testCompile libraries.junit
}

在您的根项目中定义的所有扩展属性在子项目中都可用。