12

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?

Lemao1981
  • 2,225
  • 5
  • 18
  • 30

3 Answers3

5

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

John O'Reilly
  • 10,000
  • 4
  • 41
  • 63
  • My buildSrc/build.gradle.kts looks exactly like that. So you are saying the mistake is that I´m using kotlin source folder instead of java source folder? I actually had that before, but thought using kotlin source folder is more appropriate. Shouldn´t it work with both? – Lemao1981 Oct 28 '18 at 11:52
  • It certainly works with `java`....haven't tried tbh using `kotlin`.....will test here in a while and see if that works (or whether for example you might need to explicitly set `sourceSets` in gradle to use) – John O'Reilly Oct 28 '18 at 12:10
  • 1
    Tried using `kotlin` here instead of `java` for folder name and looks like it's working fine. – John O'Reilly Oct 28 '18 at 12:46
  • Hm, then anything must be messed up in my project.. Thanks for the effort! – Lemao1981 Oct 28 '18 at 13:06
  • 1
    Is working now after reinstallation of android studio – Lemao1981 Oct 31 '18 at 22:31
3

Does NOT treat the buildSrc directory as an module in settings.gradle.kts (.)


Precompiled script plugins.

Then for apply from in Kotlin DSL, you should use plugins {}

build.gradle.kts (X:buildSrc)

plugins {
    `java-gradle-plugin`
    `kotlin-dsl`
    `kotlin-dsl-precompiled-script-plugins`
}

repositories {
    mavenCentral()
}

Precondition

buildSrc/src/resources/META-INF/gradlePlugins/some-package-id.properties file:

implementation-class=gradlePlugins.android.AndroidLibraryPlugin

buildSrc/src/main/kotlin/gradlePlugins/android/AndroidLibraryPlugin.kt

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()
    }

}

Consume the plugin:

Check that "some-package-id" will be the file name of some-package-id.properties

feature.gradle.kts

plugins {
    id("some-package-id")
}

GL

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
  • `Caused by: org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'core-library'] was not found in any of the following sources` – Dr.jacky Apr 12 '22 at 15:26
  • check the updated answer – Braian Coronel Apr 13 '22 at 01:26
  • In the `feature.gradle.kts`: `Plugin [id: 'some-package-id'] was not found in any of the following sources:` – Dr.jacky Apr 13 '22 at 10:16
  • Seems it works! I had to put the `core-library.gradle.kts` under `buildSrc/src/main/kotlin/` and NOT under a `buildSrc/src/main/kotlin/sub-folder/script/blah`. Although, this solution doesn't work when you have `id("dagger.hilt.android.plugin")` in your `core-library.gradle.kts`. Not sure why! – Dr.jacky Apr 13 '22 at 10:39
  • Check updated answer. First, you can test only the plugin consume and then integrate dagger with their preconditions. The core-library.gradle.kts file should be outside the buildSrc module – Braian Coronel Apr 13 '22 at 14:32
  • precompiled and binary scripts are different things and you answered like it the same thing – user924 Jul 15 '22 at 12:00
  • which would be the change? – Braian Coronel Jul 15 '22 at 16:21
3

You can fix it by completely removing the android {} block, doing a Gradle sync, and then adding the code block back.

Antoine
  • 307
  • 2
  • 13