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

Compile time error with KSP with Generics #4322

Closed
nitinsethi86 opened this issue Jun 6, 2024 · 4 comments
Closed

Compile time error with KSP with Generics #4322

nitinsethi86 opened this issue Jun 6, 2024 · 4 comments

Comments

@nitinsethi86
Copy link

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

@kuanyingchou
Copy link
Collaborator

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.

@nitinsethi86
Copy link
Author

@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?

@kuanyingchou
Copy link
Collaborator

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>.

@nitinsethi86
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants