WorkManager not initialized exception even though WorkManagerInitializer not disabled in manifest
Asked Answered
S

1

6

I got an app that uses WorkManager for several background tasks. Lately I see a lot of crashes in my production logs due to WM not being initialized even though I did not disable WorkManagerInitializer in the manifest.

java.lang.IllegalStateException: WorkManager is not initialized properly. The most likely cause is that you disabled WorkManagerInitializer in your manifest but forgot to call WorkManager#initialize in your Application#onCreate or a ContentProvider.
        at androidx.work.WorkManager.getInstance(WorkManager:161)
        ...

The WM documentation says

By default, WorkManager auto-initializes itself using a built-in ContentProvider. ContentProviders are created and run before the Application object, so this allows the WorkManager singleton to be setup before your code can run in most cases.

The interesting part there is in most cases. I wonder in what circumstances it is not initialized properly before the app starts.

My initial guess was that a bug in my code might have created too many WM tasks that take too long to initialize, but I was not able to recreate the crash in that way.

Anyone has seen this behavior before and has an idea what could go on here? Why is WorkManager not initialized before app start?

Statutory answered 20/5, 2020 at 22:7 Comment(0)
F
0

Add below code in your manifest file

   <provider
    android:name="androidx.work.impl.WorkManagerInitializer"
    android:authorities="${applicationId}.workmanager-init"
    android:exported="false"
    tools:node="remove" />
Favien answered 13/6, 2020 at 13:3 Comment(1)
I realize that Is one possible solution, but I was specifically asking about the auto init, hence NOT having this in the manifest. Btw, since I was not able to find an answer for the auto init, this is exactly what I am doing now.Statutory

© 2022 - 2024 — McMap. All rights reserved.