Search code examples
androiddependency-injectiondagger-2dagger

Android fail to create Dagger on project


i'm trying to use Dagger2on project but i get this error:

Error:(42, 21) error: cannot find symbol variable DaggerGithubApplicationComponent

this implementation work fine into other my project, but i dont know whay after implament that into other project i get this error, i clean project and rebuild again, unfortunately Android Studio dont know whats DaggerGithubApplicationComponent on my Application class

public GitLab Address

Components:

@ActivitiesScope
@Component(dependencies = GithubApplicationComponent.class)
public interface ApplicationComponent {
    void inject(MainActivity activityMain);
}

@AlachiqApplicationScope
@Component(
        modules = {
                NetworkServiceModule.class,
                ActivityModule.class
        }
)
public interface GithubApplicationComponent {
    GithubService getGithubService();
}

Modules:

@Module
public class ActivityModule {

    private final Activity context;

    public ActivityModule(Activity context) {
        this.context = context;
    }

    @Provides
    @AlachiqApplicationScope
    @Named("activity_context")
    public Context context() {
        return context;
    }
}

@Module
public class ContextModule {
    private final Context context;
    public ContextModule(Context context) {
        this.context = context.getApplicationContext();
    }
    @Provides
    @AlachiqApplicationScope
    @ApplicationContext
    public Context context() {
        return context;
    }
}

@Module(includes = ContextModule.class)
public class NetworkModule {

    @Provides
    @AlachiqApplicationScope
    public HttpLoggingInterceptor loggingInterceptor() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Timber.e(message);
            }
        });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        return interceptor;
    }

    @Provides
    @AlachiqApplicationScope
    public RxJavaCallAdapterFactory rxAdapter() {
        return RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io());
    }


    @Provides
    @AlachiqApplicationScope
    public Cache cache(File cacheFile) {
        return new Cache(cacheFile, 10 * 1000 * 1000); //10MB Cahe
    }

    @Provides
    @AlachiqApplicationScope
    public File cacheFile(@ApplicationContext Context context) {
        return new File(context.getCacheDir(), "okhttp_cache");
    }

    @Provides
    @AlachiqApplicationScope
    public OkHttpClient okHttpClient(HttpLoggingInterceptor loggingInterceptor, Cache cache) {
        return new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .cache(cache)
                .build();
    }
}

@Module(includes = NetworkModule.class)
public class NetworkServiceModule {
    private String mBaseUrl;

    public NetworkServiceModule(String baseUrl) {
        mBaseUrl = baseUrl;
    }

    @Provides
    @AlachiqApplicationScope
    public GithubService githubService(Retrofit retrofit) {
        return retrofit.create(GithubService.class);
    }

    @Provides
    @AlachiqApplicationScope
    public Gson gson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(DateTime.class, new DateTimeConverter());
        return gsonBuilder.create();
    }

    @Provides
    @AlachiqApplicationScope
    public Retrofit retrofit(OkHttpClient okHttpClient, Gson gson, RxJavaCallAdapterFactory rxJavaCallAdapterFactory) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(rxJavaCallAdapterFactory)
                .client(okHttpClient)
                .baseUrl(mBaseUrl)
                .build();
    }
}

Scopes:

@Scope
public @interface ActivitiesScope {
}

@Scope
public @interface AlachiqApplicationScope {
}

Qualifier:

@Qualifier
public @interface ApplicationContext {
}

Application class:

public class APP extends MultiDexApplication {
    public static  String                     packageName;
    public static  Resources                  resources;
    private static Context                    context;
    private static GithubApplicationComponent component;
    private static APP                        instance;
    private        GithubService              githubService;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //@formatter:off
            resources   = this.getResources();
            context     = getApplicationContext();
            packageName = getPackageName();
        //@formatter:on

        Timber.plant(new Timber.DebugTree());

        component = DaggerGithubApplicationComponent.builder()
                .contextModule(new ContextModule(this))
                .networkServiceModule(new NetworkServiceModule("https://api.github.com/"))
                .build();

        githubService = component.getGithubService();
    }

    public static GithubApplicationComponent getComponent() {
        return component;
    }

    public static APP get(Activity activity) {
        return (APP) activity.getApplication();
    }
}

I get error for DaggerGithubApplicationComponent class into Application class as :

component = DaggerGithubApplicationComponent.builder()
        .contextModule(new ContextModule(this))
        .networkServiceModule(new NetworkServiceModule("https://api.github.com/"))
        .build();

Solution

  • Summarizing the discussion from the chat:

    2 Issues:

    1. There was a warning message showing up for @tux-world:

    Warning: Using incompatible plugins for the annotation processing: android-apt. This may result in an unexpected behavior. 2. Expected incorrect name of the Component class.


    Solution:

    1. Fix apt in your project and the missing code will be generated:

    a) Gradle plugin 2.2.3, android-apt:1.7, apt in your Module build.gradle

    b) Gradle plugin 2.3.1, entirely REMOVED android-apt, annotationProcessor in your Module build.gradle

    1. Fix the incorrect name of the generated Component class.