I want to integrate Dagger2, I can not build a project, I fall with an error:

error: com.example.animalslibrary.ui.home.activity.HomeActivity cannot be provided without an @Inject constructor or from an @Provides-annotated method. com.example.animalslibrary.ui.home.activity.HomeActivity is injected at com.example.animalslibrary.AppComponent.injectsHomeActivity(homeActivity) 

Help to understand what's the matter, head broke already.

My actions are phased:

1) Added dependencies to Gradle

 implementation 'com.google.dagger:dagger:2.7' annotationProcessor 'com.google.dagger:dagger-compiler:2.7' 

2) Created an empty test class NetworkUtils

 public class NetworksUtils { } 

3) Created for it the NetworksModule module:

 @Module public class NetworksModule { @Provides NetworksUtils provideNetworksUtils() { return new NetworksUtils(); } } 

4) Created a "binding" interface AppComponent

 import dagger.Component; @Component(modules = NetworksModule.class) public interface AppComponent { void injectsHomeActivity(HomeActivity homeActivity); } 

5) I created the App class, for the time being I do not quite understand why (I am going on the guide). Rather, I do not understand why I need to expand the Application . DaggerAppComponent is marked in red, because the project is not going to and this class is not created.

 public class App extends Application { private static AppComponent component; @Override public void onCreate() { super.onCreate(); component = DaggerAppComponent.create(); } public static AppComponent getComponent() { return component; } } 

6) Added App class to manifest:

  <application android:name="com.example.animalslibrary.ui.App" ... 

7) I HomeActivity writing to HomeActivity :

 public class HomeActivity extends AppCompatActivity implements HomeContract.View { ... @Inject private NetwotkUtils netwotkUtils; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); ... App.getComponent().injectsHomeActivity(this); ... } } 

8) Build build, fall.

I suspect the culprit is the App class, but I can’t catch it yet. In HomeActivity only inject NetworksUtils , I have nothing more to do with Dagger.

UPD: Modification:

 public class NetwotkUtils { @Inject public NetwotkUtils() { } } 

Does not help.

    2 answers 2

     component = DaggerAppComponent.builder().setNetworksModule(NetworksModule()).build() 

    Add to the App module

    Also try to remove static from the App.

     private static AppComponent component; 
    • No, it does not help. DaggerAppComponent does not compile the same, because the project is not going to. Just in case: ibb.co/g46hNsQ - KirstenLy
    • @KirstenLy, try to remove the code from HomeActivity, then run the project build. Next, try to build through the builder - Ilya Zaharov
    • Something like this should work: DaggerAppComponent.builder (). SetNetworksModule (NetworksModule ()). Build () - Ilya Zaharov
    • No, even without a code, the project is not going to be activated. - KirstenLy
    • Removing static didn't help either. Apparently I have some special occasion =) - KirstenLy

    I asked a similar question on the English-language forum, there is an answer:

    https://stackoverflow.com/questions/54955755/android-cant-integrate-dagger2-bacause-of-error-activity-cannot-be-provided-w/54955864#54955864

    Transfer:


    There are two problems in your code, one of them is the cause of your error.

    First, you must include your module in the build process of your application:

    component = DaggerAppComponent.create();

    Second, Dagger does NOT inject private fields.

    Example of the first item:

     component = DaggerAppComponent.builder() .networkModule(new NetworkModule()) .build() 

    Additionally, I changed the version of Dagger2 to 2.21