I´m trying to migrate my android project to using gradle kotlin dsl, replacing all build.gradle files with build.gradle.kts files and using kotlin there. Already before, I used to have a kotlin file containing object elements with library and version constants (in buildSrc -> src -> main -> kotlin), like:
object Versions {
const val anyLibVersion = "1.0.0"
}
object Lib {
const val anyLib = "x:y:${Versions.anyLibVersion}"
}
In build.gradle files, I can access these constants without problems, but as soon as I switch them to build.gradle.kts, it cannot resolve them anymore. Any explaination for that?
What you'd typically have is following in buildSrc/build.gradle.kts
import org.gradle.kotlin.dsl.`kotlin-dsl`
repositories {
jcenter()
}
plugins {
`kotlin-dsl`
}
and then have your versions/dependencies in say buildSrc/src/main/java/Dependencies.kt
You can fix it by completely removing the android {} block, doing a Gradle sync, and then adding the code block back.
Does NOT treat the buildSrc directory as an module in settings.gradle.kts (.)
Then for apply from
in Kotlin DSL, you should use plugins {}
plugins {
`java-gradle-plugin`
`kotlin-dsl`
`kotlin-dsl-precompiled-script-plugins`
}
repositories {
mavenCentral()
}
implementation-class=gradlePlugins.android.AndroidLibraryPlugin
Pay attention to the package of implementation-class will be "some-package-id"
package gradlePlugins.android
import org.gradle.api.Plugin
import org.gradle.api.Project
class AndroidLibraryPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.configurePlugins()
project.configureAndroid()
project.configureDependencies()
}
}
Check that "some-package-id" will be the file name of some-package-id.properties
plugins {
id("some-package-id")
}
GL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With