Questions tagged [dagger-hilt]

Hilt is a Dagger2-based dependency injection library for Android.

The goals of Hilt are:

  • To simplify Dagger-related infrastructure for Android apps.
  • To create a standard set of components and scopes to ease setup, readability/understanding, and code sharing between apps.
  • To provide an easy way to provision different bindings to various build types (e.g. testing, debug, or release).
1333 questions
7
votes
1 answer

init is not called when injecting viewModel using Hilt

I want to make API request when ViewModel is initialized. That`s why I make API request inside init method; expecting init be triggered when I inject viewModel in Activity. What am I doing wrong? MainViewModel.kt @HiltViewModel class MainViewModel…
7
votes
1 answer

How to Inject Moshi/Gson in Room TypeConvertors using Hilt?

I am trying out hilt and i want to inject moshi for serializing and deserializing. Here's a code sample from a github Repo which is not using di: open class InfoTypeConverter { private val moshi = Moshi.Builder().build() //not using dependency…
Isac
  • 314
  • 2
  • 15
7
votes
3 answers

Dagger-Hilt @ViewModelInject sharedViewModel not injecting into fragment

I'm trying to make a shared injected view model between a fragment and an activity using the Jetpack tutorial. The shared view model is successfully injected into the parent MyActivity but when the child is rendered, the application crashes due to…
Neal Soni
  • 556
  • 1
  • 5
  • 13
6
votes
1 answer

MainActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager

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…
M.Argumedo
  • 181
  • 1
  • 3
  • 9
6
votes
1 answer

Hilt with Jetpack Compose Navigation

I checked this info https://developer.android.com/jetpack/compose/libraries#hilt-navigation how to inject ViewModel to a compose screen. For now I implemeted like this for my test app: NavHost( navController = navController, startDestination…
6
votes
1 answer

Kotlin dagger hilt, how to reinit or delete/clear a serviceComponent

I am new to dagger hilt and dependency injection in general. I followed some exemples i could found and i manage to set up my application with hilt and it's woking perfectly. However i am using a serviceComponent in one of my activity to provide…
XCarb
  • 735
  • 10
  • 31
6
votes
3 answers

Is it bad practice to pass viewmodels to child composables in jetpack compose?

Example: I have a @Composable func WorkoutScreen(...) which injects a dedicated ViewModel (e.g. with hilt). It displays some different child composables like @Composable func ProgressView(...) and some others. ProgressView is the only composable in…
Dominik Seemayr
  • 830
  • 2
  • 12
  • 30
6
votes
1 answer

Hilt circular dependency

I'm creating a pet project with Hilt, and perhaps I'm having this issue because I'm installing everything in SingletonComponent::class, and perhaps I should create components for each one. The pet project has a NetworkModule, UserPrefsModule, and…
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
6
votes
2 answers

Hilt Fragments Issue - onAttach called multiple times with different Context! Hilt Fragments should not be retained

I got following crash in crashlytics, but cannot reproduce issue. There is no exception explanation which I can use to trace: dagger.hilt.internal.Preconditions.checkState (Preconditions.java:83) com.xxx.xxx.ui.base.Hilt_BaseFragment.onAttach…
Hakan Erbaş
  • 139
  • 1
  • 10
6
votes
1 answer

Hilt - what is the Java equivalent to kotlin's "by viewmodels()" to inject viewmodel into activity?

I'm following Kotlin tutorial on learning Dagger Hilt for dependency injection. The tutorial uses class MainActivity: AppCompatActivity() { private val viewModel: TestViewModel by viewModels() } in the MainActivity to inject the viewmodel. It…
DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
6
votes
5 answers

Couldn't find `activityViewModels()` Hilt Android

I'm using Hilt and MVVM at my project and I want to get an viewModel from activityViewModel to use same in 2 activites. But my Android Studio says Unresolved Reference. My app build.gradle is like this: plugins { id…
Emanuel Garcia
  • 325
  • 2
  • 11
6
votes
2 answers

How to inject `rememberNavController` from jetpack compose into an activity using hilt?

I'm trying to inject my navHostController into my MainActivity using hilt. But I'm getting the following error when trying to compile the code: > Task…
6
votes
0 answers

Android Instrumental Test not starting with Startup library

Using Hilt and Startup together. Application is starting without problems, but instrumented test cannot, just infinite waiting. After disabling androidx.startup.InitializationProvider in Manifest.xml tests are starting well. Also looks like troubles…
6
votes
1 answer

Build failed after adding Hilt components in Android project

I was trying to explore Hilt with simplest possible example, but my application isn't building. I added all the dependencies but when I try to build, it shows an error indicating not finding the hilt gradle plugin. Here are my…
B.Ahmad
  • 107
  • 1
  • 8
6
votes
4 answers

Dagger Hilt --Error: annotation @AggregatedRoot is missing a default value for the element 'originatingRoot'--

Im using Dagger Hilt in my project and its not compilating. Checked everything in manifest and project files and have no idea what is happenning :/ error: annotation @AggregatedRoot is missing a default value for the element…
Dan Estulla
  • 386
  • 3
  • 10