3

I'm using Dagger2 for DI in android app,

I want inject viewModel in a fragment by AndroidInjector and have this two line :

 AndroidInjection.inject(this)
 viewModel = ViewModelProviders.of(this, viewModelFactory).get(ProductDetailViewModel::class.java)

When my fragment extends androidx.fragment.app.Fragment shows this error:

  • the first line uses deprecated android.support.v4.app.Fragment
  • but second uses android.app.Fragment

now I don't know my fragment should extends which one!

I get this error when choose one of them :

None of the following functions can be called with the arguments supplied: public open fun inject(activity: Activity!): Unit defined in dagger.android.AndroidInjection public open fun inject(fragment: Fragment!): Unit defined in dagger.android.AndroidInjection public open fun inject(service: Service!): Unit defined in dagger.android.AndroidInjection public open fun inject(contentProvider: ContentProvider!): Unit defined in dagger.android.AndroidInjection

beigirad
  • 4,986
  • 2
  • 29
  • 52

3 Answers3

9

My mistake was using dagger-android (not dagger-android-support), and dagger-android just has :

 AndroidInjection.inject(this)

but I should use

 AndroidSupportInjection.inject(this)

that is in supportLibrary version of dagger-android that named dagger-android-support

beigirad
  • 4,986
  • 2
  • 29
  • 52
1

Google introduced us AndroidX and for making all developer on same page they make Fragment deprecated and Make support fragment part of AndroidX.so you must need to user support fragment for same with AnroidX which part of jetpack....

https://developer.android.com/jetpack/androidx/

Neha Chauhan
  • 622
  • 4
  • 12
  • have you idea about it how to resolve androidx 'injector: AppComponent' is deprecated. Consumers should switch to support.v4.app.Fragment – Ajay Pandya May 11 '19 at 09:46
0

The androidx is refactored library namespace.

AndroidX is a major improvement to the original Android Support Library. Like the Support Library, AndroidX ships separately from the Android OS and provides backward-compatibility across Android releases. AndroidX fully replaces the Support Library by providing feature parity and new libraries.see here

so I suggest you to first you should migrate the code to the androidx. to do that follow.

Refactor>migrate to Androidx.

and kudos you're done no more conflicts with library version.

Urvish rana
  • 601
  • 10
  • 24