我正在尝试将我的Java代码迁移到Kotlin。
我有一个Hilt模块的问题:
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Provides
@Singleton
fun provideRetrofit(
gsonConverterFactory: GsonConverterFactory?,
rxJava3CallAdapterFactory: RxJava3CallAdapterFactory?,
okHttpClient: OkHttpClient?
): Retrofit {
return Retrofit.Builder().baseUrl(Configuration.URL_API)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava3CallAdapterFactory)
.client(okHttpClient)
.build()
}
@Provides
@Singleton
fun provideContext(application: Application): Context {
return application
}
@Provides
@Singleton
fun provideHttpClient(application: Application?): OkHttpClient {
val dispatcher = Dispatcher()
//HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//logging.setLevel(HttpLoggingInterceptor.Level.BODY);
val client =OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.dispatcher(dispatcher)
client.addInterceptor(ConnectivityIntecepter(application))
val interceptor = Interceptor { chain: Interceptor.Chain ->
val original = chain.request()
val request = original.newBuilder()
.method(original.method, original.body)
.build()
chain.proceed(request)
}
client.addInterceptor(interceptor)
//client.interceptors().add(logging);
return client.build()
}
@Provides
@Singleton
fun provideGson(): Gson {
return GsonBuilder().setLenient().create()
}
@Provides
@Singleton
fun providesGsonConverterFactory(): GsonConverterFactory {
return GsonConverterFactory.create()
}
@Provides
@Singleton
fun providesRxJavaCallAdapterFactory(): RxJava3CallAdapterFactory {
return RxJava3CallAdapterFactory.create()
}
@Provides
@Singleton
fun provideServicee(retrofit: Retrofit): ApiService {
return retrofit.create(ApiService::class.java)
}
}
当我尝试编译时,我得到以下错误:
.../app/build/generated/hilt/component_sources/debug/org/my/app/BaseApplication_HiltComponents.java:153:错误:[Dagger/MissingBinding] org.my.app.data.db.dao.TodayDao不能没有@ Provides注解的方法提供. public抽象静态类SingletonC实现了FragmentGetContextFix.FragmentGetContextFixEntryPoint^ org.my.app.data.db.dao.TodayDao在org.my.app.workers.TodayWorker.mTodayDao org.my.app.workers.TodayWorker_AssistedFactory在org.my.app.workers.TodayWorker_HiltModule.bind.(工厂)java.util.Map〈java.lang.String,javax.inject.Provider〈androidx.hilt.work.WorkerAssistedFactory〈?extends androidx.work.ListenableWorker〉〉〉在androidx.hilt.work.WorkerFactoryModule.provideFactory注入(workerFactories)androidx.hilt.work.HiltWorkerFactory在org.my.app.BaseApplication.workerFactory org.my.app.BaseApplication在org.my.app.BaseApplication_GeneratedInjector.injectBaseApplication(org.my.app.BaseApplication)也可在以下地址请求:
org.my.app.repository.BreviarioRepository(...,todayDao,...)
org.my.app.repository.ComentariosRepository(...,todayDao)
org.my.app.repository.HomiliasRepository(...,todayDao)
org.my.app.repository.LecturasRepository(...,mTodayDao)
org.my.app.repository.SantosRepository(...,todayDao)
org.my.app.repository.SyncRepository(...,todayDao,...)以下其他入口点也依赖于它:
dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap()[org.my.app.BaseApplication_HiltComponents.SingletonC → org.my.app.BaseApplication_HiltComponents.ActivityRetainedC → org.my.app.BaseApplication_HiltComponents.ViewModelC]
我找不到代码中的错误。
这是Java中的原始代码:
@Module
@InstallIn(SingletonComponent.class)
public abstract class NetworkModule {
@Provides
@Singleton
static Retrofit provideRetrofit(GsonConverterFactory gsonConverterFactory,
RxJava3CallAdapterFactory rxJava3CallAdapterFactory,
OkHttpClient okHttpClient
) {
return new Retrofit.Builder().baseUrl(URL_API)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava3CallAdapterFactory)
.client(okHttpClient)
.build();
}
@Provides
@Singleton
static Context provideContext(Application application) {
return application;
}
@Provides
@Singleton
static OkHttpClient provideHttpClient(Application application) {
Dispatcher dispatcher = new Dispatcher();
//HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.dispatcher(dispatcher);
client.addInterceptor(new ConnectivityIntecepter(application));
Interceptor interceptor = chain -> {
Request original = chain.request();
Request request = original.newBuilder()
.method(original.method(), original.body())
.build();
return chain.proceed(request);
};
client.addInterceptor(interceptor);
//client.interceptors().add(logging);
return client.build();
}
@Provides
@Singleton
static Gson provideGson() {
return new GsonBuilder().setLenient().create();
}
@Provides
@Singleton
static GsonConverterFactory providesGsonConverterFactory() {
return GsonConverterFactory.create();
}
@Provides
@Singleton
static RxJava3CallAdapterFactory providesRxJavaCallAdapterFactory() {
return RxJava3CallAdapterFactory.create();
}
@Provides
@Singleton
static ApiService provideService(Retrofit retrofit) {
return retrofit.create(ApiService.class);
}
}
我已经使用了Android Studio转换工具,应该注意的是,一些注入依赖项的类仍然是用Java编写的,我不知道这是否是问题的原因。我正在部分迁移文件并修复小错误,但在这里我无法让它工作。
1条答案
按热度按时间1rhkuytd1#
在将带有注解的java项目转换为Kotlin时,请确保将
apply plugin: 'kotlin-kapt'
添加到模块build.gradle文件中。这将允许kotlin处理它们。