WorkManager 无法在动态模块中注入刀柄
WorkManager cannot inject with hilt in dynamic module
当我在常规模块中使用 WorkManager + Hilt 时,该应用运行良好。但是,当我在动态模块中使用时,遇到以下问题
java.lang.NoSuchMethodException: <init> [class android.content.Context, class androidx.work.WorkerParameters]
at java.lang.Class.getConstructor0(Class.java:2320)
at java.lang.Class.getDeclaredConstructor(Class.java:2166)
at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:242)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:136)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
我从这个答案中找到了解决方案
Hilt 似乎还不支持动态模块中的注释。所以我必须创建工厂并将其添加到 DelegatingWorkerFactory。在应用程序中注册 DelegatingWorkerFactory。
当我在常规模块中使用 WorkManager + Hilt 时,该应用运行良好。但是,当我在动态模块中使用时,遇到以下问题
java.lang.NoSuchMethodException: <init> [class android.content.Context, class androidx.work.WorkerParameters]
at java.lang.Class.getConstructor0(Class.java:2320)
at java.lang.Class.getDeclaredConstructor(Class.java:2166)
at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:242)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:136)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
我从这个答案中找到了解决方案
Hilt 似乎还不支持动态模块中的注释。所以我必须创建工厂并将其添加到 DelegatingWorkerFactory。在应用程序中注册 DelegatingWorkerFactory。