https://kotlinlang.org logo
Title
a

Andrea Di Menna

03/23/2022, 12:23 PM
hi guys 🙂 I am developing a simple KSP annotation processor and applying it on a sample android library project, which has
debug
and
release
build types. Different source folders are created for each build type, i.e.
build/generated/ksp/debug/kotlin
and
build/generated/ksp/release/kotlin
respectively. In order to be able to consume the generated code I have added the following code in my
build.gradle.kts
buildTypes {
        getByName("debug") {
            sourceSets {
                getByName("main") {
                    kotlin.srcDir(file("build/generated/ksp/debug/kotlin"))
                }
            }
        }
        getByName("release") {
            sourceSets {
                getByName("main") {
                    kotlin.srcDir(file("build/generated/ksp/release/kotlin"))
                }
            }
        }
    }
Unfortunately, if I run
kspDebugKotlin
and
kspReleaseKotlin
w.o. cleaning the
build
folder my generated classes are marked with errors in Android Studio. The error reads
Redeclaration
as if both folders were added to the same source set. Do you know how to get rid of this error? Using: • ksp
1.5.31-1.0.0
• Kotlin
1.5.31
• Android Studio Bumblebee • AGP
7.1.0
e

efemoney

03/23/2022, 6:33 PM
You are indeed adding both debug & release generated folders to the main source set 🙂
Doesn’t matter in what block you call
sourceSets
, you are accessing the same instance, which means you are configuring the main sourceSet to include both generated folders.
You should do:
android {
  sourceSets {
    named("debug") { kotlin.srcDir("$buildDir/generated/ksp/debug/kotlin") }
    named("release") { kotlin.srcDir("$buildDir/generated/ksp/release/kotlin") }
  }
}
OR better still:
android {
  sourceSets.configureEach {
    kotlin.srcDir("$buildDir/generated/ksp/$name/kotlin/")
  }
}
🎉 1
j

Jiaxiang

03/23/2022, 8:08 PM
Right, you need to use android gradle plugin to add the generated source from corresponding building variant. On a side note, the generated files should be added to compile path by KSP, are you adding to source set in gradle configure file for IDE indexing?
a

Andrea Di Menna

03/24/2022, 7:28 AM
@efemoney thank you so much! I think I was deceived by the comments in https://github.com/google/ksp/issues/37 😅