My field for retrofit in this class is never injected into, it is still null when i run my code.
Here is my ServiceClass where I inject retrofit, have my api calls etc. I stripped it down for simplicity:
public class ServiceClass{
#Inject
Retrofit retrofit;
public ServiceClass(){
}
}
My module class for all network related dependencies:
#Module
public class NetworkModule {
#Provides
#ApplicationScope
Retrofit getRetrofit(OkHttpClient okHttpClient, Gson gson){
return new Retrofit.Builder()
.baseUrl(URL.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
#Provides
#ApplicationScope
OkHttpClient getOkHttpClient(Gson gson, HttpLoggingInterceptor httpLoggingInterceptor){
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.newBuilder().addInterceptor(httpLoggingInterceptor);
return okHttpClient;
}
#Provides
#ApplicationScope
HttpLoggingInterceptor getHttpLoggingInterceptor(){
return new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC);
}
#Provides
#ApplicationScope
Gson getGson(){
return new Gson();
}
}
My AppComponent this is my only component class:
#ApplicationScope
#Component(modules = {NetworkModule.class})
public interface AppComponent {
#Component.Builder
interface Builder {
#BindsInstance
Builder application(MyApplication myApplication);
AppComponent build();
}
void inject(MyApplication myApplication);
Retrofit getRetrofit();
}
My Application class:
public class MyApplication extends Application{
private AppComponent appComponent;
#Override
public void onCreate() {
super.onCreate();
DaggerAppComponent
.builder()
.application(this)
.build()
.inject(this);
}
public AppComponent getAppComponent(){
return appComponent;
}
}
I tried to fiddle around the code, I don't seem to manage to get it working properly. What am I missing here?
Update (previous information still valid) :
I have noticed you incorrectly build your component: you must add .networkModule(new NetworkModule()) after DaggerAppComponent.builder()
Make sure your private AppComponent appComponent is initialized too!
For field injection (I believe that's what you're after), you can write your constructor like this:
public ServiceClass(){
MyApplication.getInstance().getAppComponent().inject(this)
}
Naturally, you should expose your appComponent entity somehow - the above is my guess (to expose appComponent entity via application entity).
PS.: better approach (and more readable too) is to avoid field injection at all and parametrize constructor (however it's not always possible, like for example if you inject into activity).
PSS.: your AppComponent should also have void inject(ServiceClass value);
There are multiple ways of injecting retrofit in ServiceClass
You have to make a separate Component for ServiceClass like :-
#Component(dependencies = AppComponent.class)
interface ServiceClassComponent {
void injectServiceClass(ServiceClass serviceClass);
}
Or
you can just inject ServiceClass into your application component:-
void injectServiceClass(ServiceClass serviceClass);
into your AppComponent
The dependencies keyword would include all the dependent components into your particular component that you would build.
Then in the constructor of ServiceClass you need to build the Component and inject it
Related
I am getting an exception when building from the dagger2 2.16 version to the 2.23.2 version.
It is up and running in 2.16. I didn't modify any code. After upgrading to 2.23.2, it failed to build.
I am not sure what the problem is, so I ask everyone for assistance.
Thank you.
Module
#Module
public class BaseModule {
private ConfigBuilder configBuilder;
public BaseModule(#Nullable ConfigBuilder configBuilder) {
this.configBuilder = configBuilder;
}
#Singleton
#Provides
public Gson provideGson() {
GsonBuilder builder = new GsonBuilder();
if (configBuilder != null) {
configBuilder.buildGson(builder);
}
return builder.create();
}
#Singleton
#Provides
public OkHttpClient provideOkHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (configBuilder != null) {
configBuilder.buildOkHttp(builder);
}
builder.addInterceptor(new EncryptInterceptor());
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(loggingInterceptor);
}
return builder.build();
}
#Singleton
#Provides
public Retrofit provideRetrofit(OkHttpClient okHttpClient, Gson gson) {
Retrofit.Builder builder = new Retrofit.Builder()
.client(okHttpClient);
if (configBuilder != null) {
configBuilder.buildRetrofit(builder);
}
builder.addConverterFactory(GsonWrapperConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
return builder.build();
}
#Singleton
#Provides
public SharedPreferencesPlus provideSharedPreferencesPlus(Application application, Gson gson) {
return SharedPreferencesPlus.createDefault(application, gson);
}
#Module
interface ActivityModule {
#ActivityScope
#ContributesAndroidInjector
MainActivity main();
#Module
public interface ViewModelModule {
#Binds
#IntoMap
#ViewModelKey(VMMain.class)
ViewModel main(VMMain vm);
Component
#Singleton
#Component(modules = {BaseModule.class, AndroidSupportInjectionModule.class})
public interface BaseComponent {
Application provideApplication();
SharedPreferencesPlus provideSharedPreferencesPlus();
Gson provideGson();
OkHttpClient provideOkHttpClient();
Retrofit provideRetrofit();
#Component.Builder
interface Builder {
#BindsInstance
Builder application(Application application);
Builder AppModule(BaseModule baseModule);
BaseComponent build();
}
#ApplicationScope
#Component(modules = {
ActivityModule.class,
ViewModelModule.class,
DataModule.class}, dependencies = BaseComponent.class)
public interface AppComponent
{
void inject(AppContext application);
}
exception:
[Dagger/MissingBinding] java.util.Map>> cannot be provided without an #Provides-annotated method.
java.util.Map>> is injected at dagger.android.DispatchingAndroidInjector(…, injectorFactoriesWithStringKeys)
dagger.android.DispatchingAndroidInjector is injected at
org.pp.va.video.app.AppContext.serviceInjector
org.pp.va.video.app.AppContext is injected at
org.pp.va.video.di.AppComponent.inject(org.pp.va.video.app.AppContext)
It is also requested at:
dagger.android.DispatchingAndroidInjector(…, injectorFactoriesWithStringKeys)
I found that starting from 2.16 to 2.17 began to have problems.
I observed the error because of the introduction of AndroidSupportInjectionModule in AppComponent. I used to introduce the AndroidSupportInjectionModule in BaseComponent, and then the BaseComponent that AppComponent dependency on. Now it won't work. I removed the introduction of AndroidSupportInjectionModule from BaseComponent and introduced AndroidSupportInjectionModule in AppComponent, which solved my problem.
My current code is as follows:
#ApplicationScope
#Component(modules = {
ActivityModule.class,
ViewModelModule.class,
DataModule.class, AndroidSupportInjectionModule.class}, dependencies = BaseComponent.class)
public interface AppComponent
#Singleton
#Component(modules = {BaseModule.class})
public interface BaseComponent {
I am using Dagger2 in my app to provide dependencies. I get this following error when I build my app.
e: /Users/sriramr/Desktop/android/Movie/MovieInfo/app/build/generated/source/kapt/debug/in/sriram/movieinfo/di/ActivityBuilder_BindMoviesListActivity.java:22: error: in.sriram.movieinfo.di.ActivityBuilder_BindMoviesListActivity.MoviesListActivitySubcomponent (unscoped) may not reference scoped bindings:
#Subcomponent(modules = MoviesListActivityModule.class)
^
#Provides #Singleton in.sriram.movieinfo.network.TmdbService in.sriram.movieinfo.di.MoviesListActivityModule.getTmdbService(retrofit2.Retrofit)
#Provides #Singleton retrofit2.Retrofit in.sriram.movieinfo.di.NetworkModule.getRetrofit(okhttp3.OkHttpClient, retrofit2.converter.gson.GsonConverterFactory, retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory)
#Provides #Singleton okhttp3.OkHttpClient in.sriram.movieinfo.di.NetworkModule.getOkHttpClient(okhttp3.logging.HttpLoggingInterceptor, okhttp3.Cache)
#Provides #Singleton okhttp3.logging.HttpLoggingInterceptor in.sriram.movieinfo.di.NetworkModule.getHttpLoggingInterceptor()
#Provides #Singleton okhttp3.Cache in.sriram.movieinfo.di.NetworkModule.getCacheFile(#Named("application-context") android.content.Context)
#Provides #Singleton retrofit2.converter.gson.GsonConverterFactory in.sriram.movieinfo.di.NetworkModule.getGsonConverterFactory()
#Provides #Singleton retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory in.sriram.movieinfo.di.NetworkModule.getRxJavaFactory()
#Provides #Singleton in.sriram.movieinfo.cache.AppDatabase in.sriram.movieinfo.di.ContextModule.getAppDatabase(#Named("application-context") android.content.Context)
#Provides #Singleton com.squareup.picasso.Picasso in.sriram.movieinfo.di.MoviesListActivityModule.getPicasso(#Named("application-context") android.content.Context)
This is my ContextModule
#Module
public class ContextModule {
#Provides
#Named("application-context")
Context getContext(Application app) {
return app;
}
#Provides
#Singleton
AppDatabase getAppDatabase(#Named("application-context") Context context) {
return Room.databaseBuilder(context,
AppDatabase.class, "database-name").build();
}
}
And this is my NetworkModule
#Module(includes = ContextModule.class)
public class NetworkModule {
#Provides
#Singleton
Cache getCacheFile(#Named("application-context") Context context) {
File cacheFile = new File(context.getCacheDir(), "moviedb-cache");
return new Cache(cacheFile, 10 * 1000 * 1000);
}
#Provides
#Singleton
OkHttp3Downloader getOkHttp3Downloader(OkHttpClient okHttpClient) {
return new OkHttp3Downloader(okHttpClient);
}
#Provides
#Singleton
OkHttpClient getOkHttpClient(HttpLoggingInterceptor loggingInterceptor, Cache cache) {
return new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.cache(cache)
.build();
}
#Provides
#Singleton
Retrofit getRetrofit(OkHttpClient client, GsonConverterFactory gsonConverterFactory, RxJava2CallAdapterFactory callAdapter) {
return new Retrofit.Builder()
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(callAdapter)
.baseUrl("https://api.themoviedb.org/3/")
.client(client)
.build();
}
#Provides
#Singleton
HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(message -> Timber.tag("OkHttp").d(message));
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return loggingInterceptor;
}
#Provides
#Singleton
GsonConverterFactory getGsonConverterFactory() {
return GsonConverterFactory.create();
}
#Provides
#Singleton
RxJava2CallAdapterFactory getRxJavaFactory() {
return RxJava2CallAdapterFactory.create();
}
}
And finally the MovieListActivityModule
#Module(includes = NetworkModule.class)
public class MoviesListActivityModule {
#Provides
#Singleton
TmdbService getTmdbService(Retrofit retrofit) {
return retrofit.create(TmdbService.class);
}
#Provides
#Singleton
Picasso getPicasso(#Named("application-context") Context context) {
return new Picasso.Builder(context)
.loggingEnabled(true)
.build();
}
}
And this is the ActivityBuilder class
#Module
public abstract class ActivityBuilder {
#ContributesAndroidInjector(modules = MoviesListActivityModule.class)
abstract MoviesListActivity bindMoviesListActivity();
}
And finally the AppComponent
#Singleton
#Component(modules = {AndroidInjectionModule.class, ContextModule.class, ActivityBuilder.class})
public interface AppComponent {
#Component.Builder
interface Builder {
#BindsInstance
Builder application(Application application);
AppComponent build();
}
void inject(MovieInfoApp app);
}
I am new to Dagger 2 and I followed this from a random tutorial of Medium.
I don't see any SubComponent here. I understand that the subcomponent is generated.
This error just occurs for the #Singleton scoped dependencies.
I followed some stack overflow links and added #Singleton to the AppComponent interface.
How do I fix this?
Rename MoviesListActivityModule to MoviesListApplicationModule, take it off of your #ContributesAndroidInjector, and put it onto your AppComponent's modules list instead.
Behind the scenes, #ContributesAndroidInjector generates a #Subcomponent specific to your MoviesListActivity. When you try to inject your MoviesListActivity using AndroidInjection.inject(Activity), Dagger creates a subcomponent instance that holds MoviesListActivity's MembersInjector (the generated code that knows how to populate all the #Inject fields in your MoviesListActivity) and any scoped bindings that your Activity (or its dependencies) may need. That's the component you're missing, and it's named after the Module and #ContributesAndroidInjector method name, ActivityBuilder_BindMoviesListActivity.MoviesListActivitySubcomponent.
You can add scopes (like an #ActivityScope you create) to #ContributesAndroidInjector, and dagger.android will add them onto the generated subcomponent, just like setting modules = {/*...*/} will add modules onto it. However, that isn't your problem.
First of all, you were right to add #Singleton to your AppComponent; that's a very common thing, and it's right to do here. Adding #Singleton there means that Dagger will automatically watch out for bindings that are marked with #Singleton and keep copies of them in the component you annotate the same way. If you were to create an #ActivityScope annotation and add it to your subcomponent, Dagger would watch out for bindings that were annotated with #ActivityScope and return the same instance from within the same Activity instance but a different instance compared to other instances of the same Activity or other Activity types.
Your problem is that you are adding #Singleton-scoped bindings Picasso and TmdbService into your unscoped subcomponent graph. What you're telling Dagger is that across the lifetime of your application component (not your Activity, your application) you should always return the same Picasso and TmdbService instances. However, by making that binding on the module list of your subcomponent rather than your top-level #Singleton #Component, you tell Dagger about these application-level objects when it's trying to configure activity-level bindings. The same applies to the bindings in your NetworkModule, which are also listed in your error message.
After you move the modules, you will always receive the same instance of Picasso, TmdbService, OkHttpClient, and all of those others—which should allow those objects to help manage caches, batches, and requests in flight without you having to worry about which instance you're interacting with. It'll always be the same instance across the lifetime of your app.
I'm following the new Dagger2 support for android to implement a movies list sample application and below is my use case.
Activity Holds a fragment used to load list of movies
Fragment uses a presenter to hit an api using retrofit
Presenter has a dependency to the API interface class which contains Observale for the movies
I'm using #Inject inside the presenter for the ApiService interface but i got an error that i cannot use #Inject field without declaring provide annotation and below is my code
Main App component
My Movies module
My Movies Contract
My Presenter
and finally the api service interface
So how can i provide the service interface to MoviesModule in order to work properly inside the presenter
The error is
Error:(22, 8) error: [dagger.android.AndroidInjector.inject(T)] sampler.dagger.com.movieslist.data.MoviesApiService cannot be provided without an #Provides-annotated method.
sampler.dagger.com.movieslist.data.MoviesApiService is injected at
sampler.dagger.com.movieslist.movies.MoviePresenter.mApiService
sampler.dagger.com.movieslist.movies.MoviePresenter is injected at
sampler.dagger.com.movieslist.movies.MoviesModule.moviesPresenter(presenter)
sampler.dagger.com.movieslist.movies.MoviesContract.Presenter is injected at
sampler.dagger.com.movieslist.movies.MoviesFragment.mPresenter
dagger.Lazy<sampler.dagger.com.movieslist.movies.MoviesFragment> is injected at
sampler.dagger.com.movieslist.movies.MainActivity.mMoviesFragmentsProvider
sampler.dagger.com.movieslist.movies.MainActivity is injected at
dagger.android.AndroidInjector.inject(arg0)
One solution could be:
#Module
public class APIModule {
#Provides
#Singleton
Retrofit provideRetrofit(Gson gson) {
OkHttpClient client = new OkHttpClient.Builder().build();
return new Retrofit.Builder()
.baseUrl("https://stackoverflow.com/")
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
}
#Provides
#Singleton
Gson provideGson() {
return new GsonBuilder().create();
}
#Provides
#Singleton
MoviesApiService provideMoviesApiService(Retrofit retrofit) {
return retrofit.create(MoviesApiService.class);
}
}
In your MoviePresenter its better to use constructor injection than field injection:
private MoviesApiService mApiService;
#Inject
public MoviePresenter(MoviesApiService apiService) {
mApiService = apiService;
}
MoviApiService is an interface, you cannot inject an interface. You need to create a provides method to provide the retrofit service.
MoviApiService providesMoviApiService(Retrofit retrofit) {
retrofit.create(MoviApiService.class);
}
I am learning Dagger 2 now and it's such a pain for me to explain the question without codes , so let me list all my modules, components and etc first :
App.class
public class App extends Application {
private ApiComponent mApiComponent = null;
private AppComponent mAppComponent = null;
public ApiComponent getApiComponent() {
if (mApiComponent == null) {
// Dagger%COMPONENT_NAME%
mApiComponent = DaggerApiComponent.builder()
// list of modules that are part of this component need to be created here too
.appModule(new AppModule(this)) // This also corresponds to the name of your module: %component_name%Module
.apiModule(new ApiModule(this))
.build();
}
return mApiComponent;
}
public AppComponent getAppComponent() {
if (mAppComponent == null) {
// If a Dagger 2 component does not have any constructor arguments for any of its modules,
// then we can use .create() as a shortcut instead:
mAppComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
return mAppComponent;
}
}
AppComponent
#Singleton
#Component(modules = AppModule.class)
public interface AppComponent {
void inject(RetrofitDemo target);
}
AppModule
private final Application mContext;
AppModule(Application context) {
mContext = context;
}
#Singleton
#ForApplication
#Provides
Application provideApplication() {
return mContext;
}
#Singleton
#ForApplication
#Provides
Context provideContext() {
return mContext;
}
ApiComponent
#Singleton
#Component(dependencies = {AppModule.class},modules = {ApiModule.class})
public interface ApiComponent {
void inject(RetrofitDemo target);
}
APIModule
#Inject
Context application;
#Inject
public ApiModule(Context context){
this.application = context;
}
#Provides
#Singleton
Gson provideGson() {
return new GsonBuilder()
// All timestamps are returned in ISO 8601 format:
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
// Blank fields are included as null instead of being omitted.
.serializeNulls()
.create();
}
#Provides
#Singleton
OkHttpClient provideOkHttpClient() {
...
}
#Provides
#Singleton
public Retrofit provideRetrofit(Gson gson,OkHttpClient okHttpClient){
return new Retrofit.Builder()
.baseUrl(DribbleApi.END_POINT)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(okHttpClient)
.build();
}
And my activity will be like this:
#Inject
Retrofit mRetrofit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrofit_demo);
((App) getApplication()).getApiComponent().inject(this);
...
Here is the error message:
Error:(18, 10) : retrofit2.Retrofit cannot be provided without an #Inject constructor or from an #Provides- or #Produces-annotated method.
retrofit2.Retrofit is injected at com.sinyuk.yuk.RetrofitDemo.mRetrofit
com.sinyuk.yuk.RetrofitDemo is injected at com.sinyuk.yuk.AppComponent.inject(target)
What makes me confused is the retrofit instance is provided by the ApiModule, but why the error massage said it's injected at appComponent?And I can't find any place wrong in my code. T_T,it's too heavy going to learn Dagger for me...I think.
Besides, in my case I wrote dependencies = AppModule.class module = ApiModule.class in the AppComponent , and it seems to be right I think,but if I wrote module = ({AppComponent.class,ApiComponent.class}),it also works fine.Anybody can explain me why?
Kindly review my code and give me some advice. Thx in advance!
#Sinyuk There's a lot to unpack here, and Dagger is a little complicated at first blush, but I think I can help. First, you have a conceptual misunderstanding regarding the #Component annotation. A Component is an interface which you define, and which Dagger implements through code generation. You will define the interface, and annotate it with #Component and then you will provide a set of Modules to Dagger to use in the generation process. The modules which you use are passed in through the modules element of the #Component annotation. If you want to have one Component permit another Component to support the injection process, then any Component interfaces which you need to have Dagger use while injecting your code, will be passed in through the dependencies element of the #Component annotation.
--
As a result, The following is incorrect
#Component(dependencies = AppModule.class module = ApiModule.class`)
instead, to have the one Component use two Modules write:
#Component(modules = {ApiModule.class, AppModule.class})
or, to have one Component use one Module and depend upon the other Component
#Component(modules = {AppModule.class}, dependencies = {ApiComponent.class})
I hope that that helps you get onto the right path. Let me know if you have any follow up questions.
Okay so your configuration should be this
public class App extends Application {
private AppComponent mAppComponent = null;
public AppComponent getAppComponent() {
if (mAppComponent == null) {
// If a Dagger 2 component does not have any constructor arguments for any of its modules,
// then we can use .create() as a shortcut instead:
mAppComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
return mAppComponent;
}
}
And
#Singleton
#Component(modules = {AppModule.class, ApiModule.class})
public interface AppComponent {
void inject(RetrofitDemo target);
}
And
#Module
public class AppModule {
private final Application mContext;
AppModule(Application context) {
mContext = context;
}
#Provides
Application provideApplication() {
return mContext;
}
#Provides
Context provideContext() {
return mContext;
}
}
And
#Module
public class ApiModule {
#Provides
#Singleton
Gson provideGson() {
return new GsonBuilder()
// All timestamps are returned in ISO 8601 format:
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
// Blank fields are included as null instead of being omitted.
.serializeNulls()
.create();
}
#Provides
#Singleton
OkHttpClient provideOkHttpClient() {
...
}
#Provides
#Singleton
public Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient){
return new Retrofit.Builder()
.baseUrl(DribbleApi.END_POINT)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(okHttpClient)
.build();
}
}
And
//...Activity
#Inject
Retrofit mRetrofit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrofit_demo);
((App) getApplication()).getAppComponent().inject(this);
...
I need to expose my OkHttpClient from ApplicationModule so I added to ApplicationComponent. Something like this :
#Module
public class ApplicationModule {
#Provides #Singleton
public OkHttpClient provideOkHttpClient() {
final OkHttpClient.Builder client = new OkHttpClient.Builder();
return client.build();
}
#Singleton
#Component( modules = {ApplicationModule.class} )
public interface ApplicationComponent {
OkHttpClient okHttpClient();
}
so I added the OkHttpClient okHttpClient(); in the ApplicationComponent as you can see right above.
Now in my NetworkModule I use it like :
#Module
public class NetworkModule {
#Provides #ActivityScope
public ProjectService provideProjectService(OkHttpClient client) {
return new ProjectService(client);
}
#Component( dependencies = {ApplicationComponent.class}, modules = {NetworkModule.class} )
#ActivityScope
public interface NetworkComponent {
void inject(#NonNull MyActivity myActivity);
}
but now when I get a runtime error :
Caused by: java.lang.IllegalStateException: css.test.demo.ApplicationComponent must be set
at css.test.demo.main.projects.network.DaggerNetworkComponent$Builder.build(DaggerNetworkComponent.java:102)
at css.test.demo.main.projects.MyActivity.onCreate(MyActivity.java:159)
at android.app.Activity.performCreate(Activity.java:6237)
and here is how I build it in MyActivity :
NetworkComponent = DaggerNetworkComponent.builder()
.NetworkModule(new NetworkModule(this))
.build();
NetworkComponent.inject(this);
I like to emphasize that dagger contains no magic—its is just plain java. If you don't give it the information it needs, the compiler will complain.
If you have a look at your DaggerNetworkComponent.Builder you will notice that it has a method called appComponent(AppComponent component). This is where dagger expects you to add the appcomponent that your NetworkComponent depends on.
NetworkComponent = DaggerNetworkComponent.builder()
.NetworkModule(new NetworkModule(this))
.appComponent(((App)getApplication()).getAppComponent()) // add your appComponent
.build();
And it should work.