WorkManager未初始化异常,即使未在清单中禁用WorkManagerInitializer

问题描述 投票:0回答:1

我有一个使用WorkManager进行多个后台任务的应用。最近,即使未在清单中禁用WorkManagerInitializer,由于未初始化WM,我的生产日志中也发生了很多崩溃。

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:161)
        ...

WM文档说

默认情况下,WorkManager使用内置的ContentProvider自动初始化自身。 ContentProvider是在Application对象之前创建并运行的,因此在大多数情况下,这允许在代码可以运行之前设置WorkManager单例。

最有趣的部分是在大多数情况下。我想知道在什么情况下无法在应用启动前正确初始化它。

我最初的猜测是,我的代码中的错误可能创建了太多的WM任务,而这些任务初始化所需的时间太长,但我无法以这种方式重新创建崩溃。

任何人以前都见过这种行为,并且知道这里会发生什么?为什么在应用启动前未初始化WorkManager

android android-workmanager
1个回答
0
投票

在清单文件中添加以下代码

   <provider
    android:name="androidx.work.impl.WorkManagerInitializer"
    android:authorities="${applicationId}.workmanager-init"
    android:exported="false"
    tools:node="remove" />
© www.soinside.com 2019 - 2024. All rights reserved.