- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从头开始检查了所有内容,但找不到错误。我找不到错误,可能是什么?
我收到以下编译错误;
HomeViewModel.java:6: error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public final class HomeViewModel extends androidx.lifecycle.ViewModel {
^
list 文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.movielistandroid">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name="com.example.movielistandroid.Application"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MovieListAndroid">
<activity android:name=".ui.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
应用程序.kt
package com.example.movielistandroid
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class Application : Application() {}
应用模块.kt
package com.example.movielistandroid.di
import com.example.movielistandroid.BuildConfig
import com.example.movielistandroid.data.remote.MovieService
import com.example.movielistandroid.data.repositories.MoviesRepository
import com.example.movielistandroid.usecase.repositories.MoviesRepositoryImpl
import com.example.movielistandroid.utils.Constants
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Singleton
@Provides
fun provideOkHttpClient(): ...
@Singleton
@Provides
fun provideRetrofit(okHttpClient: OkHttpClient): ...
@Provides
@Singleton
fun provideApiService(retrofit: Retrofit): ...
@Provides
@Singleton
fun provideMoviesRepository(movieService: MovieService): ...
}
MoviesRepository.kt
package com.example.movielistandroid.data.repositories
import MoviesResponseModel
import com.example.movielistandroid.data.remote.MovieService
interface MoviesRepository {
...
}
MoviesRepositoryImpl.kt
package com.example.movielistandroid.usecase.repositories
import com.example.movielistandroid.data.remote.MovieService
import com.example.movielistandroid.data.remote.MovieService.Companion.MOVIE_API_KEY
import com.example.movielistandroid.data.repositories.MoviesRepository
import javax.inject.Inject
import javax.inject.Singleton
class MoviesRepositoryImpl
@Inject constructor(
private val movieService: MovieService
) : MoviesRepository {
override suspend fun getCurrentPlayingMovies() = movieService.getCurrentPlayingMovies(apiKey = MOVIE_API_KEY)
override suspend fun getUpComingMovies() = movieService.getUpComingMovies(apiKey = MOVIE_API_KEY);
}
HomeViewModel.kt
package com.example.movielistandroid.ui.home
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import com.example.movielistandroid.data.repositories.MoviesRepository
import com.example.movielistandroid.utils.Resource
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject
class HomeViewModel
@Inject constructor(
private val moviesRepository: MoviesRepository
) : ViewModel() {
fun getUpComingMovies() = liveData(Dispatchers.IO) {
...
}
fun getCurrentPlayingMovies() = liveData(Dispatchers.IO) {
...
}
}
HomeViewModel.java(生成)
package com.example.movielistandroid.ui.home;
import java.lang.System;
@kotlin.Metadata(mv = {1, 5, 1}, k = 1, d1 = {"\u0000\"\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0018\u00002\u00020\u0001B\u000f\b\u0007\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u00a2\u0006\u0002\u0010\u0004J\u0012\u0010\u0005\u001a\u000e\u0012\n\u0012\b\u0012\u0004\u0012\u00020\b0\u00070\u0006J\u0012\u0010\t\u001a\u000e\u0012\n\u0012\b\u0012\u0004\u0012\u00020\b0\u00070\u0006R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004\u00a2\u0006\u0002\n\u0000\u00a8\u0006\n"}, d2 = {"Lcom/example/movielistandroid/ui/home/HomeViewModel;", "Landroidx/lifecycle/ViewModel;", "moviesRepository", "Lcom/example/movielistandroid/data/repositories/MoviesRepository;", "(Lcom/example/movielistandroid/data/repositories/MoviesRepository;)V", "getCurrentPlayingMovies", "Landroidx/lifecycle/LiveData;", "Lcom/example/movielistandroid/utils/Resource;", "LMoviesResponseModel;", "getUpComingMovies", "app_debug"})
public final class HomeViewModel extends androidx.lifecycle.ViewModel {
private final com.example.movielistandroid.data.repositories.MoviesRepository moviesRepository = null;
@javax.inject.Inject()
public HomeViewModel(@org.jetbrains.annotations.NotNull()
com.example.movielistandroid.data.repositories.MoviesRepository moviesRepository) {
super();
}
@org.jetbrains.annotations.NotNull()
public final androidx.lifecycle.LiveData<com.example.movielistandroid.utils.Resource<MoviesResponseModel>> getUpComingMovies() {
return null;
}
@org.jetbrains.annotations.NotNull()
public final androidx.lifecycle.LiveData<com.example.movielistandroid.utils.Resource<MoviesResponseModel>> getCurrentPlayingMovies() {
return null;
}
}
build.gradle(应用程序)
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'androidx.navigation.safeargs.kotlin'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.movielistandroid"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}
dependencies {
def nav_version = "2.3.5"
def retrofit_version = "2.9.0"
def glide_version = "4.12.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.fragment:fragment-ktx:1.3.6'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation 'androidx.lifecycle:lifecycle-common:2.3.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.okhttp3:logging-interceptor:4.5.0'
implementation "com.github.bumptech.glide:glide:$glide_version"
annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-compiler:2.38.1"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
build.gradle(项目)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.5.21"
repositories {
google()
mavenCentral()
}
dependencies {
def nav_version = "2.3.5"
classpath "com.android.tools.build:gradle:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
最佳答案
您需要添加@HiltViewModel
在您的 vm 中,如果您使用 hilt vm factory 在您的 Activity/fragment 中注入(inject) vm。
此外,您可以绑定(bind)您的 MoviesRepositoryImpl
与 MoviesRepository
您已经在为 MoviesRepositoryImpl
使用构造函数注入(inject),而不是为其编写提供程序。
怎么样 -
@Module
@InstallIn(SingletonComponent::class)
interface MoviesRepositoryImplModule {
@Binds
fun bindMoviesRepositoryImpl(movieRepoImp: MoviesRepositoryImpl): MoviesRepository
}
并删除
@Provides
@Singleton
fun provideMoviesRepository(movieService: MovieService): MoviesRepository = MoviesRepositoryImpl(movieService)
来自
AppModule
关于android - 我收到 'circular dependency' Android Dagger Hilt 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68791911/
要添加 Hilt 模块,您需要执行以下操作: @Module @InstallIn(SingletonComponent::class) abstract class MyModule{ ... }
我收到这个 gradle 同步错误 - 应用了 Hilt Android Gradle 插件,但未找到 com.google.dagger:hilt-android 依赖项。 有人可以帮忙吗? :)
我最近一直在尝试将我的 Android 应用程序从 Dagger 迁移到 Hilt。 我想分阶段进行整个迁移,因此试图抑制不使用 @InstallIn 模块的 Hilt 警告。 一直遵循此处给出的迁移
我收到错误消息“已应用 Hilt Android Gradle 插件,但未找到 com.google.dagger:hilt-android-compiler 依赖项。”在构建项目时。 这是我如何将刀
在 Dagger Hilt 查看模型 1.0.0-alpha01 implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-al
我对 Android 刀柄有疑问。 我添加了 hilt 插件。 //build.gradle(:project) buildscript { ext.hilt_version = '2.37'
我正在尝试将刀柄用于包含动态功能的项目。我面临一个我无法完全理解原因的错误。我收到这样的错误: java.lang.ClassCastException: com.social.analysis.Da
美好的一天,我有使用 dagger2 的经验,但我想在 Hilt 中获得一些经验,所以我从演示应用程序开始,将演示从 Dagger2 迁移到 Hilt。在我的 Dagger 应用程序中,我有两个组件
我正在尝试使用 RoomDatabase.Callback() 方法用数据预填充 Room 数据库,并且也成功了。后来我尝试对 Hilt 进行同样的操作,但无法弄清楚如何在 hilt 模块中提供数据库
@Module @InstallIn(SingletonComponent::class) object NetworkModule { @Singleton @Provides
请考虑以下类(class): class MainRepository constructor( private val blogDao: BlogDao, private val b
我的 Android 产品代码充满了安装各种生产实现的 Hilt 模块: @Module @InstallIn(ApplicationComponent.class) public abstract
Cannot create an instance of class com.comp.app.winners.WinnersViewModel Caused by: java.lang.Instan
我开始将 Dagger 应用程序迁移到 Hilt,首先我将 AppComponent 转换为 Hilt 自动生成的 ApplicationComponent。因此我添加了 @InstallIn(App
我试图提供一个常见的 DataStore以便可以在多个地方使用相同的首选项文件,但我收到了有用的错误消息: Cannot find symbol: DaggerMyApplication_HiltCo
该应用一安装即崩溃,并引发上述奇怪的错误。 我已经注释了如下所示的事件及其子片段。 @AndroidEntryPoint class HomeActivity : AppCompatActiv
我正在将 Hilt 用于 DI,并且我有这门课。 class ChatCore @Inject constructor() 这个类需要在fragment中注入(inject),不用将fragment标
我有下一个屏幕: @ExperimentalMaterialApi @Composable fun AccountListScreen( navController: NavControlle
我想注入(inject) viewModelscope,但我做不到。 class PostPageSource @Inject constructor( val repository: MyRepos
我想用 Dagger Hilt 设置两个 Retrofit2 客户端,因为我的应用程序从两个不同的 API 获取信息。但是当我运行我的代码时,会抛出以下异常 -> App_HiltComponents
我是一名优秀的程序员,十分优秀!