Search code examples
gradlegradle-kotlin-dsl

gradle kotlin dsl - How to configure dependencies for subprojects


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.


Solution

  • 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)