14

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.

user51
  • 8,843
  • 21
  • 79
  • 158

4 Answers4

22

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)

Sylhare
  • 5,907
  • 8
  • 64
  • 80
  • 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? – Pastafarian Jan 15 '21 at 22:37
  • 1
    It was at the time a limitation from the gradle [documentation](https://docs.gradle.org/current/userguide/kotlin_dsl.html?_ga=2.52221289.1756720240.1610986438-823552276.1554568232#type-safe-accessors). To apply the plugin to all projects you could try `allprojects{ kotlin("jvm") version "1.3.50" }` – Sylhare Jan 18 '21 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 – Maciej Tułaza Jun 06 '22 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"))
    }
  }
Sylhare
  • 5,907
  • 8
  • 64
  • 80
user51
  • 8,843
  • 21
  • 79
  • 158
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")
    }
}
Adrian Witaszak
  • 121
  • 1
  • 5
0

This is the way to do it:

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

allprojects {
    apply(plugin = "org.jetbrains.kotlin.jvm")
}
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216