我试图使用项目Dagger2
创建匕首,但我得到这个错误:的Android无法在项目
Error:(42, 21) error: cannot find symbol variable DaggerGithubApplicationComponent
本实施做工精细到其他我的项目,但我不知道whay implament后成其他项目中,我得到这个错误,我清洁工程和重建再次,不幸的是Android的工作室不知道在我的应用程序类
组件什么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();
}
}
我得到错误DaggerGithubApplicationComponent
类为应用类为:
component = DaggerGithubApplicationComponent.builder()
.contextModule(new ContextModule(this))
.networkServiceModule(new NetworkServiceModule("https://api.github.com/"))
.build();
只需清理并构建并导入** ir.pishguy.testdagger.Dagger.Components.DaggerGithubApplicationComponent; **。你需要用匕首为你生成代码。刚跑过应用程序检查是否有错误。工作正常 – Raghunandan
@Raghunandan我没有得到项目上的任何其他错误,'导入ir.pishguy.testdagger.Dagger.Components.DaggerGithubApplicat ionComponent;'不解决我的问题和'DaggerGithubApplicat ionComponent'在我的项目中是未知的,你能看到git存储库?在此先感谢 –
我克隆你的回购,只是干净和建设,我没有看到任何问题。实际上是在我的手机上运行了应用程序。它的罚款 – Raghunandan