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

@Binds parameter type not assignable to the return type with AssistedInject generics #1813

Open
deividasstr opened this issue Apr 15, 2020 · 2 comments

Comments

@deividasstr
Copy link

deividasstr commented Apr 15, 2020

The goal was to provide custom generic argument to the ViewModel through Dagger using AssistedInject.
This is the reproducible sample android app: https://github.com/deividasstr/ViewModel-argument-inject

While building the project I get:

public abstract com.deividasstr.daggervmargs.AssistedViewModelFactory<? extends androidx.lifecycle.ViewModel, ? super com.deividasstr.daggervmargs.ViewModelArgument> passwordViewModelFactory(@org.jetbrains.annotations.NotNull()
                                                                                                                                                                          ^.../ApplicationComponent.java:7: error: com.deividasstr.daggervmargs.AssistedViewModelModule has errors
@dagger.Component(modules = {com.deividasstr.daggervmargs.AssistedViewModelModule.class})

Binding method:

@Binds
@IntoMap
@ViewModelKey(PasswordViewModel::class)
abstract fun passwordViewModelFactory(factory: PasswordViewModel.Factory): AssistedViewModelFactory<out ViewModel, in ViewModelArgument>

Here, the return type is AssistedViewModelFactory, which is

interface AssistedViewModelFactory<Vm : ViewModel, Args: ViewModelArgument> {
    fun create(arguments: Args): Vm
}

And ViewModelArgument is just abstract class ViewModelArgument.
ViewModel is android architecture component.

The PasswordViewModel.Factory is

    @AssistedInject.Factory
    interface Factory : AssistedViewModelFactory<PasswordViewModel, PasswordVmArgs>{
        override fun create(arguments: PasswordVmArgs): PasswordViewModel
    }

The most confusing thing is that on my company's project this code works just perfectly. The difference is that the codebase is big, it has many modules, many dependencies, some of which are not the latest.

If I use @Provides instead of @Binds and cast the parameter manually to AssistedViewModelFactory<out ViewModel, in ViewModelArgument>, it works as well.

@Provides
@IntoMap
@ViewModelKey(PasswordViewModel::class)
fun passwordViewModelFactory(factory: PasswordViewModel.Factory): AssistedViewModelFactory<out ViewModel, in ViewModelArgument> {
        return factory as AssistedViewModelFactory<out ViewModel, in ViewModelArgument>
}

If I remove the second type from AssistedViewModelFactory<out ViewModel, in ViewModelArgument>, the ViewModelArgument, it works as well.

What I have tried:
downgrading all kinds of dependencies and plugins (kotlin, gradle, gradle android, dagger) in the sample project, upgrading them in the company's project (still worked), spamming @JvmSuppressWildcards right and left, checking for differences in the related code, checking the generated code and decompiled java code.

All the dagger code seems to be generated properly.

Might be related to #1143
Could it be due to race conditions between javac and kapt?

@AntoineGagnon
Copy link

Hi, did you find a solution to this? I have a very similar issue.

@deividasstr
Copy link
Author

Hey, IIRC this was caused when dagger kapt was not added as a dependency to both modules. I'll check it out and if so, close the issue later.

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