Android无法在项目上创建Dagger

Android无法在项目上创建Dagger,android,dependency-injection,dagger-2,dagger,Android,Dependency Injection,Dagger 2,Dagger,我试图在项目中使用Dagger2,但出现以下错误: Error:(42, 21) error: cannot find symbol variable DaggerGithubApplicationComponent 这个实现在我的其他项目中可以很好地工作,但我不知道在其他项目中我得到了这个错误,我清理了项目并再次重建,不幸的是Android Studio不知道我的应用程序类上的DaggerGithubApplicationComponent是什么 组件: @ActivitiesScope

我试图在项目中使用
Dagger2
,但出现以下错误:

Error:(42, 21) error: cannot find symbol variable DaggerGithubApplicationComponent
这个实现在我的其他项目中可以很好地工作,但我不知道在其他项目中我得到了这个错误,我清理了项目并再次重建,不幸的是Android Studio不知道我的应用程序类上的DaggerGithubApplicationComponent是什么

组件

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

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

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

@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();
    }
}
应用程序类

@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();
    }
}
我将
DaggergiSubapplicationComponent
类的错误输入到应用程序类中,如下所示:

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

总结以下方面的讨论:

2个问题:

  • @tux world出现了一条警告消息:
  • 警告:使用不兼容插件进行注释处理:android-apt。这可能会导致意外行为。 2.
    组件
    类的名称应不正确


    解决方案:

  • 修复项目中的
    apt
    ,将生成缺少的代码:
  • a) Gradle插件
    2.2.3
    android apt:1.7
    apt
    在您的模块构建中。Gradle

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

  • 修复生成的
    组件
    类的错误名称

  • 只需清理、构建并导入ir.pishguy.testdagger.Dagger.Components.DaggerGithubApplicationComponent;。您需要dagger为您生成代码。只是运行应用程序来检查是否有任何错误。作品fine@Raghunandan我没有在项目中得到任何其他错误,
    import ir.pishguy.testdagger.Dagger.Components.DaggerGithubApplicat‌​离子组分不解决我的问题和
    Daggergithubapplicate‌​ionComponent
    在我的项目中是未知的,您能看到git存储库吗?提前谢谢,我克隆了你的回购协议,只是做了清理和构建,我看不出有任何问题。事实上,我在手机上运行了这个应用程序。它的fine@tux-你有没有试过拉古南丹说的话?让我换一种说法:1。临时注释所有
    DaggerGithubApplicationComment
    内容。2.暂时删除
    DaggerGithubApplicationComment
    import。3.构建一个干净的项目。4.反向执行步骤1。和2.@Bartklipinski我想知道会发生什么!!!我清理、重建项目,然后构建项目,现在我希望创建
    daggergithubapplicate‌​‌​ionComponent
    并且可以导入,我没有任何关于该组件的
    导入
    ,先生