Reputation: 2355
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?
Upvotes: 12
Views: 18625
Reputation: 22867
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
Upvotes: 3
Reputation: 305
You can fix it by completely removing the android {} block, doing a Gradle sync, and then adding the code block back.
Upvotes: 3
Reputation: 10330
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
Upvotes: 7