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
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.
{ 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() } }
... } ...... @AndroidEntryPoint class HomeFragment : Fragment() { private val viewModel: HomeViewModel by viewModels() } + Without ViewModelFactory + Without Multi-binding (Dagger)
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
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