-2

Issue I am facing:

//DaggerNetComponent is not getting generated
mNetComponent = DaggerNetComponent.builder()
                .appModule(new AppModule(this)) 
                .netModule(new NetModule("https://api.github.com"))
                .build();

NET MODULE

import android.app.Application;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


@Module
public class NetModule {
    String mBaseUrl;

    public NetModule(String mBaseUrl) {
        this.mBaseUrl = mBaseUrl;
    }


    @Provides
    @Singleton
    Cache provideHttpCache(Application application) {
        int cacheSize = 10 * 1024 * 1024;
        Cache cache = new Cache(application.getCacheDir(), cacheSize);
        return cache;
    }

    @Provides
    @Singleton
    Gson provideGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
        return gsonBuilder.create();
    }

    @Provides
    @Singleton
    OkHttpClient provideOkhttpClient(Cache cache) {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.cache(cache);
        return client.build();
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();
    }
}

APP MODULE

import android.app.Application;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;


@Module
public class AppModule {

    Application mApplication;

    public AppModule(Application mApplication) {
        this.mApplication = mApplication;
    }

    @Provides
    @Singleton
    Application provideApplication() {
        return mApplication;
    }
}

NET COMPONENT

import javax.inject.Singleton;

import dagger.Component;
import retrofit.modules.AppModule;
import retrofit.modules.NetModule;

@Singleton
@Component(modules = {AppModule.class, NetModule.class})
public interface NetComponent {
    void inject(MainActivity activity);
}

How to Debug this so I am able to generate this class

Devrath
  • 42,072
  • 54
  • 195
  • 297

2 Answers2

0

I think this is because of Cache provideHttpCache(Application application) in NetModule.

Yes, you provides Application within AppModule, but it does not have any association with NetComponent.

So, there are three ways:

1. Just pass Application manually into NetComponent the same way you did it with AppComponent (via constructor)

or (better way)

2. Create separate Component for AppModule - AppComponent which should provide Application to other components:

@Component(modules = {
   AppModule.class,
})
@Singleton
public interface AppComponent {
   Application application();
}

And then make your NetComponent to be dependent from AppComponent:

@Component(
   dependencies = AppComponent.class,
   modules = NetModule.class
)
@NetScope
public interface NetComponent {
   void inject(AddTranslationActivity activity);
}

or

3. Do the same as described in item 2, but using Dagger subcomponents

Alexander Bilchuk
  • 1,790
  • 1
  • 12
  • 17
0

If you aren't getting build errors and the Dagger component class does not exist, it sounds like the Dagger annotation processor is not running.

You'll need to add the annotation processors to your gradle dependencies. For example:

// Application build.gradle
dependencies {
    compile 'com.google.dagger:dagger:2.11'
    annotationProcessor "com.google.dagger:dagger-compiler:2.11"
}

If that still doesn't work, try invalidating caches:

File>>Invalidate Caches\Restart>>Invalidate and Restart
bcorso
  • 45,608
  • 10
  • 63
  • 75