ClassNotFoundException on Worker class



我在生产中遇到了很多奇怪的错误,这些错误只发生在少数用户身上,所以99%的用户都还好。

java.lang.Error: java.lang.ClassNotFoundException: com.packagename.core.Translations.TranslationWorker
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1119)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.ClassNotFoundException: com.packagename.core.Translations.TranslationWorker
at java.lang.Class.classForName(Class.java:-2)
at java.lang.Class.forName(Class.java:324)
at java.lang.Class.forName(Class.java:285)
at com.packagename.core.translations.TranslationWorkerFactory.createWorker(TranslationWorkerFactory.kt:19)
at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:83)
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:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.packagename.core.Translations.TranslationWorker" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.packagename.appname-2/base.apk", zip file "/data/app/com.packagename.appname-2/split_config.armeabi_v7a.apk", zip file "/data/app/com.packagename.appname-2/split_config.cs.apk", zip file "/data/app/com.packagename.appname-2/split_config.xhdpi.apk"],nativeLibraryDirectories=[/data/app/com.packagename.appname-2/lib/arm, /data/app/com.packagename.appname-2/base.apk!/lib/armeabi-v7a, /data/app/com.packagename.appname-2/split_config.armeabi_v7a.apk!/lib/armeabi-v7a, /data/app/com.packagename.appname-2/split_config.cs.apk!/lib/armeabi-v7a, /data/app/com.packagename.appname-2/split_config.xhdpi.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at java.lang.Class.classForName(Class.java:-2)
at java.lang.Class.forName(Class.java:324)
at java.lang.Class.forName(Class.java:285)
at com.packagename.core.translations.TranslationWorkerFactory.createWorker(TranslationWorkerFactory.kt:19)
at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:83)
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:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

这是TranslationWorkerFactory的代码。我正在使用dagger辅助注入将依赖项注入到工作类。

class TranslationWorkerFactory @Inject constructor(
private val workerFactories: Map<Class<out ListenableWorker>, @JvmSuppressWildcards Provider<ITranslationWorkerFactory>>
) : WorkerFactory() {
override fun createWorker(
appContext: Context,
workerClassName: String,
workerParameters: WorkerParameters
): ListenableWorker? {
val foundEntry =
workerFactories.entries.find { Class.forName(workerClassName).isAssignableFrom(it.key) }
val factoryProvider = foundEntry?.value
?: throw IllegalArgumentException("unknown worker class _name: $workerClassName")
return factoryProvider.get().create(appContext, workerParameters)
}
}

我不知道为什么会发生这种事。工作人员所做的是下载和解析大型json,因此这可能是抛出异常的原因,但这只是我的猜测,如果它可能是相关的。

有人知道发生这种情况的原因吗?

感谢

例如,您很可能已经移动/重命名了工人类(TranslationWorker(​在重构期间。

如果是请继续阅读。

复制步骤:

  • 启动应用程序(旧版本(并等待工作程序启动
  • 将worker类移到另一个包中,重新生成应用程序
  • 启动应用程序并等待系统运行工作程序

到目前为止,我还没有找到好的解决方案。但有一个可行的选择。主要思想-是用WorkerFactory中的实际路径替换工人类的错误路径。更多细节在这个后

相关内容

  • 没有找到相关文章

最新更新