Gradle - Cannot set property on null object

6k Views Asked by At

I have a .gradle in which I want to set some project properties if a condition is true.

def isRelease = project.getProperty('isRelease')

if (isRelease) {
    println 'Detected a release'
    project.properties.'releaseCenter'.'uploadURL' = project.properties.'uatUploadURL'
}

The output is:

A problem occurred evaluating root project 'tasks'.
> Cannot set property 'uploadURL' on null object

I think it has to do with the '' around the object name but I couldn't get it to work. Any help is much appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

You call project.properties: https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#getProperties--

which returns a Map. You then call properties.'releaseCenter' which is equivalent to doing properties.get("releaseCenter") which returns null. You try to get a property on a null object which is exactly the error you are seeing.

Possible solutions:

  1. Fix your Gradle configuration (some plugin may be adding this property)
  2. Define the property yourself.