我们正在使用以下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个不同的地方被调用
Activity
Fragment
BroadcastReceiver
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
工作管理器
/**
* 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
有2种可能性appContext
为空。 (怎么可能为空?)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#53SystemJobService
@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
最佳答案
这是一个错误。修复程序将于本周晚些时候在WorkManager 2.1.0-rc01
中提供。
关于android - 在background.systemjob.SystemJobService.onCreate中调用WorkManagerImpl.getInstance导致java.lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56729702/