我正在尝试遵循https://developer.android.com/training/dependency-injection/hilt-jetpack#workmanager中的指南,并遇到以下错误
E/WM-WorkerFactory: Could not instantiate com.example.android.hilt.ExampleWorker
java.lang.NoSuchMethodException: <init> [class android.content.Context, class androidx.work.WorkerParameters]
为了重现此问题,我在Dagger Hilt Example Repo中添加了根据实例编写的示例代码
class ExampleWorker @WorkerInject constructor(
@Assisted appContext: Context,
@Assisted workerParams: WorkerParameters,
val workerDependency: AppNavigator
) : Worker(appContext, workerParams) {
override fun doWork(): Result {
Log.d("WORKER", "I am the worker, got dependency: $workerDependency")
return Result.success()
}
}
注意:
AppNavigator
在NavigationModule中以@Binds abstract fun bindNavigator(impl: AppNavigatorImpl): AppNavigator
。 另请注意,将AppNavigator
替换为AppDatabase
并没有帮助。
这就是我从@Singleton
开始工作的方式>
MainActivity
不确定到底是什么问题。
这里是完整
修补程序文件,供完整参考 override fun onStart() {
super.onStart()
enqueueWorker(applicationContext)
}
private fun enqueueWorker(context: Context) {
val request = OneTimeWorkRequestBuilder<ExampleWorker>().build()
WorkManager.getInstance(context).enqueue(request)
}
我正在尝试遵循来自https://developer.android.com/training/dependency-injection/hilt-jetpack#workmanager的指南,并遇到以下错误E / WM-WorkerFactory:无法实例化com...。
根据From 2d02d1b13b6c29d99ecf94d9f874b16ff8d451cf Mon Sep 17 00:00:00 2001
From: Hossain Khan <[email protected]>
Date: Fri, 12 Jun 2020 23:32:52 -0400
Subject: [PATCH] [BUG] Worker can not be injected using Dagger Hilt
`@WorkerInject`
The example code is taken from https://developer.android.com/training/dependency-injection/hilt-jetpack#workmanager
Code is applied on top of https://github.com/googlecodelabs/android-hilt/tree/solution
---
app/build.gradle | 6 ++++++
.../com/example/android/hilt/ExampleWorker.kt | 20 +++++++++++++++++++
.../example/android/hilt/LogApplication.kt | 14 ++++++++++++-
.../example/android/hilt/ui/MainActivity.kt | 14 +++++++++++++
4 files changed, 53 insertions(+), 1 deletion(-)
create mode 100644 app/src/main/java/com/example/android/hilt/ExampleWorker.kt
diff --git a/app/build.gradle b/app/build.gradle
index 4c04d61..d9f4c8c 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -44,6 +44,9 @@ android {
sourceCompatibility 1.8
targetCompatibility 1.8
}
+ kotlinOptions {
+ jvmTarget = '1.8'
+ }
}
dependencies {
@@ -64,8 +67,11 @@ dependencies {
androidTestImplementation "androidx.test:rules:1.2.0"
androidTestImplementation "androidx.test.espresso:espresso-core:3.2.0"
+ implementation "androidx.work:work-runtime-ktx:2.3.4"
+
// Hilt dependencies
implementation "com.google.dagger:hilt-android:$hilt_version"
+ implementation 'androidx.hilt:hilt-work:1.0.0-alpha01'
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
// Hilt testing dependencies
diff --git a/app/src/main/java/com/example/android/hilt/ExampleWorker.kt b/app/src/main/java/com/example/android/hilt/ExampleWorker.kt
new file mode 100644
index 0000000..bec00ee
--- /dev/null
+++ b/app/src/main/java/com/example/android/hilt/ExampleWorker.kt
@@ -0,0 +1,20 @@
+package com.example.android.hilt
+
+import android.content.Context
+import android.util.Log
+import androidx.hilt.Assisted
+import androidx.hilt.work.WorkerInject
+import androidx.work.Worker
+import androidx.work.WorkerParameters
+import com.example.android.hilt.navigator.AppNavigator
+
+class ExampleWorker @WorkerInject constructor(
+ @Assisted appContext: Context,
+ @Assisted workerParams: WorkerParameters,
+ val workerDependency: AppNavigator
+) : Worker(appContext, workerParams) {
+ override fun doWork(): Result {
+ Log.d("WORKER", "I am the worker, got dependency: $workerDependency")
+ return Result.success()
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/example/android/hilt/LogApplication.kt b/app/src/main/java/com/example/android/hilt/LogApplication.kt
index 2c87931..de389e2 100644
--- a/app/src/main/java/com/example/android/hilt/LogApplication.kt
+++ b/app/src/main/java/com/example/android/hilt/LogApplication.kt
@@ -17,7 +17,19 @@
package com.example.android.hilt
import android.app.Application
+import androidx.hilt.work.HiltWorkerFactory
+import androidx.work.Configuration
import dagger.hilt.android.HiltAndroidApp
+import javax.inject.Inject
@HiltAndroidApp
-class LogApplication : Application()
+class LogApplication : Application(), Configuration.Provider {
+
+ @Inject
+ lateinit var workerFactory: HiltWorkerFactory
+
+ override fun getWorkManagerConfiguration() =
+ Configuration.Builder()
+ .setWorkerFactory(workerFactory)
+ .build()
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/example/android/hilt/ui/MainActivity.kt b/app/src/main/java/com/example/android/hilt/ui/MainActivity.kt
index 8c78072..15000e2 100644
--- a/app/src/main/java/com/example/android/hilt/ui/MainActivity.kt
+++ b/app/src/main/java/com/example/android/hilt/ui/MainActivity.kt
@@ -16,8 +16,12 @@
package com.example.android.hilt.ui
+import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
+import androidx.work.OneTimeWorkRequestBuilder
+import androidx.work.WorkManager
+import com.example.android.hilt.ExampleWorker
import com.example.android.hilt.R
import com.example.android.hilt.navigator.AppNavigator
import com.example.android.hilt.navigator.Screens
@@ -50,4 +54,14 @@ class MainActivity : AppCompatActivity() {
finish()
}
}
+
+ override fun onStart() {
+ super.onStart()
+ enqueueWorker(applicationContext)
+ }
+
+ private fun enqueueWorker(context: Context) {
+ val request = OneTimeWorkRequestBuilder<ExampleWorker>().build()
+ WorkManager.getInstance(context).enqueue(request)
+ }
}
--
2.24.3 (Apple Git-128)
,要使用WorkManager Configuration and Initialization documentation上的Configuration.Provider
接口,您必须Application
: