android – Dagger2 – 生成的组件类中的“未使用”模块

前端之家收集整理的这篇文章主要介绍了android – Dagger2 – 生成的组件类中的“未使用”模块前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的Dagger2 Component类包含3个模块,我试图用它们将字段依赖项注入到 Android Activity类中.生成的Component文件包含所有模块未使用的注释,链接page获取更多信息.

我的Activity类正在调用Component的inject(Activity)方法,并且模块提供了注释用于注入的字段,因此我不确定为什么生成的Component文件没有任何Providers来执行此注入.

我的代码如下,感谢您的帮助!

生成的组件类:

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.github.io/dagger/unused-modules.
     */
    @Deprecated
    public Builder daoModule(DaoModule daoModule) {
      Preconditions.checkNotNull(daoModule);
      return this;
    }

    /**
     * @deprecated This module is declared,see https://google.github.io/dagger/unused-modules.
     */
    @Deprecated
    public Builder repositoryModule(RepositoryModule repositoryModule) {
      Preconditions.checkNotNull(repositoryModule);
      return this;
    }

    /**
     * @deprecated This module is declared,see https://google.github.io/dagger/unused-modules.
     */
    @Deprecated
    public Builder portableModule(PortableModule portableModule) {
      Preconditions.checkNotNull(portableModule);
      return this;
    }
  }
}

生成组件类:

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

模块类:
是否有任何问题让一个模块提供一个对象,该对象依赖于属于同一组件的另一个模块提供的另一个对象?

@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);
    }

}

申请类别:

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;
    }
}

活动类:

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);

解决方法

问题1:为什么我的模块被标记为“未使用”?

您还没有提供正确的注射部位!就目前而言,您的组件接口是一个具有android.app.Activity唯一注入站点的接口.由于android.app.Activity在其字段上没有@Inject注释,因此您将获得无操作成员注入器.同样,您的模块被标记为未使用,因为它们实际上都没有被用作android.app.Activity的依赖项源.要解决此问题,请在组件更改中:

void inject(Activity activity);

至:

void inject(MyActivity myActivity);

问题2:

Is there any issue with having one module provide an object with a dependency on another object provided by another module belonging to the same Component?

不,这完全没问题.为了说明,我们来看一个简单的对象图:

public class Foo {

    public Foo(FooDependency fooDependency) {}
}

public class FooDependency {

    FooDependency(String name) {}
}

我们想使用Dagger将它注入以下类中:

public class FooConsumer {

    @Inject Foo foo;

    private FooConsumer() {}
}

我们想重用一个绑定FooDependency的模块,所以我们将编写两个独立的模块:

@Module
public class FooModule {

    @Provides
    Foo foo(FooDependency fooDependency) {
        return new Foo(fooDependency);
    }
}

@Module
public class FooDependencyModule {

    @Provides
    FooDependency fooDependency() {
        return new FooDependency("name");
    }
}

以及以下组件接口:

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

生成的组件DaggerFooComponent包含以下代码,它将正确使用来自单独模块FooDependencyModule的FooDependency来注入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);
  }

猜你在找的Android相关文章