Android Studio - Evaluate Expression: Cannot resolve constructor 'new groovy.lang.GroovyClassLoader()'

This error comes up when trying to debug gradle (groovy) build script which needs groovy library to be in the debugger runtime classpath and it should be at the module/project level dependency (since it deals with gradle groovy build scripts debugging) and not at the module’s source dependency level.

As a work around, in IntelliJ we used to add the groovy lib to the project level dependency as “compile-only” in the project-structure window.

But in Android studio these granular settings are no longer available, instead we have options to add new modules by creating new and not by adding some library dependency to the module classpath dependencies list.

This is extremely frustrating when trying to debug the android gradle(groovy) build scripts dealing custom parts designed for re-useability in the android gradle build.

Please suggest a proper solution if it exists already, else suggest a clean workaround with which we can work peacefully.

I’d say you should report to your IDE about this error. This does not sound like it is a Gradle problem, but purely about your IDE.

As AS is basically a specialized IJ, I wonder that you cannot do the same settings anymore. You could probably do the settings still by changing the IDE files on disk if it really was removed from the UI. Or you could use IntelliJ for that build script debugging instead.

Since AS uses AGP (Android Gradle plugin) on top of gradle dist, is AGP owned by gradle team since it is the interface to gradle distribution similar to java plugin for gradle?

apply plugin: 'android'
or 
apply plugin: 'com.android.application'

No, the Gradle folks are not involved with AGP at all, AGP is developed by Google folks.
And AS does not “use AGP on top of Gradle dist”.
Your build uses plugins from the AGP suite.
The IDE just has support for those and can work with them.
But AGP is not even the point here, is it?
You debug a Gradle build and try to evaluate an expression in the debugger and that fails.
That is most probably an IDE bug, as I said.

Sure thanks, I will take it up with jetbrains IDE team later.

1 Like

For posterity,

Seems an issue is being tracked since 5-years with no advancements or support assigned.
IDEA-204051

https://youtrack.jetbrains.com/issue/IDEA-204051/gradle-debugger-cannot-evaluate-Cannot-resolve-constructor-new-groovy.lang.GroovyClassLoader

Found the solution

just now tested, that “Evaluate Expression” works (with groovy as lang for *.gradle scripts) if we add a project/module level dependency on groovy lib. (can be compileOnly or runtimeOnly, or its supersets, cannot be on annotationProcessor or its supersets though)

NOTE: However the appropriate (compile/runtime)classpath will be polluted with this groovy lib even though if it is undesired. So it is preferred to use it in compileOnly configuration.

Tested to be working on both IDEA and AndroidStudio (ie, gradle and android-gradle-plugin).

NOTE: adding groovy to buildscript dependencies via classpath doesn’t help/work.

IDEA:

Android-Studio:

The workarounds mentioned in that ticket should also work then. So either using Java as expression language, or adding Groovy to the dependencies but just for the IDE, not in Gradle. For the latter you simply was at the wrong settings dialog.