title: Field injection in viewModels with Hilt
body: <p>I have the following situation:</p>
  <p><a href="https://i.stack.imgur.com/Z250u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z250u.png" alt="UML Diagram" /></a></p>
  <p>The MainActivity has a fragment, and they each have a viewModel, MainViewModel and FragmentViewModel respectively. I want to inject an instance of class A in both the viewmodels. My understanding from <a href="https://stackoverflow.com/questions/68117680/hilt-field-injection-in-the-super-fragment-or-viewmodel">this post</a> is that you can not use the injected class inside the init block of the viewmodel.</p>
  <p>Hence, I attached a clickListener to a view inside the Fragment to read a property of class A. However, when I click on the view, I get a <code>kotlin.UninitializedPropertyAccessException</code>. Note, I do not have the same issue when I access a property of <code>instance</code> inside the MainViewModel (i.e. Hilt is injecting an instance of A into MainViewModel for me).</p>
  <p>Does anyone have any ideas as to why Hilt is unable to inject an instance of A inside the FragmentViewModel? I have been careful as to annotate the FragmentViewModel in the same way as I have annotated the MainViewModel. Any tips on how I can share a single instance of A with dependency injection in both viewModels would also be appreciated.</p>
  <pre><code>@AndroidEntryPoint
  class MainActivity : AppCompatActivity() {
  
      private val mainViewModel: MainViewModel by viewModels()
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          findViewById&lt;Button&gt;(R.id.main_button).setOnClickListener {
              Log.e(&quot;Test&quot;, &quot;a.initCount ${mainViewModel.getInitCount()}&quot;)
          }
      }
  }
  
  @HiltViewModel
  class MainViewModel @Inject constructor() : ViewModel() {
  
      @Inject lateinit var instance: A
  
      fun getInitCount(): Int {
          return instance.initCount
      }
  }
  
  @AndroidEntryPoint
  class Fragment : Fragment() {
  
      private val viewModel: FragmentViewModel by viewModels()
  
      override fun onCreateView(
          inflater: LayoutInflater,
          container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          return inflater.inflate(R.layout.fragment, container, false)
      }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
          view.findViewById&lt;Button&gt;(R.id.fragment_button).setOnClickListener {
              Log.e(&quot;Test&quot;, &quot;viewModel.a: ${viewModel.getInitCount()}&quot;)
          }
      }
  }
  
  @HiltViewModel
  class FragmentViewModel @Inject constructor() : ViewModel() {
  
      @Inject lateinit var instance: A
  
      fun getInitCount(): Int {
          return instance.initCount
      }
  }
  
  @HiltAndroidApp
  class App : Application()
  
  class A @Inject constructor() {
  
      var initCount = 0
  
      init {
          Log.e(&quot;Test&quot;, &quot;I have been initialised: ${++initCount}&quot;)
      }
  }
  
  activity_main.xml
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
  
      &lt;Button
          android:id=&quot;@+id/main_button&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;
          android:text=&quot;main button&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          /&gt;
  
      &lt;androidx.fragment.app.FragmentContainerView
          android:id=&quot;@+id/fragment&quot;
          android:name=&quot;com.example.hilttest.Fragment&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;400dp&quot;
          /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  
  fragment.xml
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
  
      &lt;Button
          android:id=&quot;@+id/fragment_button&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;
          android:text=&quot;button&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>