I want to split my project into subprojects. The default Gradle setting from the IntelliJ IDE is:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.3.50"
}
group = "project"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
That setting compiles. ButI don't want repeat that code in every subproject. So I changed the build.gradle.kts to
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
subprojects {
plugins {
kotlin("jvm") version "1.3.50"
}
group = "project"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
}
but I get the exception:
e: C:[...]\build.gradle.kts:1:12: Unresolved reference: jetbrains e: C:[...]\build.gradle.kts:16:9: Unresolved reference: implementation e: C:[...]\build.gradle.kts:19:20: Unresolved reference: KotlinCompile e: C:[...]\build.gradle.kts:19:35: Type mismatch: inferred type is () -> Unit but Class<TypeVariable(S)!>! was expected e: C:[...]\build.gradle.kts:20:9: Unresolved reference: kotlinOptions
FAILURE: Build failed with an exception.
Where: Build file 'C:[...]\build.gradle.kts' line: 1
What went wrong: Script compilation errors:
Line 01: import org.jetbrains.kotlin.gradle.tasks.KotlinCompile ^ Unresolved reference: jetbrains
Line 16: implementation(kotlin("stdlib-jdk8")) ^ Unresolved reference: implementation
Line 19: tasks.withType { ^ Unresolved reference: KotlinCompile
Line 19: tasks.withType { ^ Type mismatch: inferred type is () -> Unit but Class<TypeVariable(S)!>! was expected
Line 20: kotlinOptions.jvmTarget = "1.8" ^ Unresolved reference: kotlinOptions
5 errors
Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
BUILD FAILED in 1s
I think there is an easy syntax error, but I can't find it.
If you apply them with apply { plugin (...) } instead, you may encounter unresolved references to the extensions generated by Gradle Kotlin DSL. To resolve that, you can comment out the erroneous usages, run the Gradle task kotlinDslAccessorsSnapshot, then uncomment the usages back and rerun the build or reimport the project into the IDE.
To conclude, the unresolved reference error happens when Kotlin has no idea what the keyword you’re typing in the file points to. It may be a function, a variable, or another construct of the language that you’ve yet to declare in the code.
In order to build a Kotlin project with Gradle, you should apply the Kotlin Gradle plugin to your project and configure the dependencies. Apply the Kotlin Gradle plugin by using the Gradle plugins DSL.
The Kotlin Gradle plugin supports incremental compilation. Incremental compilation tracks changes of source files between builds so only files affected by these changes would be compiled. Incremental compilation is supported for Kotlin/JVM and Kotlin/JS projects and is enabled by default.
Not sure how you're not also getting an error by nesting the plugins { }
block under subprojects { }
As stated in Limitations of the plugins DSL:
The
plugins {}
block must also be a top level statement in the buildscript. It cannot be nested inside another construct (e.g. an if-statement or for-loop).
So to fix your issues, move the plugins {}
to the top and imperatively apply the plugins in the subprojects {}
block:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.3.50" apply false
}
subprojects {
apply {
plugin("org.jetbrains.kotlin.jvm")
}
group = "project"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
val implementation by configurations
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
}
You can read more about the apply false
part in the Method details of PluginDependenciesSpec
which is the type/scope of the plugins {}
block.
You can read more about the val implementation by configurations
part in the Understanding what to do when type-safe model accessors are not available
Is important to define the repositories in settings.gradle.kts (project)
so that the dependencies of the subprojects are recognized.
pluginManagement {
repositories {
maven("https://dl.bintray.com/kotlin/kotlin-eap")
mavenCentral()
maven("https://plugins.gradle.org/m2/")
}
}
rootProject.name = "project"
include("app")
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
java
kotlin("jvm") version "1.4-M3" apply false
}
subprojects {
apply {
plugin("org.jetbrains.kotlin.jvm")
}
repositories {
maven("https://dl.bintray.com/kotlin/kotlin-eap")
mavenCentral()
}
val implementation by configurations
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
}
To define plugins for each submodule separately use the lambda apply { plugin("pluginId") }
apply {
plugin("org.jetbrains.kotlin.jvm")
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
GL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With