在multidex应用中使用Dagger注入依赖时出现验证错误



一个库的示例应用程序有~67k个方法。它具有multidex功能,可以克服65k方法限制。不幸的是,启用multidex后,当试图在主活动中注入EndpointAdapter时,应用程序在VerifyError上崩溃。

当应用程序被保护且multidex被禁用时不会出现此问题,因此它一定是由multidex和Dagger 1问题引起的。

我确信EndpointAdapter是在主要的索引文件,但由Dagger生成的一些类位于由multidex生成的第二个索引文件。此问题发生在带有API <21(如。在genymotion与KitKat 4.4.4).

知道为什么VerifyError会崩溃吗?

FATAL EXCEPTION: main
Process: pl.toro.libsample.debug, PID: 11775
java.lang.VerifyError: pl/toro/lib/network/EndpointAdapter
    at java.lang.Class.getDeclaredConstructors(Native Method)
    at java.lang.Class.getDeclaredConstructors(Class.java:574)
    at dagger.internal.loaders.ReflectiveAtInjectBinding.getConstructorsForType(ReflectiveAtInjectBinding.java:232)
    at dagger.internal.loaders.ReflectiveAtInjectBinding.create(ReflectiveAtInjectBinding.java:168)
    at dagger.internal.FailoverLoader.getAtInjectBinding(FailoverLoader.java:74)
    at dagger.internal.Linker.createBinding(Linker.java:224)
    at dagger.internal.Linker.linkRequested(Linker.java:141)
    at dagger.ObjectGraph$DaggerObjectGraph.getInjectableTypeBinding(ObjectGraph.java:309)
    at dagger.ObjectGraph$DaggerObjectGraph.inject(ObjectGraph.java:279)
    at pl.toro.lib.app.BaseApplication.inject(BaseApplication.java:135)
    ...

下面是MultiDex标签的输出

VM with version 1.6.0 does not have multidex support
install
MultiDexExtractor.load(/data/app/pl.toro.libsample.debug-1.apk, false)
Detected that extraction must be performed.
Extraction is needed for file /data/data/pl.toro.libsample.debug/code_cache/secondary-dexes/pl.toro.libsample.debug-1.apk.classes2.zip
Extracting /data/data/pl.toro.libsample.debug/code_cache/secondary-dexes/pl.toro.libsample.debug-1.apk.classes-1477675005.zip
Renaming to /data/data/pl.toro.libsample.debug/code_cache/secondary-dexes/pl.toro.libsample.debug-1.apk.classes2.zip
Extraction success - length /data/data/pl.toro.libsample.debug/code_cache/secondary-dexes/pl.toro.libsample.debug-1.apk.classes2.zip: 187777
load found 1 secondary dex files
install done

编辑

我已经切换到dagger2,这个问题已经解决了。dagger2不再使用反射,这是这个问题的主要因素。

正如这篇博文[1]所指出的那样,您是否在安装多索引后创建dagger图形?所以在MultiDexApplication#attachBaseContext中,在调用super之后(或者自己调用MultiDex.install())。

[1] https://developers.soundcloud.com/blog/congratulations-you-have-a-lot-of-code-remedying-androids-method-limit-part-2

最新更新