我们正在使用以下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
而非我们的应用程序引起的。
/**
* 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
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#53
@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
逻辑之间不存在冲突吗?
答案 0 :(得分:1)
这是一个错误。一个修复程序将在本周晚些时候在WorkManager 2.1.0-rc01
中提供。