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.