Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 359
Rajkumar Natarajan Avatar asked Dec 23 '19 16:12

Rajkumar Natarajan


People also ask

What is subprojects in Gradle?

The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency.

How do I add a dependency in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.


2 Answers

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)

like image 160
Sylhare Avatar answered Nov 13 '22 23:11

Sylhare


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"))
    }
  }
like image 40
Rajkumar Natarajan Avatar answered Nov 14 '22 00:11

Rajkumar Natarajan