@InstallIn注释的类也必须用@Module或@EntryPoint错误注释

Far*_*ary 5 android kotlin dagger-hilt

我不知道为什么我收到“@InstallIn 注释的类还必须用 @Module 或 @EntryPoint 注释:com.example.newsapp.di.AppModule [Hilt] 处理未完成。有关详细信息,请参阅上面的错误。” 错误。我使用教程编写了这段代码,导师的代码(与我的完全相同)工作正常。`

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
  @Provides
    fun baseURL() = BASE_URL

    @Provides
    fun logging() = HttpLoggingInterceptor()
        .setLevel(HttpLoggingInterceptor.Level.BODY)

    @Provides
    fun okHttpClient() = okhttp3.OkHttpClient.Builder()
        .addInterceptor(logging())
        .build()


    @Provides
    @Singleton
    fun provideRetrofit(baseUrl: String): NewsService =
        Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient())
            .build()
            .create(NewsService::class.java)}`
Run Code Online (Sandbox Code Playgroud)

Far*_*ary 7

我检查了导入,发现我从错误的位置导入了模块。

  • 正如目前所写的,您的答案尚不清楚。请[编辑]添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以[在帮助中心](/help/how-to-answer)找到有关如何写出好的答案的更多信息。 (2认同)