Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test dagger-ksp w/ KSP2 #1561

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 14 additions & 34 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompilerOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.internal.KaptGenerateStubsTask
import org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin
import org.jetbrains.kotlin.gradle.targets.js.ir.DefaultIncrementalSyncTask
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
Expand All @@ -35,7 +33,6 @@ plugins {
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.kapt) apply false
alias(libs.plugins.kotlin.plugin.parcelize) apply false
alias(libs.plugins.kotlin.plugin.serialization) apply false
alias(libs.plugins.agp.application) apply false
Expand Down Expand Up @@ -124,7 +121,6 @@ val knownBomConfigurations =
"androidTestImplementation",
"compileOnly",
"testCompileOnly",
"kapt",
"ksp",
)

Expand Down Expand Up @@ -168,15 +164,6 @@ subprojects {
val hasCompose = !project.hasProperty("circuit.noCompose")
plugins.withType<KotlinBasePlugin> {
tasks.withType<KotlinCompilationTask<*>>().configureEach {
// Don't double apply to stub gen
if (this is KaptGenerateStubsTask) {
// TODO due to Anvil we need to force language version 1.9
compilerOptions {
progressiveMode.set(false)
languageVersion.set(KotlinVersion.KOTLIN_1_9)
}
return@configureEach
}
val isWasmTask = name.contains("wasm", ignoreCase = true)
compilerOptions {
if (isWasmTask && this is KotlinJsCompilerOptions) {
Expand All @@ -192,23 +179,20 @@ subprojects {
.map { it.toString() }
.map(JvmTarget::fromTarget)
)
// Stub gen copies args from the parent compilation
if (this@configureEach !is KaptGenerateStubsTask) {
freeCompilerArgs.addAll(
"-Xjsr305=strict",
// Match JVM assertion behavior:
// https://publicobject.com/2019/11/18/kotlins-assert-is-not-like-javas-assert/
"-Xassertions=jvm",
// Potentially useful for static analysis tools or annotation processors.
"-Xemit-jvm-type-annotations",
// Enable new jvm-default behavior
// https://blog.jetbrains.com/kotlin/2020/07/kotlin-1-4-m3-generating-default-methods-in-interfaces/
"-Xjvm-default=all",
// https://kotlinlang.org/docs/whatsnew1520.html#support-for-jspecify-nullness-annotations
"-Xtype-enhancement-improvements-strict-mode",
"-Xjspecify-annotations=strict",
)
}
freeCompilerArgs.addAll(
"-Xjsr305=strict",
// Match JVM assertion behavior:
// https://publicobject.com/2019/11/18/kotlins-assert-is-not-like-javas-assert/
"-Xassertions=jvm",
// Potentially useful for static analysis tools or annotation processors.
"-Xemit-jvm-type-annotations",
// Enable new jvm-default behavior
// https://blog.jetbrains.com/kotlin/2020/07/kotlin-1-4-m3-generating-default-methods-in-interfaces/
"-Xjvm-default=all",
// https://kotlinlang.org/docs/whatsnew1520.html#support-for-jspecify-nullness-annotations
"-Xtype-enhancement-improvements-strict-mode",
"-Xjspecify-annotations=strict",
)
}

progressiveMode.set(true)
Expand Down Expand Up @@ -248,10 +232,6 @@ subprojects {
module("com.google.guava:listenablefuture") { replacedBy("com.google.guava:guava") }
}

pluginManager.withPlugin("org.jetbrains.kotlin.kapt") {
tasks.withType<KaptGenerateStubsTask>().configureEach { useK2Kapt.set(true) }
}

pluginManager.withPlugin("com.vanniktech.maven.publish") {
apply(plugin = "org.jetbrains.dokka")

Expand Down
4 changes: 0 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ android.suppressUnsupportedOptionWarnings=android.suppressUnsupportedOptionWarni
android.lint.useK2Uast=true
ksp.useKSP2=true

# Use Kapt4 in K2
# TODO doesn't work with Anvil yet
kapt.use.k2=false

org.gradle.parallel=true
org.gradle.configureondemand=true
org.gradle.caching=true
Expand Down
1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ emulatorWtf = { id = "wtf.emulator.gradle", version = "0.17.0" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-atomicfu = { id = "org.jetbrains.kotlin.plugin.atomicfu", version.ref = "kotlin" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlin-plugin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
kotlin-plugin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" }
Expand Down
27 changes: 2 additions & 25 deletions samples/star/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ plugins {
alias(libs.plugins.compose)
alias(libs.plugins.kotlin.plugin.compose)
alias(libs.plugins.agp.library) apply false
alias(libs.plugins.kotlin.kapt)
alias(libs.plugins.kotlin.plugin.parcelize) apply false
alias(libs.plugins.kotlin.plugin.serialization)
alias(libs.plugins.anvil)
Expand All @@ -25,7 +24,7 @@ plugins {
alias(libs.plugins.emulatorWtf)
}

// Cannot enable both Desktop and Android w/ kapt due to
// Cannot enable both Desktop and Android w/ java due to
// https://youtrack.jetbrains.com/issue/KT-30878
val buildDesktop = project.hasProperty("circuit.buildDesktop")

Expand Down Expand Up @@ -124,8 +123,6 @@ kotlin {
implementation(libs.ktor.client.engine.okhttp)
implementation(libs.okhttp)
implementation(libs.okhttp.loggingInterceptor)
val kapt by configurations.getting
kapt.dependencies.addLater(libs.dagger.compiler)
}
}
maybeCreate("jvmCommonTest").apply {
Expand Down Expand Up @@ -272,32 +269,12 @@ fun String.capitalizeUS() = replaceFirstChar {

val kspTargets = kotlin.targets.names.map { it.capitalizeUS() }

// Workaround for https://youtrack.jetbrains.com/issue/KT-59220
afterEvaluate {
for (target in kspTargets) {
if (target != "Android" && target != "Jvm") continue
val buildType = if (target == "Android") "Release" else ""
val kspTaskName = "ksp${buildType}Kotlin${target}"
val useKSP2 = providers.gradleProperty("ksp.useKSP2").getOrElse("false").toBoolean()
val generatedKspKotlinFiles =
if (useKSP2) {
val kspReleaseTask = tasks.named<KspAATask>(kspTaskName)
kspReleaseTask.flatMap { it.kspConfig.kotlinOutputDir }
} else {
val kspReleaseTask = tasks.named<KspTaskJvm>(kspTaskName)
kspReleaseTask.flatMap { it.destination }
}
tasks.named<KotlinCompile>("kaptGenerateStubs${buildType}Kotlin${target}").configure {
source(generatedKspKotlinFiles)
}
}
}

ksp { arg("circuit.codegen.lenient", "true") }

dependencies {
for (target in kspTargets) {
val targetConfigSuffix = if (target == "Metadata") "CommonMainMetadata" else target
add("ksp${targetConfigSuffix}", projects.circuitCodegen)
add("ksp${targetConfigSuffix}", libs.dagger.compiler)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
package com.slack.circuit.star.home

import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
Expand All @@ -25,6 +26,7 @@ import com.slack.circuit.star.di.AppScope
import com.slack.circuit.star.parcel.CommonParcelize
import com.slack.circuit.star.resources.Res
import com.slack.circuit.star.resources.star_icon
import com.slack.circuit.star.ui.StarTheme
import org.jetbrains.compose.resources.painterResource

@CommonParcelize data object AboutScreen : StaticScreen
Expand Down Expand Up @@ -52,9 +54,8 @@ fun About(modifier: Modifier = Modifier) {
}
}

// TODO this breaks kapt because it's only on desktop and not other platforms
// @Preview
// @Composable
// private fun AboutPreview() {
// StarTheme { About() }
// }
@Preview
@Composable
private fun AboutPreview() {
StarTheme { About() }
}
3 changes: 0 additions & 3 deletions samples/tacos/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright (C) 2023 Slack Technologies, LLC
// SPDX-License-Identifier: Apache-2.0
import org.jetbrains.kotlin.gradle.internal.KaptGenerateStubsTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.agp.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.kapt)
alias(libs.plugins.kotlin.plugin.parcelize)
alias(libs.plugins.ksp)
alias(libs.plugins.kotlin.plugin.compose)
Expand All @@ -22,7 +20,6 @@ android {

tasks
.withType<KotlinCompile>()
.matching { it !is KaptGenerateStubsTask }
.configureEach {
compilerOptions {
freeCompilerArgs.addAll(
Expand Down
Loading