1

I'm using the WorkManager library(version: 1.0.0-alpha12). In my app I'm creating a new process using the below xml line in AndroidManifest.

android:process=":myprocess"

This is how I use the WorkManager:

 public static void startCheckStatusWorker() {
    WorkManager manager = WorkManager.getInstance();
    String uniqueName = "check_status_worker";
    PeriodicWorkRequest.Builder builder = new PeriodicWorkRequest.Builder(CheckStatusWorker.class, 1, TimeUnit.DAYS);
    builder.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build());
    PeriodicWorkRequest request = builder.build();
    manager.enqueueUniquePeriodicWork(uniqueName, ExistingPeriodicWorkPolicy.KEEP, request);
 }

But when I call this method from the new process, then the app crashes. This is the exception thrown,

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.java:139)

If I call the same method, from the normal app process it works well and I don't disable the WorkManagerInitializer in my app manifest as it suggests.

Is there a way to get the WorkManager instance from the new process?

Any suggestion would be appreciated. Thanks.

chathura
  • 3,362
  • 6
  • 41
  • 68
  • See my post here: https://stackoverflow.com/questions/51845895/android-workmanager-run-job-in-a-fixed-process/65446768#65446768 – Scott Driggers Dec 25 '20 at 09:13

1 Answers1

2

If you REALLY REALLY need the service to be in a separate process, you can disable the WorkManagerInitializer in the AndroidManifest. Goes something like this:

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

(For the official documentation refer to https://developer.android.com/topic/libraries/architecture/workmanager/advanced/custom-configuration#java)

After disabling it , in your Application class (if you don't have one you should create your own and extend the android Application class), and call WorkManager.initialize().

That fixed it for me.

But please note, according to some answers in the google issues forums(https://issuetracker.google.com/issues/79993883), the workmanager will still use the main-process in order to do work (even if it's called from a second process).

What I recommend? If there is no serious advantage from having the service on a non-main-process, you should just make the service be on the main process since it would be harder to maintain (for example static global vars will have two instances, one for each process)

sonique
  • 4,539
  • 2
  • 30
  • 39
edy
  • 452
  • 1
  • 3
  • 10
  • Is there a way to make workmanager work in a library seperate from app that is using this library? – ddog Jan 09 '19 at 13:10
  • i'm not sure , but I think you can. android project> your-lib>build.gradle has workmanager declared in it. app-module> build.gradle will have workmanager from your lib – edy Jan 10 '19 at 14:12