Hello. Pardon if this has already been asked. But ...
# gradle
m
Hello. Pardon if this has already been asked. But I just introduced
buildSrc
with
*.gradle.kts
files in a multi-module Android project. I notice that with this change, the Kotlin Gradle DSL seems to be overriding the
kotlin-stdlib
to
1.5.31
for my actual project even though the project declares the following in the root
build.gradle
Copy code
dependencies {
        …
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
The
buildSrc/build.gradle.kts
file is registering the
kotlin-dsl
plugin and declares the following in the
dependencies
block:
Copy code
dependencies {
  …
  implementation(kotlin("gradle-plugin"))
}
e
Gradle uses its own embedded version of Groovy and Kotlin for its build scripts, https://github.com/gradle/gradle/issues/17375
inside buildSrc (or anywhere that gets loaded into buildscript classpath or with `plugins {
kotlin-dsl
}`) you should be using `plugins {
embedded-kotlin
}` or possibly
plugins { kotlin("jvm") version embeddedKotlinVersion }
I use
settings.gradle
Copy code
pluginManagement {
    plugins {
        resolutionStrategy {
            eachPlugin {
                if (requested.id.id.startsWith("org.jetbrains.kotlin.")) useVersion("1.6.10")
            }
        }
    }
}
to get my projects to build with Kotlin 1.6.10, although there's other ways of achieving the same result
but it's normal that kotlin version used to compile buildSrc differs from the kotlin version used to compile the root project… even though it can look confusing
m
Thank you so much for your reply! Sorry, but I'm not sure I fully follow since I am not well-versed with Gradle. Ultimately, my goal is to provide the following within `buildSrc/src/main/kotlin`:
kotlin-library.gradle.kts
Copy code
plugins {
  kotlin("jvm")
  // additional plugins
}
kotlin-android-library.gradle.kts
Copy code
plugins {
  kotlin("android")
  id("com.android.library")
  // additional plugins
}
The goal is so that a pure JVM Kotlin module can simply apply
Copy code
plugins {
    id 'kotlin-library'
}
And an Android Kotlin library module can simply apply
Copy code
plugins {
    id 'kotlin-android-library'
}
Regarding the
// additional plugins
section, these would be linters such as
Detekt
and
Spotless
.
I think I may have solved my issue, or at least provided a workaround. In my
buildSrc/build.gradle.kts
, I had the following:
Copy code
dependencies {
  …
  implementation(kotlin("gradle-plugin"))
}
I then changed it to
Copy code
dependencies {
  …
  implementation(kotlin(module = "gradle-plugin", version = "1.6.10"))
}
And now my project is building with
kotlin-stdlib
1.6.10
. 🤷‍♂️