Android 如果dagger kotlin中没有@Inject构造函数或@Providers注释方法,则无法提供
我正在将我的一个项目转换为kotlin源代码。 在建造匕首时,我遇到了这个问题Android 如果dagger kotlin中没有@Inject构造函数或@Providers注释方法,则无法提供,android,kotlin,dagger,Android,Kotlin,Dagger,我正在将我的一个项目转换为kotlin源代码。 在建造匕首时,我遇到了这个问题 cannot be provided without an @Inject constructor or an @Provides-annotated method. 我有如下两个模块 @Module class AppClient { @Provides @Singleton fun provideHttpCache(application: Application): Cache { val cache
cannot be provided without an @Inject constructor or an @Provides-annotated method.
我有如下两个模块
@Module
class AppClient {
@Provides
@Singleton
fun provideHttpCache(application: Application): Cache {
val cacheSize = 10 * 1024 * 1024
return Cache(application.cacheDir, cacheSize.toLong())
}
@Provides
@Singleton
fun provideGson(): Gson {
val gsonBuilder = GsonBuilder()
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
// gsonBuilder.excludeFieldsWithoutExposeAnnotation();
return gsonBuilder.create()
}
@Provides
@Singleton
fun provideOkhttpClient(cache: Cache): OkHttpClient {
val interceptor = HttpLoggingInterceptor()
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
val client = OkHttpClient.Builder()
client.cache(cache)
client.addInterceptor(interceptor)
return client.build()
}
@Provides
@Singleton
fun provideRetrofit(gson: Gson, okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(NetworkConstant.BASE_URL)
.client(okHttpClient)
.build()
}
@Provides
@Singleton
fun provideApiCall(retrofit: Retrofit): NetworkCall {
return retrofit.create(NetworkCall::class.java)
}
@Provides
@Singleton
fun provideSharedPreference(application: Application): SharedPreferences {
return application.applicationContext.getSharedPreferences(
application.getString(R.string.shared_pref_name),
MODE_PRIVATE
)
}
@Provides
@Singleton
fun provideSharedPreferenceEditor(sharedPreferences: SharedPreferences): SharedPreferences.Editor {
return sharedPreferences.edit()
}
@Provides
@Singleton
fun provideAppPresenter(
sharedPreferences: SharedPreferences, editor: SharedPreferences.Editor,
apiCall: NetworkCall): AppPresenter {
return AppPresenter(sharedPreferences, editor, apiCall)
}
}
第二个模块
@Module
class AppModule(val application: MyApplication) {
@Provides
@Singleton
internal fun provideApplication(): MyApplication {
return application
}
}
应用程序类
class MyApplication : Application() {
lateinit var mApiComponent: AppComponent
override fun onCreate() {
super.onCreate()
mApiComponent = DaggerAppComponent.builder()
.appModule(AppModule(this))
.appClient(AppClient())
.build()
}
fun getAppComponent(): AppComponent {
return mApiComponent
}
}
以及组件接口
@Singleton
@Component(modules = [AppModule::class, AppClient::class])
interface AppComponent {
fun inject(activity: LoginActivity)
}
有人能帮我解决一下这里出了什么问题吗?看来我通过了错误的应用程序类。我太傻了顺便说一句,不要使用单独的模块来提供应用程序。改用
@Component.Builder
或@Component.Factory
@Component.Builder
不推荐使用。因此,请使用@Component.Factory