This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Top-level build file where you can add configuration options common to all sub-projects/modules. | |
buildscript { | |
... | |
ext.hilt_version = '2.28-alpha' | |
dependencies { | |
... | |
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
apply plugin: 'kotlin-android' | |
apply plugin: 'kotlin-kapt' | |
apply plugin: 'kotlin-android-extensions' | |
//Hilt plugin | |
apply plugin: 'dagger.hilt.android.plugin' | |
android{ | |
.... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
//your other dependencies | |
ext.hilt_version = '2.28-alpha' | |
implementation "com.google.dagger:hilt-android:$hilt_version" | |
kapt "com.google.dagger:hilt-android-compiler:$hilt_version" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Your Application class | |
@HiltAndroidApp | |
class CarExpoApplication :Application() { | |
.... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@AndroidEntryPoint | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
........ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@AndroidEntryPoint | |
class MainActivity : AppCompatActivity() { | |
@Inject lateinit var exampleClass: ExampleClass | |
override fun onCreate(savedInstanceState: Bundle?) { | |
.... | |
exampleClass.greetMessage() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vs.hiltapp.app | |
import android.app.Application | |
import dagger.hilt.android.HiltAndroidApp | |
//this will be the entry point of HILT DI injection to your app. | |
@HiltAndroidApp | |
class HiltSampleApp : Application() { | |
override fun onCreate() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
... | |
implementation "com.google.dagger:hilt-android:2.28-alpha" | |
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha" | |
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02' | |
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha02' | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleViewModel @ViewModelInject constructor( | |
private val networkRepository: NetworkRepository | |
) : ViewModel() { | |
//code | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//add this annotation to make it accessible all dependencies from DI Graph created using HILT | |
@AndroidEntryPoint | |
class MainActivity : AppCompatActivity() { | |
//access viewmodel like this | |
private val sampleViewModel: SampleViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) |
OlderNewer