gradlemulti-module

Gradle: Share version defined in buildSrc in pluginManagement/dependencies/


I'm looking for factorizing all plugins/dependencies version used in my multimodule project. I tried different approach. For instance, I tried that one :

I created a buildSrc an object containing all versions exposed as constants (in ./buildSrc/main/kotlin/Versions.kt) :

object Versions {
    const val myApp = "1.0.12"

    const val kotlin = "1.8.20"
    const val agp = "7.1.3"
    const val compose = "1.4.0"
    const val ktor = "2.2.4"
    const val jUnit = "5.9.0"
}

I added plugin management in my root project as follow (in ./settings.gradle.kts) :

pluginManagement {
    repositories {
        gradlePluginPortal()
        maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
        google()
        mavenCentral()
    }

    plugins {
        kotlin("jvm").version(Versions.kotlin)
        kotlin("plugin.serialization").version(Versions.kotlin)
        kotlin("multiplatform").version(Versions.kotlin)
        kotlin("android").version(Versions.kotlin)
        id("com.android.tools.build").version(Versions.agp)
        id("com.android.base").version(Versions.agp)
        id("com.android.application").version(Versions.agp)
        id("com.android.library").version(Versions.agp)
        id("org.jetbrains.kotlin").version(Versions.kotlin)
        id("org.jetbrains.compose").version(Versions.compose)

        id("com.squareup.sqldelight").version(Versions.sqlDelight)
    }

}

It seems that can't work as I have the following error :

e: D:\src\project\settings.gradle.kts:12:31: Unresolved reference: Versions
e: D:\src\project\settings.gradle.kts:13:48: Unresolved reference: Versions
...

* What went wrong:
Script compilation errors:

  Line 12:         kotlin("jvm").version(Versions.kotlin)
                                         ^ Unresolved reference: Versions

  Line 13:         kotlin("plugin.serialization").version(Versions.kotlin)
                                                          ^ Unresolved reference: Versions

This approach seems bad, or I missed something something. Can you help to fix or give me the standard approach ?


Solution

  • The best approach I found is the one explained in the documentation that I starting to be comfortable with :)

    in the settings.gradle.kts of the root project, I added a catalog as following :

    dependencyResolutionManagement {
      versionCatalogs {
        create("libs") {
          version("ktor", "2.2.4")
          version("nd4j", "1.0.0-M2.1")
          version("kotlin", "1.8.20")
    
          library("kotlin-logging-jvm", "io.github.oshai:kotlin-logging-jvm:4.0.0-beta-23")
          library("logback-classic", "ch.qos.logback:logback-classic:1.4.5")
          library("log4j-to-slf4j", "org.apache.logging.log4j:log4j-to-slf4j:2.19.0")
          bundle("logging", listOf(
              "kotlin-logging-jvm",
              "logback-classic",
              "log4j-to-slf4j",
          ))
    
          library("ktor-serialization-gson-jvm", "io.ktor", "ktor-serialization-gson-jvm").versionRef("ktor")
          ....
          bundle("ktorServer", listOf(
              "ktor-server-content-negotiation-jvm",
              "ktor-server-core-jvm",
              "ktor-server-cors",
              "ktor-serialization-gson-jvm",
              "ktor-server-core",
              "ktor-server",
              "ktor-server-cio",
              "freemarker",
              "ktor-server-freemarker",
          ))
    
          library("ktor-client-cio", "io.ktor", "ktor-client-cio").versionRef("ktor")
          ...
          bundle("ktorClient", listOf(
              "ktor-client-cio",
              "ktor-server-netty",
              "ktor-client-content-negotiation",
              "ktor-serialization-kotlinx-json",
              "ktor-serialization-gson",
              "ktor-client-logging",
              "ktor-client-auth",
              "ktor-client-logging",
          ))
    
          library("jmimemagic", "net.sf.jmimemagic:jmimemagic:0.1.5")
        }
      }
    }
    
    

    And so in my submodule, I just have to add a bundle or single dependency like that in build.gradle.kts

    dependencies {
      implementation(libs.bundles.logging)
      implementation(libs.bundles.ktorServer)
      implementation(libs.bundles.ktorClient)
      implementation(libs.jmimemagic)
    }