I want to integrate Dagger2, but i cant' build my project, build failed with:

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

I looking for answer about 3 hours, and i asking you for help now.

My actions step by step:

1) Add depenceses to Gradle:

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

2) Create empty test class NetworkUtils:

public class NetworksUtils {

3) Create module for it:

public class NetworksModule {
    NetworksUtils provideNetworksUtils() {
        return new NetworksUtils();

4) Create "connection" interface:

import dagger.Component;

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

5) Create App class. I don't completle understand why i did this(teaching by guide),exactly i don't understand why i need to extend by Application. Maybe to create all components when application starts. DaggerAppComponent is red, because of failed while building, this class does't created yet.

public class App extends Application {
    private static AppComponent component;

    public void onCreate() {
        component = DaggerAppComponent.create();

    public static AppComponent getComponent() {
        return component;

6) Add App to manifest:


7) Now i write my HomeActivity class...

public class HomeActivity extends AppCompatActivity implements 
HomeContract.View {
private NetwotkUtils netwotkUtils;

protected void onCreate(Bundle savedInstanceState) {

8)...and goes down when try to build project.

I think my error about App, but i can't understand where and how fix it.


This modification is not helped:

public class NetwotkUtils {
    public NetwotkUtils() {
      • 2
    • You use the Application class as a convenience. You want only one version of the component across all activities (generally, there are exceptions). It needs to be accessible to them all. The easiest way to do that is to stick them in the Application, which is created first.
      • 1
    • Also- you're using a REALLY old version of Dagger. Newer versions have tools to make things like Activity injection more convenient. The newest version is 2.21
      • 1
    • Gabe Sechan, ok, i understand about App, ty. It seems like singletons, which created first when app started. But i don't understend: how version 2.21 can be older that 2.7 i used? I watched Dagger2 Github, 2.21 there, it's rly last, so how i used 2.7?

There are two problems with your code and one is leading to error you're getting.

  1. You must include your module into building here component = DaggerAppComponent.create();
  2. Dagger is NOT injecting into private fields.

Example of p1:

   .networkModule(new NetworkModule())
  • 1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions