google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.42k stars 2.01k forks source link

XX_MembersInjector.class is repeatedly generated in the wrong package #955

Closed KingJ0629 closed 4 years ago

KingJ0629 commented 6 years ago

demo's url : https://github.com/kj387948407/DaggerBugTest

dagger2 generates Middle_MembersInjector.class should be in this package com.uama.happinesscommunity.base at the module base, but in the demo, The file is repeatedly generated in module mine and module user, respectively, and build the project, throws exception: `Error:Execution failed for task ':app:transformClassesWithDexForDebug'.

com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lcom/uama/happinesscommunity/base/Middle_MembersInjector;`

and mine another project throws exception `Error:Execution failed for task ':app:transformClassesWithJarMergingForXflcDevelop'.

com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/uama/happinesscommunity/base/Middle_MembersInjector.class`

Middle_MembersInjector.class exactly the same, and in the same directory, resulting in an error. Do not know whether this understanding is correct.

HassanIbraheem commented 6 years ago

Just faced this issue as well. Found #814 which seems to be the same.

The generated MembersInjector is in the right package, but is generated in multiple modules.

If I understood it correctly, this happens when your Middle class doesn't need any injections, so dagger doesn't generate a MembersInjector for it in base module. When dagger processes the other modules, and doesn't find that MembersInjector, it generates one, in each module.

The duplicate generated MembersInjector are actually not used at all, and you'll only get the error when you assemble the top-level module (app).

A workaround that seems to work, is adding an unneeded @Inject field to Middle class. This will make dagger generate the MembersInjector in the base module, and it won't be generated in other modules any more.

KingJ0629 commented 6 years ago

@hsnim You understand quite right, I met this problem a half month ago, it is the same way as you temporarily solve for a moment.

ronshapiro commented 6 years ago

@hsnim is correct - additionally, this is a build issue - there's nothing wrong with generating the same type twice, and most build systems should be able to dedupe this (especially since they're the exact same file without any differences). You can file an issue with the gradle plugin if you'd like. I think (thought not positive) that proguard should also solve this.

Unfortunately, there's no way for Dagger to be alerted about Middle's existence until it's used in a context in which Dagger is invoked.

wangjing53406 commented 6 years ago

@hsnim it's work for me, thx. You must add dagger's annotationProcessor to base module

BzCoder commented 5 years ago

@hsnim thx, you are exactly right,I met the same problem and solved it with the method as you provided.

mchiu-snap commented 4 years ago

Filed https://issuetracker.google.com/issues/149460619 for d8 team, including workaround patch.

Chang-Eric commented 4 years ago

Reopening this issue since I don't think we need to be generating a MembersInjector for those classes without @Inject fields at all.

guilherme-pereira-ifood commented 4 years ago

@Chang-Eric should we keep the kapt/annotationProcessor for dagger compiler on the module holding the base classes?

Chang-Eric commented 4 years ago

I feel like it probably doesn't hurt as I assume future changes could add injected members to the base class or there may be other injected types later added to that module. The one concern I can think of would be if it adds to your build time but I'm guessing that if nothing is generated then kapt/annotation processing doesn't add much time to your build.