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
com.example.animalslibrary.AppComponent.injectsHomeActivity(homeActivity)

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:

@Module
public class NetworksModule {
    @Provides
    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;

    @Override
    public void onCreate() {
        super.onCreate();
        component = DaggerAppComponent.create();
    }

    public static AppComponent getComponent() {
        return component;
    }
}

6) Add App to manifest:

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

7) Now i write my HomeActivity class...

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)...and goes down when try to build project.

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

UPD:

This modification is not helped:

public class NetwotkUtils {
    @Inject
    public NetwotkUtils() {
    }
}
Answer
      • 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:

DaggerAppComponent.builder()
   .networkModule(new NetworkModule())
   .build()
  • 1
Reply Report

Warm tip !!!

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

Trending Tags

Related Questions