1 year ago

#334729

test-img

Manuel Lucas

Dagger Hilt doesn't recognize Singleton and Inject annotations

I was developing an App where I try to use some tecnologies as JetpackCompose, and Dagger Hilt, for dependency injection.

Everything works fine with network connection, but I start to get the following error when I try to implement dependecy injection for my localdatabase which I'm using Room:

C:\Users\manuel.lucas\AndroidStudioProjects\JetpackComposePokedex\app\build\generated\source\kapt\debug\com\plcoding\jetpackcomposepokedex\PokedexApplication_HiltComponents.java:137: error: [Dagger/Nullable] com.plcoding.jetpackcomposepokedex.data.local.LocalDatabase is not nullable, but is being provided by @org.jetbrains.annotations.Nullable @javax.annotation.Nonnull @Provides @Singleton com.plcoding.jetpackcomposepokedex.data.local.LocalDatabase com.plcoding.jetpackcomposepokedex.di.LocalModule.provideDB(android.app.Application)
  public abstract static class SingletonC implements PokedexApplication_GeneratedInjector,
                         ^
      com.plcoding.jetpackcomposepokedex.data.local.LocalDatabase is injected at
          com.plcoding.jetpackcomposepokedex.di.LocalModule.provideDao(localDatabase)
      com.plcoding.jetpackcomposepokedex.data.local.PokemonDAO is injected at
          com.plcoding.jetpackcomposepokedex.repository.LocalRepository(dao)
      com.plcoding.jetpackcomposepokedex.repository.LocalRepository is injected at
          com.plcoding.jetpackcomposepokedex.myPokemons.MyPokemonViewModel(localRepository)
      com.plcoding.jetpackcomposepokedex.myPokemons.MyPokemonViewModel is injected at
          com.plcoding.jetpackcomposepokedex.myPokemons.MyPokemonViewModel_HiltModules.BindsModule.binds(vm)
      @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
          dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.plcoding.jetpackcomposepokedex.PokedexApplication_HiltComponents.SingletonC ? com.plcoding.jetpackcomposepokedex.PokedexApplication_HiltComponents.ActivityRetainedC ? com.plcoding.jetpackcomposepokedex.PokedexApplication_HiltComponents.ViewModelC]

Regaring my LocalModule class which is failing is this:`

In this class Singleton class is in red, as AS doesn't recognize

package com.plcoding.jetpackcomposepokedex.di

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import androidx.room.Room

import android.app.Application
import com.plcoding.jetpackcomposepokedex.data.local.LocalDatabase
import com.plcoding.jetpackcomposepokedex.data.local.PokemonDAO
import com.plcoding.jetpackcomposepokedex.util.Constants
import javax.annotation.Nonnull
import javax.annotation.Nullable
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object LocalModule {

    @Singleton
    @Provides
    @Nonnull
    open fun provideDB(application: Application?): LocalDatabase? {
        return Room.databaseBuilder(
            application!!,
            LocalDatabase::class.java,
            Constants.DATABASE_NAME
        )
            .fallbackToDestructiveMigration()
            .allowMainThreadQueries()
            .build()
    }


    @Singleton
    @Provides
    @Nonnull
        open fun provideDao(localDatabase: LocalDatabase): PokemonDAO{
            return localDatabase.pokemonDao()
        }
}

Regarding my build.gradle(app)

plugins{
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'com.google.gms.google-services'
}`

...
    // hilt
    implementation "com.google.dagger:hilt-android:2.37"
    implementation "androidx.hilt:hilt-common:1.0.0"
    kapt "com.google.dagger:hilt-compiler:2.37"
    kapt "androidx.hilt:hilt-compiler:1.0.0"
    implementation "androidx.hilt:hilt-navigation-fragment:1.0.0"
    implementation "androidx.hilt:hilt-navigation-compose:1.0.0-alpha03"

    //Inject and Singleton
    implementation "javax.inject:javax.inject:1"
...

And regarding my build.gradle(proyect)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.37"
        classpath 'com.google.gms:google-services:4.3.10'

    }
}

allprojects {
    repositories {
        // Check that you have the following line (if not, add it):
        google()  // Google's Maven repository
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

I'd swear that the reason Singleton and Inject annotations doesn't find, is due to some wrong library implementation.

the picture which show this is the following

enter image description here

Things I tried are:

  • Remove implementation "javax.inject:javax.inject:1"
  • Add NonNull and Nullable to method of my LocalModule
  • After everychange make "Invalidated cache and restart"

[EDIT]

Finally solved by after remove every build folder of your project and make and "Invalidated cache and restart". Also adding implementation "javax.inject:javax.inject:1"

android

kotlin

dagger-2

dagger

dagger-hilt

0 Answers

Your Answer

Accepted video resources