在我的模块中,在我的BaseApp类中
class BaseApp : Application() {
companion object {
lateinit var appComponent: AppComponent
val baseUrl = "https://api.github.com/"
}
override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.builder()
.networkModule(NetworkModule(baseUrl))
.apiModule(ApiModule())
.appModule(AppModule(this))
.localModule(LocalModule(this))
.build()
}
}
找不到DaggerAppComponent。
未生成Dagger Component的原因可能是什么?类?
这些是我的模块类
这是ApiModule。
@Module
class ApiModule{
@Provides
@Singleton
fun provideApi(@Named("retrofit") retrofit: Retrofit) : API = retrofit.create(API::class.java)
@Provides
@Singleton
fun provideApiClient(Api: API) = ApiClient(Api)
}
这是AppModule
@Module
class AppModule(val application: Application) {
@Provides
@Singleton
fun provideApp() = application
}
这是LocalModule
@Module
class LocalModule(val application: Application) {
private val authDatabase: AuthDatabase
init {
authDatabase = Room.databaseBuilder(application, AuthDatabase::class.java, "auth.db").build()
}
@Provides
@Singleton
fun provideLocalStorage() = SharedPrefStorage(application)
@Singleton
@Provides
fun provideRoomDatabase(): AuthDatabase = authDatabase
@Singleton
@Provides
fun provideAuthDao(authDatabase: AuthDatabase): AuthDao = authDatabase.getAuthDao()
}
这是NetworkModule
@Module
class NetworkModule(val mBaseUrl: String) {
@Provides
@Singleton
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor = HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY }
@Provides
@Singleton
fun provideSecretHeaderInterceptor(local: LocalStorage, authDatabase: AuthDatabase): SecretHeaderInterceptor = SecretHeaderInterceptor(local, authDatabase)
@Provides
@Singleton
fun provideOkHttpClient(loggingInterceptor: HttpLoggingInterceptor, secretHeaderInterceptor: SecretHeaderInterceptor): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.addInterceptor(secretHeaderInterceptor)
.build()
}
@Provides
@Singleton
fun provideGson(): Gson {
return GsonBuilder().create()
}
@Provides
@Singleton
@Named("retrofit")
fun provideRetrofit(gson : Gson, okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build()
}
}
这是我的Component类
@Singleton
@Component(modules = [(AppModule::class),(NetworkModule::class),(ApiModule::class),(LocalModule::class)])
interface AppComponent{
fun inject(extensionStudyActivity: ApplyExtensionStudyActivity)
fun inject(applyStayingActivity: ApplyStayingActivity)
fun inject(changePasswordActivity: ChangePasswordActivity)
fun inject(noticeActivity: NoticeActivity)
fun inject(pointLogActivity: PointLogActivity)
fun inject(registerActivity: RegisterActivity)
fun inject(signInActivity: SignInActivity)
fun inject(splashActivity: SplashActivity)
fun inject(applyMusicDialogFragment: ApplyMusicDialogFragment)
fun inject(bugReportDialogFragment: BugReportDialogFragment)
fun inject(institutionDialogFragment: InstitutionDialogFragment)
fun inject(logoutDialogFragment: LogoutDialogFragment)
fun inject(applyGoingFragment: ApplyGoingFragment)
fun inject(applyGoingDocFragment: ApplyGoingDocFragment)
fun inject(applyGoingEditFragment: ApplyGoingEditFragment)
fun inject(applyGoingLogFragment: ApplyGoingLogFragment)
fun inject(applyMusicFragment: ApplyMusicFragment)
fun inject(applyMusicLogFragment: ApplyMusicLogFragment)
fun inject(mealFragment: MealFragment)
fun inject(myPageFragment: MyPageFragment)
fun inject(noticeDescriptionFragment: NoticeDescriptionFragment)
fun inject(putInFragment: PutInFragment)
}
我该怎么办? 请解决...
答案 0 :(得分:0)
删除所有dagger2实现代码,并在下面替换为它。
def final dagger_version = '2.22.1'
implementation "com.google.dagger:dagger:${dagger_version}"
implementation "com.google.dagger:dagger-android:${dagger_version}"
implementation "com.google.dagger:dagger-android-support:${dagger_version}"
kapt "com.google.dagger:dagger-compiler:${dagger_version}"
kapt "com.google.dagger:dagger-android-processor:${dagger_version}"
并在gradle中应用apply plugin: 'kotlin-kapt'
。
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'