Disable WorkManager Initializer when using app startup

1k Views Asked by At

I'm using WorkManager 2.7.1 with Hilt and up till now I've always just removed app startup completely:

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

I'm now ingesting another library that is actually using app startup so I need to just stop the WorkManager initialisation as per the guidelines here

<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>

Unfortunately, however, I'm not sure this is actually doing what it's supposed to. I get errors whenever I try to access a Worker in the same way that I would get in the previous configuration if I missed out the removal of InitializationProvider. So is the documentation correct or is it just not valid for the latest WorkManager?

I have observed that the log from WorkManagerInitializer Initializing WorkManager with default configuration. does not happen with either of the options but for whatever reason Hilt is no longer creating the classes referenced by @HiltWorker

2

There are 2 best solutions below

2
ralepinski On BEST ANSWER

Yeah, Airship has a dependency on work manager which causes it load even if you remove from app startup. Here is the Github issue.

We provided a workaround in SDK 16.7:

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

            <meta-data
                android:name="androidx.work.WorkManagerInitializer"
                android:value="androidx.startup"
                tools:node="remove" />

            <meta-data
                android:name="com.urbanairship.AirshipInitializer"
                tools:node="remove" />
            
            <meta-data
                android:name="com.urbanairship.NoDependencyAirshipInitializer"
                android:value="androidx.startup" />
        </provider>
2
Barry Irvine On

Actually I think I understand it now. The other Initializer that I was using (AirshipInitializer) is declaring a dependency on WorkManagerInitializer so even though I've said to remove it, it gets started by the AirshipInitializer which then stops my Configuration.Provider code working and thus I can't instantiate the workers!