@Inject set Non-injectable property
I am trying to use Android Dagger โก implementation and inject DispatchingAndroidInjector
into a class Application
:
class MyApp : Application(), HasActivityInjector {
private lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
@Inject set
override fun onCreate() {
super.onCreate()
AppInjector.init(this)
}
override fun activityInjector(): ActivityInjector<Activity> {
return dispatchingAndroidInjector
}
}
But I get IllegalArgumentExecption
by stating that the property was " lateinit
not initialized":
06-19 10:57:30.773 10797-10797/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 10797
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.ui.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property dispatchingAndroidInjector has not been initialized
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property dispatchingAndroidInjector has not been initialized
at com.example.app.MyApp.activityInjector(MyApp.kt:28)
at dagger.android.AndroidInjection.inject(AndroidInjection.java:55)
at com.example.app.injection.AppInjector.handleActivity(AppInjector.kt:41)
at com.example.app.injection.AppInjector.access$handleActivity(AppInjector.kt:14)
at com.example.app.injection.AppInjector$init$1.onActivityCreated(AppInjector.kt:21)
at android.app.Application.dispatchActivityCreated(Application.java:197)
at android.app.Activity.onCreate(Activity.java:961)
at android.support.v4.app.BaseFragmentActivityGingerbread.onCreate(BaseFragmentActivityGingerbread.java:54)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:319)
at com.example.app.ui.MainActivity.onCreate(MainActivity.kt:20)
at android.app.Activity.performCreate(Activity.java:6682)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
This is the class AppInjector
that is used to create AppComponent
and inject dependencies MyApp
:
object AppInjector {
fun init(app: MyApp) {
DaggerAppComponent.builder().application(app).build().inject(app)
// ...
}
}
And the interface AppComponent
:
@Singleton
@Component(modules = arrayOf(
AndroidInjectionModule::class,
AppModule::class,
MainActivityModule::class
)) interface AppComponent {
@Component.Builder interface Builder {
@BindsInstance fun application(app: Application): Builder
fun build(): AppComponent
}
fun inject(app: MyApp)
}
I am using annotation @Inject
in the property accessor as described in the documentation , but it doesn't seem to be the case at work. What am I doing wrong?
source to share
I can't test this right now, but I guess because your property is private. The dagger does not use reflection, so the fields it introduces must be visible from the outside of the class for it to work.
(Highlighting set
clearly doesn't help with this, since in this case it is just a private setter. Note that fields are private for all properties, not private properties have more visibility getters / setters needed.)
You should also be able to just comment out the property instead of its setter, so this will be the end result:
@Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
If you want your property to be private, use the constructor instead.
source to share