Caused by: org.gradle.api.invalidusercodeexception: cannot run project.afterevaluate(closure) when the project is already evaluated.

Explanation:

The error message you are seeing is related to a specific exception in Gradle called InvalidUserCodeException. The exception is triggered when you try to run the project.afterEvaluate closure but the project has already been evaluated.

Gradle allows you to define tasks and configurations for your project in a build script. The build script is evaluated and executed in a specific order. The project.afterEvaluate closure is used to define actions that should be executed after the evaluation of the build script.

However, if you mistakenly try to run the project.afterEvaluate closure when the project has already been evaluated, Gradle will throw the InvalidUserCodeException.

To resolve this issue, you can double-check the code that is triggering the project.afterEvaluate closure. Make sure it is placed in the correct location and is not called multiple times. Also, ensure that the closure is not enclosed within a conditional statement that might evaluate multiple times during the build process.

Here is an example of a correct usage of the project.afterEvaluate closure:

<root>
  <buildscript>
    <dependencies>
      <!-- Dependencies for Gradle plugins -->
    </dependencies>
  </buildscript>
  
  <plugins>
    <!-- Apply necessary plugins -->
  </plugins>

  <task name="myTask" dependsOn="otherTask">
    <!-- Define your task -->
  </task>

  <project>
    <!-- Other Gradle configurations -->
  </project>

  <project.afterEvaluate>
    <!-- Actions to be executed after project evaluation -->
  </project.afterEvaluate>
</root>

In the above example, the project.afterEvaluate closure is placed at the end of the build script, ensuring that it is called after the evaluation of all configurations, tasks, and plugins.

Related Post

Leave a comment