Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Migration From Dagger to Hilt - #Android11Meetup

Migration From Dagger to Hilt - #Android11Meetup

Ahmad Arif Faizin

August 08, 2020
Tweet

More Decks by Ahmad Arif Faizin

Other Decks in Programming

Transcript

  1. Replace Me! (Bisa ditambahkan dengan image yang relevan) Migrating your

    Dagger app to Hilt Ahmad Arif Faizin (Academy Content Writer-Dicoding Indonesia)
  2. • Without DI class Car { private val engine =

    Engine() fun start() { engine.start() } } fun main() { val car = Car() car.start() } • With DI class Car(private val engine: Engine){ fun start() { engine.start() } } fun main() { val engine = Engine() val car = Car(engine) car.start() } Diff Manual DI
  3. Dagger Hilt Hilt is a dependency injection library for Android

    that reduces the boilerplate of doing manual dependency injection in your project. It simplify Dagger process by providing containers for every Android class in your project and managing their lifecycles automatically for you.
  4. Dagger Hilt in Action • Before class MainActivity : AppCompatActivity()

    { lateinit var engine: Engine override fun onCreate(...) { ... engine = Engine() val car = Car(engine) car.start() } } • After @AndroidEntryPoint class MainActivity : AppCompatActivity() { @Inject lateinit var engine: Engine override fun onCreate(...) { ... val car = Car(engine) car.start() } }
  5. Hilt ❤ Jetpack class HomeViewModel @ViewModelInject constructor(...) : ViewModel() {

    ... } ...... @AndroidEntryPoint class HomeFragment : Fragment() { private val viewModel: HomeViewModel by viewModels() } + Without ViewModelFactory + Without Multi-binding (Dagger)
  6. Migration Flow 1. Add @HiltAndroidApp in MyApplication 2. Add @InstallIn

    in every Module 3. Use @ApplicationContext to provide Context 4. Use @ViewModelInject to inject ViewModel 5. Remove all Component and Dagger injection code 6. Add @AndroidEntryPoint in every Activity/Fragment
  7. Migration Overview ✓ All @Component/@Subcomponent usages should be removed. ✓

    All @Module classes should be annotated with @InstallIn. ✓ Use @ApplicationContext if you need Context ✓ Use a scope that matches the component ✓ All classes should be annotated with @AndroidEntryPoint. Include Activity that contain Fragment. ✓ Use @ViewModelInject to Inject ViewModel