如何在Gradle中正确设置额外属性?

时间:2015-12-21 15:07:36

标签: java android gradle junit dependencies

在我的顶级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代码中添加其他内容才能使其正常工作?

1 个答案:

答案 0 :(得分:1)

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

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

在您的子项目中:

dependencies{
  testCompile libraries.junit
}

根项目中定义的所有ext属性都可在子项目中使用。