question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

2.38 - lateinit property workerFactory has not been initialized

See original GitHub issue

@HiltAndroidApp
class MyApplication : Application(), Configuration.Provider {

    @Inject
    internal lateinit var workerFactory: HiltWorkerFactory

    override fun getWorkManagerConfiguration() =
        Configuration.Builder()
            .setExecutor(Dispatchers.Default.asExecutor())
            .setWorkerFactory(workerFactory)
            .setTaskExecutor(Dispatchers.Default.asExecutor())
            .setMaxSchedulerLimit(MAX_SCHEDULER_LIMIT)
            .build()

    
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.cisystems.ssmp.arm_vmb2, PID: 13731
    java.lang.RuntimeException: Unable to create service com.cisystems.ssmp.arm_vmb2.services.HeartbeatService: kotlin.UninitializedPropertyAccessException: lateinit property workerFactory has not been initialized
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3544)
        at android.app.ActivityThread.access$1300(ActivityThread.java:199)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: kotlin.UninitializedPropertyAccessException: lateinit property workerFactory has not been initialized
        at com.cisystems.ssmp.arm_vmb2.MyApplication.getWorkerFactory$app_minApi23Debug(MyApplication.kt:37)
        at com.cisystems.ssmp.arm_vmb2.MyApplication.getWorkManagerConfiguration(MyApplication.kt:42)
        at androidx.work.impl.WorkManagerImpl.getInstance(WorkManagerImpl.java:155)
        at androidx.work.WorkManager.getInstance(WorkManager.java:184)
        at com.cisystems.ssmp.arm_vmb2.workers.DirectoryUpdateWorker$Companion.stop$app_minApi23Debug(DirectoryUpdateWorker.kt:337)
        at com.cisystems.ssmp.arm_vmb2.services.HeartbeatService$Companion.stopExecutes(HeartbeatService.kt:57)
        at com.cisystems.ssmp.arm_vmb2.services.HeartbeatService$Companion.access$stopExecutes(HeartbeatService.kt:36)
        at com.cisystems.ssmp.arm_vmb2.services.HeartbeatService.onCreate(HeartbeatService.kt:66)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3532)
        at android.app.ActivityThread.access$1300(ActivityThread.java:199) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:9
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
bcorsocommented, Jul 23, 2021

@BugsBunnyBR if this is an issue with ContentProviders calling before onCreate() you should be able to fix that issue by using an entry point instead of relying on field injection. For example:

@HiltAndroidApp
class MyApplication : Application(), Configuration.Provider {

    @EntryPoint
    @InstallIn(SingletonComponent::class)
    interface HiltWorkerFactoryEntryPoint {
        fun workerFactory(): HiltWorkerFactory
    }

    override fun getWorkManagerConfiguration() =
        Configuration.Builder()
            .setExecutor(Dispatchers.Default.asExecutor())
            .setWorkerFactory(EntryPoints.get(this, HiltWorkerFactoryEntryPoint::class.java).workerFactory())
            .setTaskExecutor(Dispatchers.Default.asExecutor())
            .setMaxSchedulerLimit(MAX_SCHEDULER_LIMIT)
            .build()
}

The entry point will take initiative to create the component if it’s not already created.

0reactions
Chang-Ericcommented, Jul 26, 2021

The original report sounds like #2784 is the cause. Closing this assuming that is the case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

lateinit property WorkerFactory has not been initialized - Stack ...
The way you initialized your workmanager and workmanagerfactory is only working till workmanager version 2.5.
Read more >
Could not instantiate Worker when using Hilt for dependency ...
I believe that all configuration is correct in my project. I attached a sample project to this post. In case anyone is interested,...
Read more >
lateinit property ApplicationContext has not been initialized ...
Inject the context to the respective file where it needs to be used via constructor and use that instance. You can also use...
Read more >
WorkerFactory - Android Developers
The returned worker must be a newly-created instance and must not have been previously returned or invoked by WorkManager.
Read more >
You can check whether the lateinit property has been ... - Reddit
At times this can be useful like if an object will definitely be initialised in all places except 1 where it may not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found