I am trying to do a field injection in a non Activity class, but I am always getting that the field has not been initialized/null. I have read Can I use Dagger 2's field injection in Kotlin?, also Dagger 2 on Android @Singleton annotated class not being injected, and still have the same problem. Here is how I have setted it up
This is the model class
class Greetings {
val sayHello: String = "Hello from Dagger 2"
}
This is the module class
@Module
class GreetingsModule {
@Provides
@Singleton
fun providesGreetings(): Greetings {
return Greetings()
}
}
This is the component
@Singleton
@Component(modules = arrayOf(GreetingsModule::class))
interface GreetingsComponent {
fun inject(mainActivity: MainActivity)
fun inject(testGreetings: TestGreetings)
}
And the class that extends from Application
class App: Application() {
private lateinit var greetingsComponent: GreetingsComponent
override fun onCreate() {
super.onCreate()
greetingsComponent = DaggerGreetingsComponent.builder().build()
}
fun getGreetings() = greetingsComponent
}
And this is how I am injecting it into another class and where is null/not initialized
class TestGreetings {
@Inject
lateinit var greetings: Greetings
fun checkIfNull() {
if (greetings != null) {
Log.d("INFO", "${ greetings.sayHello}")
} else {
Log.d("INFO", "null !!!!!!")
}
}
}
What exactly I am doing wrong??