elihart
08/25/2021, 5:14 PM@Attr(R2.layout.my_layout)
class MyClass
Where the resource is defined in a class like this
public final class R2 {
public static final class layout {
public static final int my_layout = 1616;
}
}
We borrowed this pattern from Butterknife, and the R2 class comes from the Butterknife gradle plugin to make the resource values final.
When a resource value is used as the annotation argument like this, KSP and the kotlin compiler don’t seem to recognize it. I’ve done some debugging and the whole argument is just absent in the compiler representation of the arguments list. However, if I use a constant Int directly the argument is available.
Does anyone know why KSP/Kotlin can’t recognize a value defined this way?elihart
08/25/2021, 5:18 PMbuild/generated/source/r2/debug/
- I’m wondering if Ksp or Kotlin is not correctly incorporating generated sources from there?Jiaxiang
08/25/2021, 7:06 PMelihart
08/25/2021, 7:51 PMkotlin {
sourceSets.main {
kotlin.srcDir("build/generated/source/r2/")
}
}
elihart
08/25/2021, 8:11 PMkotlin {
sourceSets {
main.kotlin.srcDirs += 'build/generated/source/r2/debug'
}
}
elihart
08/25/2021, 8:12 PMJiaxiang
08/25/2021, 8:30 PMbuild/generated
is picked up later after KSP processing. )Ting-Yuan Huang
08/25/2021, 9:50 PMelihart
08/25/2021, 9:53 PMelihart
08/25/2021, 9:54 PMLike how will KSP know which folder it should look into (in this case the compile works probably due to the fact thatcould KSP pick up sources inis picked up later after KSP processing. )build/generated
build/generated
before processing starts?Ting-Yuan Huang
08/25/2021, 10:13 PMTing-Yuan Huang
08/25/2021, 10:44 PMelihart
08/26/2021, 12:46 AMkspKotlin
that is failing. The normal Android R class seems to be correctly processed, it is only the “R2” class that is generated by the custom gradle plugin that isn’t automatically included, and I need to manually include its source
(this R2 class is just a workaround to reference resources in annotations, since the normal R class has non final field values)Ting-Yuan Huang
08/26/2021, 12:51 AMcompileKotlin
see instead of picking up directly from build/generated
. Does the custom compiler plugin register R2 to compileKotlin
?Ting-Yuan Huang
08/26/2021, 12:54 AMelihart
08/26/2021, 1:20 AM"generated/source/r2/${variant.dirName}"
elihart
09/01/2021, 6:59 PMGradle detected a problem with the following location: '...build/generated/source/r2/debug'.
Reason: Task 'kspDebugKotlin' uses this output of task 'generateDebugR2' without declaring an explicit or implicit dependency.
This can lead to incorrect results being produced, depending on what order the tasks are executed.
Please refer to <https://docs.gradle.org/7.2/userguide/validation_problems.html#implicit_dependency> for more details about this problem.
So it would be nice if there was a better solution for KSP to pick up the same generated files automatically that android and kotlin tasks doelihart
09/01/2021, 9:47 PMprivate fun Project.setUpR2TaskDependency() {
// We have to submit another afterEvaluate, as the android plugin may be applied after the ProcessorPlugin, and not had a chance
// to create sources yet.
afterEvaluate {
requireAndroidVariants().forEach {
val r2Task = runCatching { project.tasks.named("generate${it.name.capitalize()}R2") }.getOrNull()
if (r2Task != null) {
project.tasks.named("ksp${it.name.capitalize()}Kotlin").dependsOn(r2Task)
project.extensions.configure(KotlinAndroidProjectExtension::class.java) {
sourceSets.getByName("main")
.kotlin
.srcDir("build/generated/source/r2/${it.name}")
}
}
}
}
}
Ting-Yuan Huang
09/01/2021, 10:23 PMelihart
09/01/2021, 10:30 PMTing-Yuan Huang
09/03/2021, 9:23 AMelihart
09/03/2021, 6:57 PMTing-Yuan Huang
09/07/2021, 6:32 PMallowSourcesFromOtherPlugins=true
in
// build.gradle.kts
ksp {
allowSourceFromOtherPlugins=true
}
We didn't enable it by default because we are unsure about the impact yet. For example, it may pick up outputs from kapt and it can be a disaster. We need to study more before we can flip the switch.elihart
09/07/2021, 7:02 PM