在background.systemjob.SystemJobService.onCreate中调用WorkManagerImpl.getInstance导致java.lang.IllegalStateException

时间:2019-06-24 04:04:41

标签: android android-workmanager

我们正在使用以下WorkManager库

def work_version = "2.1.0-beta01"
implementation "androidx.work:work-runtime:$work_version"

我们的Application类已被修改如下,以便与WorkManager一起很好地工作

public class WeNoteApplication extends MultiDexApplication implements DefaultLifecycleObserver, Configuration.Provider {

    private static WeNoteApplication me;

    @Override
    public void onCreate() {
        super.onCreate();

        me = this;

        ...
    }

   public static WeNoteApplication instance() {
        return me;
    }

    @NonNull
    @Override
    public Configuration getWorkManagerConfiguration() {
        return new Configuration.Builder()
                .build();
    }
}

我们没有在WorkManager中添加任何与AndroidManifest.xml相关的代码。我们仅依靠Default initialization


这就是我们构造WorkManager

的方式
public static WorkManager getWorkManager() {
    WeNoteApplication weNoteApplication = WeNoteApplication.instance();
    return WorkManager.getInstance(weNoteApplication);
}

上面的函数在4个不同的地方被调用

  1. Activity
  2. Fragment
  3. BroadcastReceiver
  4. IntentService

仍然,我们在生产中收到以下崩溃日志。它发生在Android 7.1,Android 8.1,Android 9(到目前为止)

java.lang.RuntimeException: 
  at android.app.ActivityThread.handleCreateService (ActivityThread.java:3265)
  at android.app.ActivityThread.-wrap5 (ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1598)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:241)
  at android.app.ActivityThread.main (ActivityThread.java:6274)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:886)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776)
Caused by: java.lang.IllegalStateException: 
  at androidx.work.impl.WorkManagerImpl.getInstance (WorkManagerImpl.java:142)
  at androidx.work.impl.background.systemjob.SystemJobService.onCreate (SystemJobService.java:53)
  at android.app.ActivityThread.handleCreateService (ActivityThread.java:3255)
  at android.app.ActivityThread.-wrap5 (ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1598)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:241)
  at android.app.ActivityThread.main (ActivityThread.java:6274)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:886)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776)

如您在崩溃中看到的那样,崩溃是内部WorkManager而非我们的应用程序引起的。

通过查看https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/work/workmanager/src/main/java/androidx/work/impl/WorkManagerImpl.java#142

WorkManagerImpl

/**
 * Retrieves the singleton instance of {@link WorkManagerImpl}.
 *
 * @param context A context for on-demand initialization.
 * @return The singleton instance of {@link WorkManagerImpl}
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public static @NonNull WorkManagerImpl getInstance(@NonNull Context context) {
    synchronized (sLock) {
        WorkManagerImpl instance = getInstance();
        if (instance == null) {
            Context appContext = context.getApplicationContext();
            if (appContext instanceof Configuration.Provider) {
                initialize(
                        appContext,
                        ((Configuration.Provider) appContext).getWorkManagerConfiguration());
                instance = getInstance(appContext);
            } else {
                throw new IllegalStateException("WorkManager is not initialized properly.  You "
                        + "have explicitly disabled WorkManagerInitializer in your manifest, "
                        + "have not manually called WorkManager#initialize at this point, and "
                        + "your Application does not implement Configuration.Provider.");
            }

IllegalStateException

有两种可能性
  1. appContext为空。 (怎么可能为空?)
  2. appContext未实现Configuration.Provider(但是我们已经在Configuration.Provider中实现了WeNoteApplication吗?)

要弄清楚上下文如何从WorkManagerImpl传递到SystemJobService,我们看一下 androidx.work.impl.background.systemjob.SystemJobService.onCreate-https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/work/workmanager/src/main/java/androidx/work/impl/background/systemjob/SystemJobService.java#53

SystemJobService

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@RequiresApi(WorkManagerImpl.MIN_JOB_SCHEDULER_API_LEVEL)
public class SystemJobService extends JobService implements ExecutionListener {
    private static final String TAG = Logger.tagWithPrefix("SystemJobService");
    private WorkManagerImpl mWorkManagerImpl;
    private final Map<String, JobParameters> mJobParameters = new HashMap<>();
    @Override
    public void onCreate() {
        super.onCreate();
        mWorkManagerImpl = WorkManagerImpl.getInstance(getApplicationContext());

一见钟情。但是,尽管我已经在WorkManagerImpl中实现了Configuration.Provider,但为什么Configuration.Provider无法获得Application?是因为我正在使用MultiDexApplication吗?


查找

在SystemJobService中,由于自动备份原因,它可以期望为null,并且可以从WorkManagerImpl.getInstance容忍null。

@Override
public void onCreate() {
    super.onCreate();
    mWorkManagerImpl = WorkManagerImpl.getInstance(getApplicationContext());
    if (mWorkManagerImpl == null) {
        // This can occur if...
        // 1. The app is performing an auto-backup.  Prior to O, JobScheduler could erroneously
        //    try to send commands to JobService in this state (b/32180780).  Since neither
        //    Application#onCreate nor ContentProviders have run, WorkManager won't be
        //    initialized.  In this case, we should ignore all JobScheduler commands and tell it
        //    to retry.

但是,当前的WorkManagerImpl.getInstance(Context context)实现是,它将永远不会返回null而是抛出IllegalStateException

这两个SystemJobService.onCreate逻辑和WorkManagerImpl.getInstance逻辑之间不存在冲突吗?


问题跟踪器

我将观察结果提交给https://issuetracker.google.com/issues/135858602

1 个答案:

答案 0 :(得分:1)

这是一个错误。一个修复程序将在本周晚些时候在WorkManager 2.1.0-rc01中提供。