IllegalStateException: WorkManager 已初始化

具有以下依赖项:

dependencies {
    implementation "androidx.work:work-runtime:2.0.1"
    androidTestImplementation "androidx.work:work-testing:2.0.1"
}

第二次运行此代码时:

Configuration config = new Configuration.Builder().build();
WorkManager.initialize(getApplicationContext(), config);

this.workManager = WorkManager.getInstance();

我收到此错误消息:

java.lang.IllegalStateException: WorkManager is already initialized.
Did you try to initialize it manually without disabling WorkManagerInitializer?
See WorkManager#initialize(Context, Configuration) or the class level Javadoc for more information.

它还会在本机端抛出分段错误:

A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR),
fault addr 0x878 in tid 10892 (ova.workmanager),
pid 10892 (ova.workmanager)

这将是 的文档。WorkManager#initialize(Context, Configuration)


目的是防止在手动初始化期间崩溃(以便更改日志级别)。如何禁用 ?如果可能的话,我不想使用关键字。WorkManagerInitializerstatic


答案 1

这是如何替代提供者:androidx.work.impl.WorkManagerInitializer

<application>
    ...

    <!-- disable default provider -->
    <provider
        android:name="androidx.work.impl.WorkManagerInitializer"
        android:authorities="${applicationId}.workmanager-init"
        android:exported="false"
        android:enabled="false"/>

    <!-- register custom provider -->
    <provider
        android:name=".CustomWorkManagerInitializer"
        android:authorities="${applicationId}.WorkManagerInit"/>

</application>

源代码:自定义工作管理器初始化(在 Kotlin 中)。


除非注册其他提供程序,否则这将给出:

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.

并在以下位置注册:ContentProvidersrc/debug/Manifest.xml

public class WorkManagerInit extends ContentProvider {

    @Override
    public boolean onCreate() {
        if(getContext() != null) {
            Configuration config = new Configuration.Builder().build();
            WorkManager.initialize(getContext().getApplicationContext(), config);
        }
        return true;
    }
    ...
}

答案 2

WorkManager 2.6 开始,App Startup 在 WorkManager 内部使用。要提供自定义初始值设定项,您需要删除 androidx.startup 节点。

如果您未在应用中使用“应用启动”,则可以将其完全删除。

<provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    tools:node="remove">
</provider>

否则,仅删除 WorkManagerInitializer 节点。

<provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    android:exported="false"
    tools:node="merge">

    <!-- If you are using androidx.startup to initialize other components -->
    <meta-data
        android:name="androidx.work.WorkManagerInitializer"
        android:value="androidx.startup"
        tools:node="remove" />
</provider>

使用早于 2.6 的 WorkManager 版本时,请改为删除 workmanager-init:

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

推荐