Scheduling Tasks With Android WorkManager | raywenderlich.com

In this WorkManager tutorial, you’ll learn how to schedule different kinds of tasks, test the tasks, as well as debug different tasks.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/20689637-scheduling-tasks-with-android-workmanager

i got error like this, when i tried to Using On-Demand Initialization. please help

 Process: com.raywenderlich.android.workmanager, PID: 10557
    java.lang.RuntimeException: Unable to create application com.raywenderlich.android.workmanager.WorkManagerApp: java.lang.IllegalStateException: WorkManager is already initialized.  Did you try to initialize it manually without disabling WorkManagerInitializer? See WorkManager#initialize(Context, Configuration) or the class level Javadoc for more information.
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6465)
        at android.app.ActivityThread.access$1300(ActivityThread.java:219)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.IllegalStateException: WorkManager is already initialized.  Did you try to initialize it manually without disabling WorkManagerInitializer? See WorkManager#initialize(Context, Configuration) or the class level Javadoc for more information.
        at androidx.work.impl.WorkManagerImpl.initialize(WorkManagerImpl.java:183)
        at androidx.work.WorkManager.initialize(WorkManager.java:210)
        at com.raywenderlich.android.workmanager.WorkManagerApp.onCreate(WorkManagerApp.kt:52)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1182)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6460)
        at android.app.ActivityThread.access$1300(ActivityThread.java:219) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
2021-08-07 14:26:51.546 10557-10557/com.raywenderlich.android.workmanager I/Process: Sending signal. PID: 10557 SIG: 9

With On-Demand Initialization, you have to disable the default WorkManager Provider in your manifest for you to avoid this error. Have you added this in your manifest:

<provider
  android:name="androidx.work.impl.WorkManagerInitializer"
  android:authorities="${applicationId}.workmanager-init"
  tools:node="remove" />

Let me know if you have this on your manifest.