Search code examples
androidgradleandroid-gradle-plugingradle-plugingradle-kotlin-dsl

Gradle cannot resolve `java` in composite build + version catalog


I am in the process of creating a minimal example of gradle version cataloge plus build logic defined in a composite build. The basic setup is working so far. But for some reason I cannot gradle to recognize the java expression within my first build-logic plugin my.android-commons.gradle.kts.

java not found in gradle file

I do not understand why, because the java plugin is declared and should be sufficient for gradle to find java, right?

This is my current file tree:

/root
- /app
- /core
- /feature

- /build-logic
--- /android-commons
---- /src/main/kotlin
----- my.android-commons.gradle.kts (5)
--- build.gradle.kts (4)
- settings.gradle.kts (3)

- /gradle
-- libs.versions.toml

- build.gradle.kts (2)
- settings.gradle.kts (1)

Here is the content of the important files:

(1) /root/settings.gradle.kts

// == Define locations for build logic ==
pluginManagement {
    includeBuild("build-logic")
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

// == Define locations for components ==
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
include("core")
include("feature")

// == Define the inner structure of this component ==
rootProject.name = "Session Timer"
include("app")

(2) /root/build.gradle.kts

plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.kotlin.android) apply false
    alias(libs.plugins.android.library) apply false
}

(3) ../build-logic/settings.gradle.kts

dependencyResolutionManagement {
    repositories {
        gradlePluginPortal()
        google()
    }
    versionCatalogs {
        create("libs") {
            from(files("../gradle/libs.versions.toml"))
        }
    }
}

rootProject.name = "build-logic"
include("android-commons")

(4) ../build-logic/android-commons/build.gradle.kts

plugins {
    `kotlin-dsl`
}

(5) ../build-logic/android-commons/src/../my.android-commons.gradle.kts

plugins {
    id("java")
}

java { // not found by gradle
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

This is the link to the project repo: https://github.com/muetzenflo/sessiontimer.git


Solution

  • I reviewed your project and provided a solution as follows.

    off.android-commons.gradle.kts

    fun Project.configureJava() {
        java {
            toolchain {
                languageVersion.set(JavaLanguageVersion.of(17))
            }
        }
    }
    
    fun Project.java(action: JavaPluginExtension.() -> Unit) = extensions.configure<JavaPluginExtension>(action)
    
    fun Project.configureKotlin() {
        // Configure Java to use our chosen language level. Kotlin will automatically pick this up
        configureJava()
    }
    
    class KotlinAndroidConventionPlugin : Plugin<Project> {
        override fun apply(target: Project) {
            with(target) {
                with(pluginManager) {
                    apply("org.jetbrains.kotlin.android")
                }
    
                configureKotlin()
            }
        }
    }
    

    build.gradle.kts
    android-commons level

    plugins {
        `kotlin-dsl`
    }
    
    repositories {
        mavenCentral()
    }
    
    gradlePlugin{
        plugins {
            register("kotlinAndroid") {
                id = "app.sessiontimer.kotlin.android"
                implementationClass = "Off_android_commons_gradle.KotlinAndroidConventionPlugin"
            }
        }
    }