Slide 1

Slide 1 text

GDG Location

Slide 2

Slide 2 text

GDG Location What is DI ?

Slide 3

Slide 3 text

GDG Location • Reduces the boilerplate code • Makes our code reusable and clean • Make testing easier • Ease of refactoring DI advantages

Slide 4

Slide 4 text

GDG Location // No Dependency Injection class Car { private val engine = Engine() fun start() {...} }

Slide 5

Slide 5 text

GDG Location // No Dependency Injection class Car { private val engine = Engine() fun start() {...} } fun main() { val car = Car() car.start() }

Slide 6

Slide 6 text

GDG Location // With Dependency Injection class Car(private val engine: Engine) { fun start() {...} }

Slide 7

Slide 7 text

GDG Location // With Dependency Injection class Car(private val engine: Engine) { fun start() {...} } fun main() { val engine = Engine() val car = Car(engine) car.start() }

Slide 8

Slide 8 text

GDG Location DI in Android Many ways to do it... Kodein Koin Dagger ...

Slide 9

Slide 9 text

GDG Location Hilt

Slide 10

Slide 10 text

GDG Location Hilt ● Standardize DI in Android ● Build on top of Dagger ● Tooling Support ● AndroidX Extensions

Slide 11

Slide 11 text

GDG Location Setup buildscript { ... dependencies { ... classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha' } } build.gradle (project)

Slide 12

Slide 12 text

GDG Location Setup apply plugin: 'kotlin-kapt' apply plugin: 'dagger.hilt.android.plugin' //... dependencies { implementation "com.google.dagger:hilt-android:2.28-alpha" kapt "com.google.dagger:hilt-android-compiler:2.28-alpha" } build.gradle (app)

Slide 13

Slide 13 text

GDG Location Hilt annotations ● @HiltAndroidApp ● @Inject ● @AndroidEntryPoint ● @InstallIn ● ...

Slide 14

Slide 14 text

GDG Location @HiltAndroidApp class CarApplication : Application()

Slide 15

Slide 15 text

GDG Location class Car @Inject constructor() { fun start() { } }

Slide 16

Slide 16 text

GDG Location class Car @Inject constructor() { fun start() { } } @AndroidEntryPoint class CarActivity: AppCompatActivity(){ }

Slide 17

Slide 17 text

GDG Location class Car @Inject constructor() { fun start() { } } @AndroidEntryPoint class CarActivity: AppCompatActivity(){ @Inject lateinit var car: Car }

Slide 18

Slide 18 text

GDG Location class Car @Inject constructor() { fun start() { } } @AndroidEntryPoint class CarActivity: AppCompatActivity(){ @Inject lateinit var car: Car override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) car.start() } }

Slide 19

Slide 19 text

GDG Location Components

Slide 20

Slide 20 text

GDG Location Components

Slide 21

Slide 21 text

GDG Location Hilt Module When Interface 3rd party lib Configuration

Slide 22

Slide 22 text

GDG Location class Car @Inject constructor( private val db: CarDB ) { ... }

Slide 23

Slide 23 text

GDG Location @Module @InstallIn(ApplicationComponent::class) object DataModule{ @Provides fun provideCardDB(@ApplicationContext context: Context) = Room.databaseBuilder(context,CarDB::class.java,"car.db") .build() }

Slide 24

Slide 24 text

GDG Location @Module @InstallIn(ApplicationComponent::class) object DataModule{ @Provides fun provideCardDB(@ApplicationContext context: Context) = Room.databaseBuilder(context,CarDB::class.java,"card.db") .build() }

Slide 25

Slide 25 text

GDG Location @Module @InstallIn(ApplicationComponent::class) object DataModule{ @Provides @Singleton fun provideCardDB(@ApplicationContext context: Context) = Room.databaseBuilder(context,CarDB::class.java,"card.db") .build() }

Slide 26

Slide 26 text

GDG Location AndroidX Extensions class CarViewModel @ViewModelInject constructor( private val db: CarDB ) : ViewModel() { ... }

Slide 27

Slide 27 text

GDG Location AndroidX Extensions class CarViewModel @ViewModelInject constructor( private val db: CarDB ) : ViewModel() { ... } @AndroidEntryPoint class CarActivity0 : AppCompatActivity() { val viewModel: CarViewModel by ViewModels() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) viewModel.start() } }

Slide 28

Slide 28 text

GDG Location Resources ● Using Hilt in your Android app [https://cutt.ly/3fsTeno] ● Migrating your Dagger app to Hilt [https://cutt.ly/1fsTiU7]

Slide 29

Slide 29 text

Thank you!