未生成Dagger 2组件 [英] Dagger 2 component not generated
问题描述
在我的模块中,在我的基本Application类中
In my module, in my base Application class
component = DaggerCompClassComponent.builder()
.classModule(new ModuleClass()).build();
找不到DaggerCompClassComponent。
it can not find DaggerCompClassComponent.
我在build.gradle模块上
I have on module build.gradle
apply plugin: 'com.neenbedankt.android-apt'
.........................
apt 'com.google.dagger:dagger-compiler:2.8'
compile 'com.google.dagger:dagger:2.8'
provided 'javax.annotation:jsr250-api:1.0'
并在Project build.gradle中,
and in Project build.gradle,
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
我已经完成了构建/重建/清理/重启项目。我有一个注入对象的Component类和一个提供注入对象的ModuleClass。
I have done build / rebuild / clean / restart project. I have a Component class where I inject objects and a ModuleClass where I provide objects to inject.
不生成Dagger Component的原因可能是什么? class?
What can be the cause for not generating Dagger Component . class ?
编辑:
这是我的ModuleClass,用@Module标注:
This is my ModuleClass, adnotated with @Module:
@Provides
@Singleton
public Interceptor provideInterceptor() {
return new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
builder.addHeader("AppName-Android", BuildConfig.VERSION_NAME + "-" + BuildConfig.VERSION_CODE)
.addHeader("Content-Type", "application/json");
return chain.proceed(builder.build());
}
};
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient(Interceptor interceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.interceptors().add(interceptor);
return builder.build();
}
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient client) {
return new Retrofit.Builder()
.baseUrl(BaseApplication.getRes().getString(R.string.api_base_url))
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
@Provides
@Singleton
WebServiceCall provideWebService(Retrofit retrofit) {
return retrofit.create(WebServiceCall.class);
}
这是我的组件类:
@Component(modules = ModuleClass.class)
@Singleton
public interface ComponentClass {
void inject(Interceptor o);
void inject(OkHttpClient o);
void inject(Retrofit o);
void inject(WebServiceCall o);
}
推荐答案
开发时在Kotlin上,您应该在对应的 annotationProcessor
旁边添加以下行:
When developing on Kotlin, you should add the following lines next to their annotationProcessor
counterparts:
kapt 'com.google.dagger:dagger-android-processor:2.15'
kapt 'com.google.dagger:dagger-compiler:2.15'
并在同一文件的开头添加 apply插件:'kotlin-kapt'
。
and add apply plugin: 'kotlin-kapt'
at the start of the same file.
该部分对我来说是这样的:
That section looks like this for me:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt' // <- Add this line
apply plugin: 'io.fabric'
这篇关于未生成Dagger 2组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!