17

I'm creating gradle multi project for kotlin programming.

When I create dependencies under subprojects in main project build.gradle.kts I'm getting error Configuration with name 'implementation' not found.

Below is my configuration -

    plugins {
        kotlin("jvm") version "1.3.61" apply false
    }

    subprojects {   
        dependencies {
            val implementation by configurations
            implementation(kotlin("stdlib-jdk8"))
        }
    }

Once I move the plugins and dependencies into subproject build.gradle.kts then it is working fine. How can I make dependencies under subprojects work fine?

Code is on github.

4 Answers 4

25

With Kotlin dsl, you can add your dependencies as long as you use either apply(plugin = "org.jetbrains.kotlin.jvm") or apply(plugin = "java").

Those needs to be where you put your dependencies { .. }, usually inside a subprojects { .. }.

So here would be a simple build.gradle.kts that would propagate the kotlin dependency in all its subprojects.

plugins {
    kotlin("jvm") version "1.3.50"
}

repositories {
    mavenCentral()
}

subprojects {
   apply(plugin = "org.jetbrains.kotlin.jvm")

   dependencies {
      implementation(kotlin("stdlib-jdk8"))
   } 

   tasks.withType<KotlinCompile> {
       kotlinOptions {
           freeCompilerArgs = listOf("-Xjsr305=strict")
           jvmTarget = "11"
       }
   }

}

(You would still need to have the kotlin plugin, however no need to specify the version in the other subproject once defined at the root)

3
  • A simple question I'm hoping you can answer - why do we need to reapply the kotlin plugin at the subprojects level even after applying it in the root project level with the plugins block? Jan 15, 2021 at 22:37
  • 1
    It was at the time a limitation from the gradle documentation. To apply the plugin to all projects you could try allprojects{ kotlin("jvm") version "1.3.50" }
    – Sylhare
    Jan 18, 2021 at 16:17
  • 2
    this helped me after a few hours of trying to make it all running.. thanks @sylhare! however, I still can't put implementation without quotation marks because Gradle reports an error: "Unresolved reference: implementation"; to make it running I had to add val implementation by configurations before my dependencies { } declaration Jun 6, 2022 at 10:17
4

Adding the below configuration worked for me

  buildscript {
     repositories {
        maven {
        url = uri("https://plugins.gradle.org/m2/")
        }
     }
     dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61")
     }
  }

  subprojects {
    apply(plugin = "java")
    apply(plugin = "org.jetbrains.kotlin.jvm")

    dependencies {
        val implementation by configurations
        implementation(kotlin("stdlib-jdk8"))
    }
  }
1
  • 1
    At least for me, it doesn't need apply(plugin = "java")
    – silentorb
    Feb 11, 2020 at 4:23
0

In your root build.gradle.kts you can also exclude projects if you want:

subprojects {
    if (!project.name.contains("ios")) {
       apply("${rootDir}/ktlint.gradle.kts")
       apply(plugin = "org.jetbrains.kotlinx.kover")
       apply(plugin = "com.diffplug.spotless")
    }
}
-1

This is the way to do it:

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.7.20"
}

allprojects {
    apply(plugin = "org.jetbrains.kotlin.jvm")
}
1
  • The question is about declaring dependencies in subprojects. allprojects is therefore too wide.
    – saw303
    Nov 22, 2023 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.