Android无法在项目

时间:2017-04-18 06:41:27

标签: android dependency-injection dagger-2 dagger

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

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

这个实现很好地适用于我的其他项目,但我不知道在进入其他项目之后,我得到了这个错误,我清理项目并重新重建,不幸的是Android Studio不知道我的Application类上有什么DaggerGithubApplicationComponent

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 {
}

申请类

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类的错误导入Application类:

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

1 个答案:

答案 0 :(得分:1)

总结the chat的讨论:

2个问题:

  1. @ tux-world出现了一条警告消息:
      

    警告:使用不兼容的插件进行注释处理:android-apt。这可能会导致意外行为。

  2. Component类的预期名称不正确。
  3. 解决方案:

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

      a)您的Module build.gradle中的Gradle插件2.2.3android-apt:1.7apt

      b)Gradle插件2.3.1,在模块android-apt中完全删除annotationProcessorbuild.gradle

      1. 修复生成的Component类的错误名称。