How to activate type checking for all Groovy classes

To activate type checking for all your Groovy classes, you can add the Groovy compilation configuration from this article to your compileGroovy Gradle task. Doing that is equivalent to annotating each class with the @TypeChecked annotation.

apply plugin: 'groovy'

compileGroovy.groovyOptions.configurationScript = file('gradle/config/groovyc.groovy')

Add the file gradle/config/groovyc.groovy Groovy compiler configuration script relative to your build.gradle:

import groovy.transform.TypeChecked

withConfig(configuration) {

The configuration references a CompilerConfiguration instance. That’s how you‘ve declared an AST transformation compilation customizer to activate static type checking for all your Groovy files.

When you are using an undeclared variable, the compileGroovy task will throw an error like “[Static type checking] — The variable [naame] is undeclared” now, thus helping you to find typos, unmatched return types, wrong assignments, unavailable variables and methods, you name it.

You could also activate static compilation by default:

import groovy.transform.CompileStatic

withConfig(configuration) {

With @CompileStatic, Groovy tries to generate byte-code close to what javac would generate.