google / dagger

A fast dependency injector for Android and Java.

Home Page:https://dagger.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Compile time error with KSP with Generics

nitinsethi86 opened this issue · comments

I am trying to test the waters with KSP (currently in aplha ) mainly because of the expected build time improvements.

I am seeing the below compile time error with ksp. The same setup works with kapt. Is this expected with the current version of 2.51.1?



app/build/generated/ksp/debug/java/com/ms/multimoduleapp/DaggerApplicationComponent.java:78: error: incompatible types: TestDelegate cannot be converted to ITestDelegate
LoginActivity_MembersInjector.injectTestDelegate(instance, new TestDelegate());

I am not quite sure if I understand the comment regarding generics in the below thread.

#2349 (comment)

Dagger version: 2.51.1

I have also attached the reproducer project in case its needed.
Reproducer_KSP.zip

Hi, @nitinsethi86 ,

Thanks for reporting. I was able to reproduce. This should be due to an issue in the KSP library where a type defined as ITestDelegate<? extends InterfaceUsedInGenerics> would be seen as ITestDelegate<InterfaceUsedInGenerics>. While this is fine in Kotlin because ITestDelegate has declaration-site variance, it becomes incompatible with your TestDelegate in Java. I just filed the issue at google/ksp#1969.

As a workaround I would try migrating the place where you inject ITestDelegate to Kotlin.

@kuanyingchou Thanks for the reply.
The classes involved is considerably large. So, the workaround is slightly challenging.
Is there any other workaround that we can apply?

You can create a wrapper in Kotlin and inject that wrapper instead. Something like:

class DelegateWrapper @Inject constructor(
    val testDelegate: ITestDelegate<InterfaceUsedInGenerics>)

Note that due to another issue in an underlying library Dagger uses, the value of suppress in @JvmSuppressWildcards is not respected. Please use @JvmWildcard instead. For example, @JvmSuppressWildcards(suppress = false) ITestDelegate<InterfaceUsedInGenerics> in your code should be replaced with ITestDelegate<@JvmWildcard InterfaceUsedInGenerics>.

That works. Thanks. I will wait for the stable support of KSP in dagger.