Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Compilation Error with Kotlin DSL configuring Java Spec

I'm attempting to create a build file for a Kotlin project that will sometimes include java sources. In the past with the Groovy based build files in a multi-project build, I could specify the sourceCompatibility in the subproject block with no issue. With the Kotlin DSL I know it must be in the java block to configure with the Kotlin DSL, but when I attempt to do that from the subproject block in my root build.gradle.kts file I get a compilation error:

Script compilation errors:

Line 14:     java {
           ^ Expression 'java' cannot be invoked as a function. The function 'invoke()' is not found

Line 14:     java {
           ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
               public val PluginDependenciesSpec.java: PluginDependencySpec defined in org.gradle.kotlin.dsl

Line 15:         sourceCompatibility = JavaVersion.VERSION_1_8
               ^ Unresolved reference: sourceCompatibility

3 errors

I've included a gist to the gradle build file I'm using. Now I can get it working if I specify the java block in one of the subprojects build.gradle.kts files, but I want the setting applied to all of the subprojects, not just specific projects.

like image 679
PatTheGamer Avatar asked Dec 02 '18 06:12

PatTheGamer


People also ask

What is the Kotlin Gradle DSL?

Gradle's Kotlin DSL provides an alternative syntax to the traditional Groovy DSL with an enhanced editing experience in supported IDEs, with superior content assist, refactoring, documentation, and more.

What is DSL in Gradle?

Simply, it stands for 'Domain Specific Language'. IMO, in gradle context, DSL gives you a gradle specific way to form your build scripts. More precisely, it's a plugin-based build system that defines a way of setting up your build script using (mainly) building blocks defined in various plugins.

What is Kotlin DSL?

The Kotlin DSL provides built-in support for three destination types: Fragment , Activity , and NavGraph destinations, each of which has its own inline extension function available for building and configuring the destination.


1 Answers

You can just use

configure<JavaPluginExtension> { ... }
like image 195
JB Nizet Avatar answered Sep 28 '22 08:09

JB Nizet