Android 无法在项目上创建 Dagger

Android fail to create Dagger on project

我正在尝试在项目中使用 Dagger2,但出现此错误:

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

这个实现在我的其他项目中工作得很好,但我不知道在将它植入其他项目后我得到这个错误,我清理项目并再次重建,不幸的是 Android Studio 不知道是什么 DaggerGithubApplicationComponent 在我的申请中 class

public GitLab Address

组件:

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

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

模块:

@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();
    }
}

作用域:

@Scope
public @interface ActivitiesScope {
}

@Scope
public @interface AlachiqApplicationScope {
}

限定符:

@Qualifier
public @interface ApplicationContext {
}

申请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();
    }
}

我在 DaggerGithubApplicationComponent class 应用程序 class 中收到错误:

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

总结来自 the chat 的讨论:

2 个问题:

  1. 出现了@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.


解决方案:

  1. 在您的项目中修复apt,将生成缺少的代码:

a) Gradle 插件 2.2.3, android-apt:1.7, apt 在你的模块 build.gradle

b) Gradle 插件 2.3.1,在您的模块 build.gradle

中完全删除 android-aptannotationProcessor
  1. 修复生成的名字不正确Componentclass.