在构建期间Gradle NPE?

时间:2016-01-20 11:02:27

标签: java gradle properties nullpointerexception dependencies

我在尝试构建项目时得到NullPointerException。我目前在顶层声明我的依赖关系为properties,然后在子模块中引用(如下所示):

错误:

A problem occurred evaluating project ':myProject'.
> java.lang.NullPointerException (no error message)

Caused by: java.lang.NullPointerException
        at org.gradle.util.GUtil.flatten(GUtil.java:69)
        at org.gradle.util.GUtil.flatten(GUtil.java:66)
        at org.gradle.util.GUtil.collectionize(GUtil.java:93)
        at org.gradle.util.GUtil$collectionize.call(Unknown Source)
        at org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.methodMissing(DefaultDependencyHandler.groovy:
        at org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.invokeMethod(DefaultDependencyHandler.groovy)  

顶级声明:

ext.libraries = [

   junit: 'junit:junit:4.12',
   mockito: 'org.mockito:mockito-all:1.9.5'

]

引用子模块中的属性:

testCompile([
   libraries.junit,
   libraries.mockito

])

我在子模块的testCompile([行收到错误。可能导致这种情况的原因是什么?

1 个答案:

答案 0 :(得分:1)

以下脚本适用于gradle 2.8:

apply plugin: 'java'

ext.libraries = [

   junit: 'junit:junit:4.12',
   mockito: 'org.mockito:mockito-all:1.9.5'

]

repositories {
  mavenCentral()
}

dependencies {
  testCompile(
    [
      libraries.junit,
      libraries.mockito
    ]
  )
}

在这里查看demo