google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.42k stars 2.01k forks source link

HiltWorker not working after app get killed #3177

Closed shanih closed 2 years ago

shanih commented 2 years ago

Hi. Using HiltWorker and HiltWorkerFactory works just fine when the app is alive, but after it gets killed, the worker won't initialize. When commenting out every hilt-related code, including the provider in the manifest, the worker works after app killing.

my code: worker -

@HiltWorker
class NotificationWorker @AssistedInject constructor(
    @Assisted val appContext: Context,
    @Assisted workerParams: WorkerParameters,
//   injected classes...
) : Worker(appContext, workerParams) {...}

App -

@HiltAndroidApp
class KorenApplication : Application(), Configuration.Provider {
    @Inject
    lateinit var workerFactory: HiltWorkerFactory

    override fun getWorkManagerConfiguration() =
        Configuration.Builder()
            .setWorkerFactory(workerFactory)
            .setMinimumLoggingLevel(android.util.Log.VERBOSE)
            .build()
}

manifest - tried both

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

and

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

logs when trying to start the worker:

2022-01-20 14:54:01.061 24083-24083/com.maatayim.koren.dev D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2022-01-20 14:54:01.079 24083-24083/com.maatayim.koren.dev D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2022-01-20 14:54:01.247 24083-24083/com.maatayim.koren.dev D/WM-PackageManagerHelper: androidx.work.impl.background.systemjob.SystemJobService enabled
2022-01-20 14:54:01.326 24083-24123/com.maatayim.koren.dev D/WM-SystemJobScheduler: Scheduling work ID d97cd55d-1f9f-404f-bd26-84bd26029478 Job ID 27
2022-01-20 14:54:01.339 24083-24123/com.maatayim.koren.dev D/WM-GreedyScheduler: Starting work for d97cd55d-1f9f-404f-bd26-84bd26029478
2022-01-20 14:54:01.347 24083-24127/com.maatayim.koren.dev D/WM-Processor: WorkerWrapper could not be found for d97cd55d-1f9f-404f-bd26-84bd26029478
2022-01-20 14:54:01.350 24083-24127/com.maatayim.koren.dev D/WM-GreedyScheduler: Cancelling work ID d97cd55d-1f9f-404f-bd26-84bd26029478
2022-01-20 14:54:01.369 24083-24129/com.maatayim.koren.dev D/WM-PackageManagerHelper: androidx.work.impl.background.systemalarm.RescheduleReceiver enabled
2022-01-20 14:54:01.380 24083-24129/com.maatayim.koren.dev D/WM-SystemJobScheduler: Scheduling work ID 7a0f42ab-87c1-48bb-a87d-670a5736ace9 Job ID 28
2022-01-20 14:54:01.384 24083-24129/com.maatayim.koren.dev D/WM-Processor: Processor stopping background work d97cd55d-1f9f-404f-bd26-84bd26029478
2022-01-20 14:54:01.384 24083-24129/com.maatayim.koren.dev D/WM-WorkerWrapper: Work interrupted for null
2022-01-20 14:54:01.386 24083-24129/com.maatayim.koren.dev D/WM-WorkerWrapper: WorkSpec null is already done. Not interrupting.
2022-01-20 14:54:01.386 24083-24129/com.maatayim.koren.dev D/WM-Processor: WorkerWrapper interrupted for d97cd55d-1f9f-404f-bd26-84bd26029478
2022-01-20 14:54:01.387 24083-24129/com.maatayim.koren.dev D/WM-StopWorkRunnable: StopWorkRunnable for d97cd55d-1f9f-404f-bd26-84bd26029478; Processor.stopWork = true
2022-01-20 14:54:01.389 24083-24129/com.maatayim.koren.dev D/WM-Processor: Processor stopping background work d97cd55d-1f9f-404f-bd26-84bd26029478
2022-01-20 14:54:01.389 24083-24129/com.maatayim.koren.dev D/WM-WorkerWrapper: Work interrupted for null
2022-01-20 14:54:01.390 24083-24129/com.maatayim.koren.dev D/WM-WorkerWrapper: WorkSpec null is already done. Not interrupting.
2022-01-20 14:54:01.390 24083-24129/com.maatayim.koren.dev D/WM-Processor: WorkerWrapper interrupted for d97cd55d-1f9f-404f-bd26-84bd26029478
2022-01-20 14:54:01.391 24083-24129/com.maatayim.koren.dev D/WM-StopWorkRunnable: StopWorkRunnable for d97cd55d-1f9f-404f-bd26-84bd26029478; Processor.stopWork = true
2022-01-20 14:54:01.393 24083-24129/com.maatayim.koren.dev D/WM-WorkerWrapper: Work interrupted for Work [ id=d97cd55d-1f9f-404f-bd26-84bd26029478, tags={ ... } ]
2022-01-20 14:54:01.397 24083-24129/com.maatayim.koren.dev D/WM-WorkerWrapper: Work interrupted for Work [ id=d97cd55d-1f9f-404f-bd26-84bd26029478, tags={ ... } ]
Chang-Eric commented 2 years ago

Hm, I don't think could be an issue related to Hilt, since we just help you create the WorkerFactory but the rest is up to your configuration and WorkManager. This seems more like a WorkManager issue and that maybe when deleting the Hilt portion something else was accidentally deleted. If you're seeing something where the Worker object can't be constructed I think that could be something in Hilt, but so far I don't see anything like that in your logs.