Search code examples
android-workmanagerdagger-hilturbanairship.com

Disable WorkManager Initializer when using app startup


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


Solution

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