草庐IT

android - Dagger2 - 生成的组件类中的 "Unused"模块

coder 2023-11-25 原文

我的 Dagger2 组件类包含 3 个模块,我试图使用它们将字段依赖项注入(inject) Android Activity 类。生成的组件文件有注释说所有模块都未使用,链接此 page获取更多信息。

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

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

生成的组件类:

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

Activity 类:

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:为什么我的模块被标记为“未使用”?

您没有提供正确的注入(inject)部位!就目前而言,您的组件界面是一个具有 android.app.Activity 唯一注入(inject)站点的界面。由于 android.app.Activity 在其字段上没有 @Inject 注释,因此您将获得一个无操作成员注入(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 将它注入(inject)到以下类中:

public class FooConsumer {

    @Inject Foo foo;

    private FooConsumer() {}
}

我们想重用一个模块绑定(bind)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");
    }
}

以及以下组件接口(interface):

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

生成的组件 DaggerFooComponent 包含以下代码,这些代码将正确使用来自单独模块 FooDependencyModuleFooDependency 来注入(inject) 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 - Dagger2 - 生成的组件类中的 "Unused"模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40944451/

有关android - Dagger2 - 生成的组件类中的 "Unused"模块的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  3. ruby - 在 Ruby 中使用匿名模块 - 2

    假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于

  4. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  5. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  6. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  7. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  8. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  9. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  10. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

随机推荐