Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am attempting to use the
Android Dagger ‡ implementation
and inject a
DispatchingAndroidInjector
into my
Application
class:
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 an IllegalArgumentExecption
stating that the "lateinit
property has not been 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 AppInjector
class used to create the AppComponent
and inject the MyApp
dependencies:
object AppInjector {
fun init(app: MyApp) {
DaggerAppComponent.builder().application(app).build().inject(app)
// ...
And the AppComponent
interface:
@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 the @Inject
annotation on the property accessor, as outlined in the documentation, but it doesn't seem to be working. What am I doing wrong?
I can't test it right now, but I think it's because your property is private. Dagger doesn't use reflection, so the fields it injects have to be visible from outside the class in order for it to operate.
(Writing out set
explicitly doesn't help with this, as it just represents a private setter in this case. Note that fields are private for all properties, and non-private properties have getters/setters with broader visibilities as appropriate.)
You should also be able to just annotate the property instead of its setter, so this would be the end result:
@Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
If you want your property to be private, consider using constructor injection instead.
change this
private lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
@Inject set
private internal var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>
@Inject set
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.