From 67ebd99e1fdb962042dd35d4a7859d0ab77f338a Mon Sep 17 00:00:00 2001 From: Yang Date: Thu, 31 Aug 2023 12:22:57 +1000 Subject: [PATCH] Dagger KSP. --- android/app/build.gradle.kts | 4 +- build-logic/build.gradle.kts | 6 +-- .../convention/KaptConventionPlugin.kt | 43 ------------------- .../convention/KspConventionPlugin.kt | 36 ++++++++++++++++ gradle.properties | 3 -- gradle/libs.versions.toml | 2 +- 6 files changed, 42 insertions(+), 52 deletions(-) delete mode 100644 build-logic/src/main/kotlin/io/github/reactivecircus/kstreamlined/buildlogic/convention/KaptConventionPlugin.kt create mode 100644 build-logic/src/main/kotlin/io/github/reactivecircus/kstreamlined/buildlogic/convention/KspConventionPlugin.kt diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 7577b7d9..94b7ea1c 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -16,7 +16,7 @@ import java.time.Instant plugins { id("kstreamlined.android.application") id("kstreamlined.android.application.compose") - id("kstreamlined.kapt") + id("kstreamlined.ksp") id("com.google.dagger.hilt.android") id("com.google.gms.google-services") apply false id("com.google.firebase.firebase-perf") @@ -243,7 +243,7 @@ dependencies { // Hilt implementation(libs.hilt.android) - kapt(libs.hilt.compiler) + ksp(libs.hilt.compiler) // LeakCanary debugImplementation(libs.leakcanary.android) diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index d67e52cb..a10d4647 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -120,9 +120,9 @@ gradlePlugin { id = "kstreamlined.kmm.test" implementationClass = "io.github.reactivecircus.kstreamlined.buildlogic.convention.KMMTestConventionPlugin" } - register("kapt") { - id = "kstreamlined.kapt" - implementationClass = "io.github.reactivecircus.kstreamlined.buildlogic.convention.KaptConventionPlugin" + register("ksp") { + id = "kstreamlined.ksp" + implementationClass = "io.github.reactivecircus.kstreamlined.buildlogic.convention.KspConventionPlugin" } } } diff --git a/build-logic/src/main/kotlin/io/github/reactivecircus/kstreamlined/buildlogic/convention/KaptConventionPlugin.kt b/build-logic/src/main/kotlin/io/github/reactivecircus/kstreamlined/buildlogic/convention/KaptConventionPlugin.kt deleted file mode 100644 index 85fdecf7..00000000 --- a/build-logic/src/main/kotlin/io/github/reactivecircus/kstreamlined/buildlogic/convention/KaptConventionPlugin.kt +++ /dev/null @@ -1,43 +0,0 @@ -package io.github.reactivecircus.kstreamlined.buildlogic.convention - -import io.github.reactivecircus.kstreamlined.buildlogic.isCiBuild -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.kotlin.dsl.configure -import org.jetbrains.kotlin.gradle.plugin.KaptExtension - -@Suppress("MagicNumber") -internal class KaptConventionPlugin : Plugin { - override fun apply(target: Project) = with(target) { - with(pluginManager) { - apply("org.jetbrains.kotlin.kapt") - } - - extensions.configure { - javacOptions { - if (hasHiltCompilerDependency) { - option("-Adagger.fastInit=enabled") - option("-Adagger.strictMultibindingValidation=enabled") - option("-Adagger.experimentalDaggerErrorMessages=enabled") - if (isCiBuild) { - option("-Xmaxerrs", 500) - } - } - } - } - - // disable kapt tasks for unit tests - tasks.matching { - it.name.startsWith("kapt") && it.name.endsWith("UnitTestKotlin") - }.configureEach { - enabled = false - } - } -} - -private val Project.hasHiltCompilerDependency: Boolean - get() = configurations.any { - it.dependencies.any { dependency -> - dependency.name == "hilt-compiler" - } - } diff --git a/build-logic/src/main/kotlin/io/github/reactivecircus/kstreamlined/buildlogic/convention/KspConventionPlugin.kt b/build-logic/src/main/kotlin/io/github/reactivecircus/kstreamlined/buildlogic/convention/KspConventionPlugin.kt new file mode 100644 index 00000000..57392ecf --- /dev/null +++ b/build-logic/src/main/kotlin/io/github/reactivecircus/kstreamlined/buildlogic/convention/KspConventionPlugin.kt @@ -0,0 +1,36 @@ +package io.github.reactivecircus.kstreamlined.buildlogic.convention + +import com.google.devtools.ksp.gradle.KspExtension +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.kotlin.dsl.configure + +internal class KspConventionPlugin : Plugin { + override fun apply(target: Project) = with(target) { + with(pluginManager) { + apply("com.google.devtools.ksp") + } + + extensions.configure { + if (hasHiltCompilerDependency) { + arg("dagger.fastInit", "enabled") + arg("dagger.strictMultibindingValidation", "enabled") + arg("dagger.experimentalDaggerErrorMessages", "enabled") + } + } + + // disable ksp tasks for unit tests + tasks.matching { + it.name.startsWith("ksp") && it.name.endsWith("UnitTestKotlin") + }.configureEach { + enabled = false + } + } +} + +private val Project.hasHiltCompilerDependency: Boolean + get() = configurations.any { + it.dependencies.any { dependency -> + dependency.name == "hilt-compiler" + } + } diff --git a/gradle.properties b/gradle.properties index 0a738eed..2151e067 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,9 +14,6 @@ kotlin.compiler.keepIncrementalCompilationCachesInMemory=true # try K2 compiler kotlin.experimental.tryK2=true -# Enable kapt compile avoidance -kapt.include.compile.classpath=false - # Enable AndroidX android.useAndroidX=true diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7106652d..6cb31387 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -13,7 +13,7 @@ fladle = "0.17.4" nativeCoroutines = "1.0.0-ALPHA-18" desugarJdkLibs = "2.0.3" leakcanary = "2.12" -hilt = "2.47" +hilt = "2.48" kotlinx-coroutines = "1.7.3" kotlinx-datetime = "0.4.0" firebase-analytics = "21.3.0"