2016-12-03 2 views
5

My Dagger2コンポーネントクラスには、Androidアクティビティクラスにフィールド依存性を注入するために使用する3つのモジュールが含まれています。生成されたコンポーネントファイルには、すべてのモジュールが未使用である旨のコメントがあり、これにはpageをリンクしています。Dagger2 - 生成コンポーネントクラスの "未使用"モジュール

アクティビティクラスはコンポーネントの注入(アクティビティ)メソッドを呼び出しており、モジュールによって提供される注入用の注釈付きフィールドがあるため、生成されたコンポーネントファイルにこの注入を行うプロバイダがない理由はわかりません。

私のコードは以下のとおりです。ありがとうございます!

生成したコンポーネントクラス:

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

モジュールクラス: 1つのモジュールはでオブジェクトを提供したとのすべての問題はあります同じコンポーネントに属する別のモジュールによって提供される別のオブジェクトへの依存性?

@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); 
+0

あなたのダガーバージョンは廃止[ダガー2.2コンポーネントビルダーモジュール方式の – EpicPandaForce

+0

可能な複製である何](http://stackoverflow.com/questions/36521302/dagger-2-2-component-builder-module-method-deprecated) –

答えて

3

質問1:はなぜ私のモジュールは、 "未使用" としてマークされていますか?

あなたは正しい注射部位を提供していません!それが現れているように、あなたのコンポーネントインターフェースは、唯一の注射部位がandroid.app.Activityであるものです。 android.app.Activityのフィールドには@Injectの注釈が含まれていないので、no-opのメンバーインジェクタを取得します。同様に、あなたのモジュールは実際にはandroid.app.Activityの依存関係のソースとして使用されていないので、未使用とマークされています。

void inject(Activity activity); 

へ:

void inject(MyActivity myActivity); 

質問2:あなたのコンポーネントの変更では、この、解決するには

は、1つのモジュールが依存関係を持つオブジェクトを提供したとのすべての問題はあります同じコンポーネントに属する別のモジュールによって提供される別のオブジェクト上にあるか?

いいえ、これは完全に問題ありません。説明するために、単純なオブジェクトのグラフを見てみましょう:

public class Foo { 

    public Foo(FooDependency fooDependency) {} 
} 

public class FooDependency { 

    FooDependency(String name) {} 
} 

我々はダガーを使用して、以下のクラスの内部でそれを注入したい:

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が正しくFooを注入するために別のモジュールFooDependencyModuleからFooDependencyを使用する次のコードが含まれています

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

両方の質問にお答えいただきありがとうございます。 inject()パラメータのサブクラスに対して行われていないインジェクションは非常に微妙です – win4fun

関連する問題