From c2631095374c5bad867aa5fddbdcb44c7a6c51a2 Mon Sep 17 00:00:00 2001 From: LooKeR Date: Wed, 10 Jul 2024 00:42:15 +0530 Subject: [PATCH] Add jvm library plugin --- build-logic/structure/build.gradle.kts | 13 +++-- .../main/kotlin/AndroidApplicationPlugin.kt | 7 ++- .../src/main/kotlin/AndroidLibraryPlugin.kt | 11 ++-- .../com/looker/droidify/JvmLibraryPlugin.kt | 16 ++++++ .../com/looker/droidify/KotlinAndroid.kt | 57 ++++++++++++------- .../com/looker/droidify/ProjectExtensions.kt | 7 ++- gradle/libs.versions.toml | 1 + 7 files changed, 75 insertions(+), 37 deletions(-) create mode 100644 build-logic/structure/src/main/kotlin/com/looker/droidify/JvmLibraryPlugin.kt diff --git a/build-logic/structure/build.gradle.kts b/build-logic/structure/build.gradle.kts index dac79a4ba..1617fdedf 100644 --- a/build-logic/structure/build.gradle.kts +++ b/build-logic/structure/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.jetbrains.kotlin.gradle.dsl.JvmTarget plugins { `kotlin-dsl` @@ -10,9 +10,10 @@ java { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } -tasks.withType().configureEach { - kotlinOptions { - jvmTarget = JavaVersion.VERSION_17.toString() + +kotlin { + compilerOptions { + jvmTarget = JvmTarget.JVM_17 } } @@ -53,5 +54,9 @@ gradlePlugin { id = "looker.android.library" implementationClass = "AndroidLibraryPlugin" } + register("jvmLibraryPlugin") { + id = "looker.jvm.library" + implementationClass = "JvmLibraryPlugin" + } } } diff --git a/build-logic/structure/src/main/kotlin/AndroidApplicationPlugin.kt b/build-logic/structure/src/main/kotlin/AndroidApplicationPlugin.kt index f690b2585..648c23765 100644 --- a/build-logic/structure/src/main/kotlin/AndroidApplicationPlugin.kt +++ b/build-logic/structure/src/main/kotlin/AndroidApplicationPlugin.kt @@ -1,10 +1,11 @@ import com.android.build.api.dsl.ApplicationExtension import com.looker.droidify.configureKotlinAndroid +import com.looker.droidify.kotlin2 +import com.looker.droidify.libs import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.dependencies -import org.gradle.kotlin.dsl.embeddedKotlin class AndroidApplicationPlugin : Plugin { override fun apply(target: Project) { @@ -25,8 +26,8 @@ class AndroidApplicationPlugin : Plugin { } } dependencies { - add("implementation", embeddedKotlin("stdlib")) - add("implementation", embeddedKotlin("reflect")) + add("implementation", kotlin2("stdlib", libs)) + add("implementation", kotlin2("reflect", libs)) } } } diff --git a/build-logic/structure/src/main/kotlin/AndroidLibraryPlugin.kt b/build-logic/structure/src/main/kotlin/AndroidLibraryPlugin.kt index 3235039fb..bc3294a1c 100644 --- a/build-logic/structure/src/main/kotlin/AndroidLibraryPlugin.kt +++ b/build-logic/structure/src/main/kotlin/AndroidLibraryPlugin.kt @@ -1,11 +1,12 @@ import com.android.build.api.variant.LibraryAndroidComponentsExtension import com.android.build.gradle.LibraryExtension import com.looker.droidify.configureKotlinAndroid +import com.looker.droidify.kotlin2 +import com.looker.droidify.libs import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.dependencies -import org.gradle.kotlin.dsl.embeddedKotlin class AndroidLibraryPlugin : Plugin { override fun apply(target: Project) { @@ -39,10 +40,10 @@ class AndroidLibraryPlugin : Plugin { } } dependencies { - add("implementation", embeddedKotlin("stdlib")) - add("implementation", embeddedKotlin("reflect")) - add("testImplementation", embeddedKotlin("test")) - add("androidTestImplementation", embeddedKotlin("test")) + add("implementation", kotlin2("stdlib", libs)) + add("implementation", kotlin2("reflect", libs)) + add("testImplementation", kotlin2("test", libs)) + add("androidTestImplementation", kotlin2("test", libs)) } } } diff --git a/build-logic/structure/src/main/kotlin/com/looker/droidify/JvmLibraryPlugin.kt b/build-logic/structure/src/main/kotlin/com/looker/droidify/JvmLibraryPlugin.kt new file mode 100644 index 000000000..d258c95a7 --- /dev/null +++ b/build-logic/structure/src/main/kotlin/com/looker/droidify/JvmLibraryPlugin.kt @@ -0,0 +1,16 @@ +package com.looker.droidify + +import org.gradle.api.Plugin +import org.gradle.api.Project + +class JvmLibraryPlugin : Plugin { + override fun apply(target: Project) { + with(target) { + with(pluginManager) { + apply("org.jetbrains.kotlin.jvm") + apply("looker.lint") + } + configureKotlinJvm() + } + } +} diff --git a/build-logic/structure/src/main/kotlin/com/looker/droidify/KotlinAndroid.kt b/build-logic/structure/src/main/kotlin/com/looker/droidify/KotlinAndroid.kt index e0a95497b..2898bc615 100644 --- a/build-logic/structure/src/main/kotlin/com/looker/droidify/KotlinAndroid.kt +++ b/build-logic/structure/src/main/kotlin/com/looker/droidify/KotlinAndroid.kt @@ -4,10 +4,15 @@ import DefaultConfig import com.android.build.api.dsl.CommonExtension import org.gradle.api.JavaVersion import org.gradle.api.Project +import org.gradle.api.plugins.JavaPluginExtension +import org.gradle.kotlin.dsl.assign +import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.dependencies -import org.gradle.kotlin.dsl.provideDelegate -import org.gradle.kotlin.dsl.withType -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension +import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension +import org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension +import org.jetbrains.kotlin.gradle.dsl.KotlinVersion // Taken from NIA sample app by Google @@ -34,33 +39,41 @@ internal fun Project.configureKotlinAndroid( } } - configureKotlin() + configureKotlin() dependencies { add("coreLibraryDesugaring", libs.getLibrary("android.desugarJdkLibs")) } } +internal fun Project.configureKotlinJvm() { + extensions.configure { + // Up to Java 11 APIs are available through desugaring + // https://developer.android.com/studio/write/java11-minimal-support-table + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + + configureKotlin() +} + /** * Configure base Kotlin options */ -private fun Project.configureKotlin() { - // Use withType to workaround https://youtrack.jetbrains.com/issue/KT-55947 - tasks.withType().configureEach { - kotlinOptions { - // Set JVM target to 11 - jvmTarget = JavaVersion.VERSION_11.toString() - // Treat all Kotlin warnings as errors (disabled by default) - // Override by setting warningsAsErrors=true in your ~/.gradle/gradle.properties - val warningsAsErrors: String? by project - allWarningsAsErrors = warningsAsErrors.toBoolean() - freeCompilerArgs = freeCompilerArgs + listOf( - "-opt-in=kotlin.RequiresOptIn", - // Enable experimental coroutines APIs, including Flow - "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi", - "-opt-in=kotlinx.coroutines.FlowPreview", - "-Xcontext-receivers" - ) - } +private inline fun Project.configureKotlin() = configure { + when (this) { + is KotlinAndroidProjectExtension -> compilerOptions + is KotlinJvmProjectExtension -> compilerOptions + else -> TODO("Unsupported project extension $this ${T::class}") + }.apply { + apiVersion = KotlinVersion.KOTLIN_2_0 + jvmTarget = JvmTarget.JVM_11 + freeCompilerArgs = listOf( + "-opt-in=kotlin.RequiresOptIn", + // Enable experimental coroutines APIs, including Flow + "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi", + "-opt-in=kotlinx.coroutines.FlowPreview", + "-Xcontext-receivers" + ) } } diff --git a/build-logic/structure/src/main/kotlin/com/looker/droidify/ProjectExtensions.kt b/build-logic/structure/src/main/kotlin/com/looker/droidify/ProjectExtensions.kt index 721b8969c..f76d6c55a 100644 --- a/build-logic/structure/src/main/kotlin/com/looker/droidify/ProjectExtensions.kt +++ b/build-logic/structure/src/main/kotlin/com/looker/droidify/ProjectExtensions.kt @@ -4,9 +4,10 @@ import org.gradle.api.Project import org.gradle.api.artifacts.MinimalExternalModuleDependency import org.gradle.api.artifacts.VersionCatalog import org.gradle.api.artifacts.VersionCatalogsExtension +import org.gradle.api.artifacts.dsl.DependencyHandler import org.gradle.api.provider.Provider import org.gradle.kotlin.dsl.getByType -import org.gradle.plugin.use.PluginDependency +import org.gradle.kotlin.dsl.kotlin val Project.libs get(): VersionCatalog = extensions.getByType().named("libs") @@ -14,5 +15,5 @@ val Project.libs fun VersionCatalog.getLibrary(alias: String): Provider = findLibrary(alias).get() -fun VersionCatalog.getPlugin(alias: String): Provider = - findPlugin(alias).get() +fun DependencyHandler.kotlin2(module: String, catalog: VersionCatalog): Any = + kotlin(module, version = catalog.findVersion("kotlin").get().strictVersion) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3cfa022e6..126003899 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -108,6 +108,7 @@ ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } # Plugins defined by this project looker-android-application = { id = "looker.android.application", version = "unspecified" } looker-android-library = { id = "looker.android.library", version = "unspecified" } +looker-jvm-library = { id = "looker.jvm.library", version = "unspecified" } looker-hilt = { id = "looker.hilt", version = "unspecified" } looker-hilt-work = { id = "looker.hilt.work", version = "unspecified" } looker-lint = { id = "looker.lint", version = "unspecified" }