用dagger在组件化开发时,当有多个子类继承BasexxxFragment,里面都用@Inject来实例化对象的时候,编译报错:
BasexxxFragment_MembersInjector is defined multiple times。
AGPBI: {"kind":"error","text":"Type BaseBindFragment_MembersInjector is defined multiple times: /Users/Documents/android_project/taxidriverowner/taxidriver/build/intermediates/mixed_scope_dex_archive/out/096dfca1641ca500820ceed26817100b9ab72d8b6398592be460b1dcef6ffba9_0.jar:classes.dex, /Users/Documents/android_project/taxidriverowner/taxidriver/build/intermediates/mixed_scope_dex_archive/out/f7e7b7f5d31272aef30fb9cf12a2ab4c837a351a1ff24ef4e3f874fc7add7466_0.jar:classes.dex","sources":[{"file":"/User/Documents/android_project/taxidriverowner/taxidriver/build/intermediates/mixed_scope_dex_archive/out/096dfca1641ca500820ceed26817100b9ab72d8b6398592be460b1dcef6ffba9_0.jar"}],"tool":"D8"}
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:
Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
Type base.mvvm.BaseBindFragment_MembersInjector is defined multiple times: /Users/h/Documents/android_project/taxidriverowner/taxidriver/build/intermediates/mixed_scope_dex_archive/out/096dfca1641ca500820ceed26817100b9ab72d8b6398592be460b1dcef6ffba9_0.jar:classes.dex, /Users/Documents/android_project/taxidriverowner/taxidriver/build/intermediates/mixed_scope_dex_archive/out/f7e7b7f5d31272aef30fb9cf12a2ab4c837a351a1ff24ef4e3f874fc7add7466_0.jar:classes.dex
at com.android.builder.dexing.D8DexArchiveMerger.getExceptionToRethrow(D8DexArchiveMerger.java:128)
at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:115)
at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:111)
at com.android.build.gradle.internal.tasks.DexMergingWorkAction.merge(DexMergingTask.kt:805)
at com.android.build.gradle.internal.tasks.DexMergingWorkAction.run(DexMergingTask.kt:760)
at com.android.build.gradle.internal.profile.ProfileAwareWorkAction.execute(ProfileAwareWorkAction.kt:74)
at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:63)
at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:67)
at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:63)
at org.gradle.internal.classloader.ClassLoaderUtils.executeInClassloader(ClassLoaderUtils.java:97)
at org.gradle.workers.internal.NoIsolationWorkerFactory$1.lambda$execute$0(NoIsolationWorkerFactory.java:63)
at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:44)
at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:41)
at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:200)
at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:195)
at org.gradle.internal.operations.DefaultBuildOperationRunner$3.execute(DefaultBuildOperationRunner.java:75)
at org.gradle.internal.operations.DefaultBuildOperationRunner$3.execute(DefaultBuildOperationRunner.java:68)
at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:153)
at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:68)
at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:62)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.lambda$call$2(DefaultBuildOperationExecutor.java:76)
at org.gradle.internal.operations.UnmanagedBuildOperationWrapper.callWithUnmanagedSupport(UnmanagedBuildOperationWrapper.java:54)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:76)
at org.gradle.workers.internal.AbstractWorker.executeWrappedInBuildOperation(AbstractWorker.java:41)
at org.gradle.workers.internal.NoIsolationWorkerFactory$1.execute(NoIsolationWorkerFactory.java:60)
at org.gradle.workers.internal.DefaultWorkerExecutor.lambda$submitWork$2(DefaultWorkerExecutor.java:200)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runExecution(DefaultConditionalExecutionQueue.java:215)
at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runBatch(DefaultConditionalExecutionQueue.java:164)
at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.run(DefaultConditionalExecutionQueue.java:131)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: /Users/Documents/android_project/taxidriverowner/taxidriver/build/intermediates/mixed_scope_dex_archive/out/096dfca1641ca500820ceed26817100b9ab72d8b6398592be460b1dcef6ffba9_0.jar:classes.dex
at Version.fakeStackEntry(Version_2.2.64.java:0)
at com.android.tools.r8.utils.a0.a(SourceFile:89)
at com.android.tools.r8.D8.run(D8.java:11)
at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:113)
... 36 more
搜索一番,问题是当基类里面没有添加过@inject注解的情况下,多个子类添加了@inject注解,就会导致生成多个BasexxxFragment_MembersInjector,,解决办法:
写一个用不着的类,在基类里面用@inject注解将其实例化一下。
需要注意的是,基类需要是抽象类,注解实例化的对象不能是private的。
demo:
import javax.inject.Inject
class Unused @Inject constructor()
基类里面通过注解实例化一个它的对象
abstract class BaseBindFragment{
// 解决dagger组件化编译报错
@Inject
lateinit var unused: Unused
}
相关问题:
dragger2在组件化时出现的问题BaseLazyFragment_MembersInjector - 简书
google/dagger的issues
|