6

I'm learning to use Jetpack Compose and I was trying to follow a tutorial to create a Pokedex with Hilt and MVVM, everything was going well until I tried to implement the viewModel, when I injected it and tried to run the app, I got the title error ( Given component holder class com.example.pokedex.MainActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager ), I already tried several solutions from the forum changing the way of injecting it but none of them has worked for me.

My code:

Pokedex Application:

@HiltAndroidApp
class PokedexApplication : Application() {
  override fun onCreate() {
    super.onCreate()
    Timber.plant(Timber.DebugTree())
  }
}

MainActivity

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      PokedexTheme {
        val navController = rememberNavController()
        NavHost(navController = navController,
          startDestination = "pokemon_list_screen"
        ) {
          composable("pokemon_list_screen") {
            PokemonListScreen(navController = navController)
          }
          composable(
            "pokemon_detail_screen/{dominantColor}/{pokemonName}",
            arguments = listOf(
              navArgument("dominantColor") {
                type = NavType.IntType
              },
              navArgument("pokemonName") {
                type = NavType.StringType
              }
            )) {
            val dominantColor = remember {
              val color = it.arguments?.getInt("dominantColor")
              color?.let { Color(it) } ?: Color.White
            }
            val pokemonName = remember {
              it.arguments?.getString("pokemonName")

            }
          }
        }
      }
    }
  }
}

AppModule

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

  @Singleton
  @Provides
  fun providePokemonRepository(
    api: PokeApi
  ) = PokemonRepository(api)

  @Singleton
  @Provides
  fun providePokeApi(): PokeApi {
    return Retrofit.Builder()
      .addConverterFactory(GsonConverterFactory.create())
      .baseUrl(BASE_URL)
      .build()
      .create(PokeApi::class.java)
  }
}

Repository

@ActivityScoped
class PokemonRepository @Inject constructor (
  private val api: PokeApi
  ) {

  suspend fun getPokemonList(limit: Int, offset: Int): Resource<PokemonList> {
    val response = try {
      api.getPokemonList(limit, offset)
    } catch (e: Exception) {
      return Resource.Error(e.message.toString())
    }
    return Resource.Success(response)
  }
}

Composable where I call the ViewModel

@Composable
fun PokemonList(
  navController: NavController,
  viewModel: PokemonListViewModel = hiltViewModel()
) {

  val pokemonList by remember { viewModel.pokemonList }
  val endReached by remember { viewModel.endReached }
  val loadError by remember { viewModel.loadError }
  val isLoading by remember { viewModel.isLoading }

  LazyColumn(contentPadding = PaddingValues(16.dp)) {
    val itemCount = if(pokemonList.size % 2 == 0) pokemonList.size / 2 else pokemonList.size / 2 + 1

    items(itemCount) {
      if (it >= itemCount - 1 && !endReached){
        viewModel.loadPokemonPaginated()
      }
      PokedexRow(rowIndex = it, entries = pokemonList, navController = navController)
    }
  }
}

ViewModel (Just first code)

@HiltViewModel
class PokemonListViewModel @Inject constructor(
  private val repository: PokemonRepository
) : ViewModel() {

  private var curPage = 0

  var pokemonList = mutableStateOf<List<PokedexListEntry>>(listOf())
  var loadError = mutableStateOf("")
  var isLoading = mutableStateOf(false)
  var endReached = mutableStateOf(false)

  init {
    loadPokemonPaginated()
  }

Compose version: 1.1.1

Kotlin version 1.6.10

Dependencies

implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'
implementation 'androidx.activity:activity-compose:1.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"

implementation("com.google.dagger:hilt-android:2.41")
kapt("com.google.dagger:hilt-android-compiler:2.38.1")

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation "com.squareup.okhttp3:okhttp:4.9.3"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"

// Timber
implementation 'com.jakewharton.timber:timber:4.7.1'

// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'

// Coroutine Lifecycle Scopes
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1"

// Coil
implementation "io.coil-kt:coil:1.3.2"
implementation "com.google.accompanist:accompanist-coil:0.7.0"

//Dagger - Hilt
implementation 'com.google.dagger:hilt-android:2.41'
kapt 'com.google.dagger:hilt-compiler:2.41'
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'

//Navigation
implementation "androidx.navigation:navigation-compose:2.4.2"

//Palette
implementation "androidx.palette:palette:1.0.0"
M.Argumedo
  • 181
  • 1
  • 3
  • 9
  • Have you inspected answers [here](https://stackoverflow.com/q/67319542/3585796)? – Phil Dukhov Apr 13 '22 at 08:35
  • 1
    Did you add "hilt-android-gradle-plugin" in build.gradle (project) or "dagger.hilt.android.plugin" in build.gradle (app) by step in this document. https://developer.android.com/training/dependency-injection/hilt-android – Suphakrit Nakhabat Jun 28 '22 at 18:57
  • Hi, Follow instructions in this answer https://stackoverflow.com/a/72621197/3187366 – issamux Jan 06 '23 at 18:21
  • Did anyone find the solution? None of the answers mentioned in the comments didn't solve my issue – Varun Narisetty Jun 01 '23 at 00:23
  • I faced the same issue. **I forgot to add the hilt plugins** to the project's root build.gradle and app build.gradle. Read this documentation https://developer.android.com/training/dependency-injection/hilt-android – Carlos Leonardo Camilo Vargas Jun 15 '23 at 23:13

1 Answers1

0
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 21 '23 at 16:22