Dagger2 - "Unused" modules in the class of generated components

The My Dagger2 Component class contains 3 modules that I am trying to use to enter field dependencies into the Android activity class. There are comments in the generated Component file saying that all modules are not used, linking this page for more information.

My Activity class calls the Component injection (Activity) method and has fields annotated for injection that are provided by the modules, so I'm not sure why there aren't any Providers for this injection in the generated Component file.

My code is below, thanks for the help!

Component class generated:

public final class DaggerMainComponent implements MainComponent { private DaggerMainComponent(Builder builder) { assert builder != null; } public static Builder builder() { return new Builder(); } public static MainComponent create() { return builder().build(); } @Override public void inject(Activity activity) { MembersInjectors.<Activity>noOp().injectMembers(activity); } public static final class Builder { private Builder() {} public MainComponent build() { return new DaggerMainComponent(this); } /** * @deprecated This module is declared, but an instance is not used in the component. This method is a no-op. For more, see https://google.imtqy.com/dagger/unused-modules. */ @Deprecated public Builder daoModule(DaoModule daoModule) { Preconditions.checkNotNull(daoModule); return this; } /** * @deprecated This module is declared, but an instance is not used in the component. This method is a no-op. For more, see https://google.imtqy.com/dagger/unused-modules. */ @Deprecated public Builder repositoryModule(RepositoryModule repositoryModule) { Preconditions.checkNotNull(repositoryModule); return this; } /** * @deprecated This module is declared, but an instance is not used in the component. This method is a no-op. For more, see https://google.imtqy.com/dagger/unused-modules. */ @Deprecated public Builder portableModule(PortableModule portableModule) { Preconditions.checkNotNull(portableModule); return this; } } } 

Class of non-generated components :

 @Component(modules={DaoModule.class,RepositoryModule.class,PortableModule.class}) public interface MainComponent { void inject(Activity activity); } 

Module classes : Is there a problem that one module provides an object of dependency on another object provided by another module belonging to the same component?

 @Module public class DaoModule { private DatabaseHelper databaseHelper; public DaoModule(DatabaseHelper databaseHelper){ this.databaseHelper = databaseHelper; } @Provides public Dao<Player,Integer> providePlayerDao(){ return databaseHelper.getPlayerDao(); } @Provides public Dao<GamePlayed,Integer> provideGamePlayedDao() { try { return databaseHelper.getDao(GamePlayed.class); } catch (SQLException e) { return null; } } @Provides public Dao<GamePlayer,Integer> provideGamePlayerDao() { try { return databaseHelper.getDao(GamePlayer.class); } catch (SQLException e) { return null; } } } ... @Module public class RepositoryModule { @Provides public IGameResultRepository provideGameResultRepository( Dao<Player,Integer> playerDao, Dao<GamePlayed,Integer> gameDao, Dao<GamePlayer, Integer> gamePlayerDao) { return new OrmliteGameResultRepository(playerDao,gameDao,gamePlayerDao); } } @Module public class PortableModule { @Provides public GameResultListener provideGameResultListener(IGameResultRepository gameResultRepository){ return new GameResultListener(gameResultRepository); } } 

Application Class:

 public class AppStart extends Application { private MainComponent mainComponent; @Override public void onCreate() { super.onCreate(); DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext()); mainComponent = DaggerMainComponent.builder() .daoModule(new DaoModule(databaseHelper)) .build(); } public MainComponent getMainComponent(){ return mainComponent; } } 

Action class:

 public class MyActivity extends Activity { @Inject GameResultListener gameResultListener; @Inject Dao<Player,Integer> dao; @Inject IGameResultRepository repository; @Override protected void onCreate(Bundle state) { super.onCreate(state); ((AppStart)this.getApplication()).getMainComponent().inject(this); 
+5
source share
1 answer

Question 1: Why are my modules marked as "unused"?

You have not provided the correct injection site! Accordingly, your component interface is one with the only injection site android.app.Activity . Since android.app.Activity does not have @Inject annotations in its fields, then you get an element injector without an operation. Similarly, your modules are marked as unused because none of them are actually used as dependency sources for android.app.Activity . To fix this, modify your component:

 void inject(Activity activity); 

in

 void inject(MyActivity myActivity); 

Question 2:

Is there a problem with one module providing an object of dependency on another object provided by another module belonging to the same component?

No, that's fine. To illustrate, take a simple graph of objects:

 public class Foo { public Foo(FooDependency fooDependency) {} } public class FooDependency { FooDependency(String name) {} } 

We want to introduce it to the following class using Dagger:

 public class FooConsumer { @Inject Foo foo; private FooConsumer() {} } 

We would like to reuse the binding of the FooDependency module, so we will write two separate modules:

 @Module public class FooModule { @Provides Foo foo(FooDependency fooDependency) { return new Foo(fooDependency); } } @Module public class FooDependencyModule { @Provides FooDependency fooDependency() { return new FooDependency("name"); } } 

And the following component interface:

 @Component(modules = {FooModule.class, FooDependencyModule.class}) public interface FooComponent { void inject(FooConsumer fooConsumer); } 

The generated DaggerFooComponent component contains the following code that will correctly use FooDependency from a separate FooDependencyModule module to enter Foo :

  @SuppressWarnings("unchecked") private void initialize(final Builder builder) { this.fooDependencyProvider = FooDependencyModule_FooDependencyFactory.create(builder.fooDependencyModule); this.fooProvider = FooModule_FooFactory.create(builder.fooModule, fooDependencyProvider); this.fooConsumerMembersInjector = FooConsumer_MembersInjector.create(fooProvider); } 
+3
source

Source: https://habr.com/ru/post/1260747/


All Articles