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

Migration From Dagger to Hilt - #Android11Meetup

Migration From Dagger to Hilt - #Android11Meetup

1fe3e1b3343b555b0a195180607fbd6a?s=128

Ahmad Arif Faizin

August 08, 2020
Tweet

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. Dependency Injection (DI) ???

  3. • 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
  4. 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.
  5. Dagger Hilt Source : ttps://www.remotelands.com/travelogues/app/uploads/2019/08/Keris-bali-02.jpg

  6. Why Dagger Hilt?

  7. 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() } }
  8. Injection Flow Engine Initialization

  9. Component hierarchy

  10. Hilt ❤ Jetpack class HomeViewModel @ViewModelInject constructor(...) : ViewModel() {

    ... } ...... @AndroidEntryPoint class HomeFragment : Fragment() { private val viewModel: HomeViewModel by viewModels() } + Without ViewModelFactory + Without Multi-binding (Dagger)
  11. Let’s Code! s.id/simple-hilt

  12. 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
  13. Before and after code https://s.id/dagger-to-hilt

  14. 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
  15. Referensi • https://developer.android.com/training/dependency-injection/hilt-android • https://codelabs.developers.google.com/codelabs/android-dagger-to-hilt • https://dagger.dev/hilt/migration-guide.html

  16. Contact: Ahmad Arif Faizin arif@dicoding.com medium.com/@arifaizin Replace Me! (Bisa ditambahkan

    dengan image yang relevan)