Skip to content

Using jakarta.inject.Singleton Annotation makes the build fail #4780

@Besa123

Description

@Besa123

I tried using the jakarta.inject.Singleton inside my Singleton Module

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import jakarta.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class ValidationModule {

    @Provides
    @Singleton
    fun provideEmailValidator(
    ): EmailValidator {
        return EmailValidatorImpl()
    }

    @Provides
    @Singleton
    fun providePasswordValidator(
    ): PasswordValidator {
        return PasswordValidatorImpl()
    }
}

but I got this error when i tried to build

error: [Dagger/IncompatiblyScopedBindings] com.besa.shelflife.ShelfLifeApplication_HiltComponents.SingletonC scoped with @Singleton may not reference bindings with different scopes:
  public abstract static class SingletonC implements ShelfLifeApplication_GeneratedInjector,
                         ^
      @Provides @jakarta.inject.Singleton com.besa.core.domain.validation.EmailValidator com.besa.core.di.ValidationModule.provideEmailValidator()
  
      @Provides @jakarta.inject.Singleton com.besa.core.domain.validation.PasswordValidator com.besa.core.di.ValidationModule.providePasswordValidator()

after switching to javax.inject.Singleton the issue got fixed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions