Gradle: No such property for class error thrown in

2019-02-24 10:58发布

问题:

I am trying to declare the dependencies in my gradle project using the technique described in this answer.

However when I do so I get this error:

No such property: libraries for class

How can I fix this?

Dependencies declared as properties at top level build.gradle as follows:

ext.libraries = [


junit: 'junit:junit:4.10'

]

Trying to retrieve dependency in module level build.gradle (where error is being thrown):

testCompile([
              libraries.junit
])

Error thrown:

No such property: libraries for class: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

回答1:

  1. In your top level settings.gradle, include your subproject:

    include 'subproject'
    
  2. In your top level build.gradle define your ext properties:

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

it does not have to be a map, if you're using just 1 value, but I'm sticking with your formatting.

  1. To use this in your subproject build.gradle:

    dependencies{
      testCompile libraries.junit 
    }