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

Dagger 2.50 KSP fails to generate field in implementation for a @Named field #4225

Open
ggrell opened this issue Jan 27, 2024 · 2 comments
Open

Comments

@ggrell
Copy link

ggrell commented Jan 27, 2024

In the process of switching from KAPT to KSP, using Dagger 2.50. I have a top-level app component for my Android app which contains a @Named field, like this:

@AppScope
@Component(modules = [...])
interface AppComponent {
    val someInjectedString: String
        @Named("Something") get
}

The compile failed with: DaggerAppComponent.java:109: error: AppComponentImpl is not abstract and does not override abstract method getSomeInjectedString() in AppComponent private static final class AppComponentImpl implements AppComponent {

The generated code in DaggerAppComponent.java is missing the getter for the named field. All other fields and functions have implementations. This works correctly under KAPT.

@henrik-klev
Copy link

I have a similar issue with injecting a named parameter

class DefaultClient @Inject constructor(
    ...,
    @Named(BuildConfig.LIBRARY_PACKAGE_NAME) private val loggingInterceptor: LoggingInterceptor
) : HttpClient {

causes

[ksp] dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException$UnexpectedException:
  Validation trace:
    => element (CLASS): ...DefaultClient
    => element (CONSTRUCTOR): DefaultClient(...,com.optomany.axept.pro.log.LoggingInterceptor)
    => element (PARAMETER): loggingInterceptor
    => annotation type: javax.inject.Named
	at dagger.internal.codegen.base.DaggerSuperficialValidation$ValidationException.from(DaggerSuperficialValidation.java:527)
...
Caused by: java.lang.ClassCastException: class dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.DefaultKspType cannot be cast to class java.lang.String (dagger.spi.internal.shaded.androidx.room.compiler.processing.ksp.DefaultKspType is in unnamed module of loader java.net.URLClassLoader @58c23d18; java.lang.String is in module java.base of loader 'bootstrap')

@bcorso
Copy link

bcorso commented Jan 30, 2024

@ggrell thanks for reporting, we'll try to take a look at this and figure out why the getter method is missing in KSP.

In the meantime, you should be able to just swap this over to a normal function, which I would recommend anyway since it's cleaner and more intuitive. For example

@AppScope
@Component(modules = [...])
interface AppComponent {
    @Named("Something")
    fun getSomeInjectedString(): String
}

@henrik-klev that is a different issue -- it's a duplicate of #4051. You can see that issue for the current workaround, or the more general workaround here #4049 (comment)

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

No branches or pull requests

4 participants