Android 无法使用刀柄创建MainViewModel的实例
我正在用一个简单的项目测试hilt,我想实现的是用hilt生成我的MainViewModel的一个实例这就是我到目前为止所做的 主要活动 主要片段 RepoImpl 数据源MPL 现在,这是应用程序遵循的架构,这里Android 无法使用刀柄创建MainViewModel的实例,android,kotlin,mvvm,dagger,dagger-hilt,Android,Kotlin,Mvvm,Dagger,Dagger Hilt,我正在用一个简单的项目测试hilt,我想实现的是用hilt生成我的MainViewModel的一个实例这就是我到目前为止所做的 主要活动 主要片段 RepoImpl 数据源MPL 现在,这是应用程序遵循的架构,这里Repo和DataSource是我使用的简单接口 因此,在此之后,我将生成hilt生成实例所需的所有内容 基本应用程序 应用模块 上面的模块提供了tragoDao(),因此我可以在我的数据源impl中访问它,因为我需要这个数据库的唯一实例,所以我在它的提供上使用@Singleton 然
Repo
和DataSource
是我使用的简单接口
因此,在此之后,我将生成hilt生成实例所需的所有内容
基本应用程序
应用模块
上面的模块提供了tragoDao(),因此我可以在我的数据源impl
中访问它,因为我需要这个数据库的唯一实例,所以我在它的提供上使用@Singleton
然后我创建另一个模块,让hilt知道上面接口的实现
@Module
@InstallIn(ActivityRetainedComponent::class)
abstract class ActivityModule {
@Binds
abstract fun bindDataSource(dataSource:DataSourceImpl): DataSource
@Binds
abstract fun bindRepo(repo: RepoImpl): Repo
}
因为我需要MainViewModel的实例,所以我使用ActivityRetainedComponent
编译应用程序后,我收到此错误
java.lang.RuntimeException:无法创建类的实例
com.g.tragosapp.ui.viewmodel.MainViewModel
依赖关系
我还补充说
implementation "androidx.fragment:fragment-ktx:1.2.5"
implementation "androidx.activity:activity-ktx:1.1.0"
implementation "androidx.core:core:1.3.1"
这没什么区别
class RepoImpl
应该是
@Singleton class RepoImpl
对于DataSourceImpl
然后将@InstallIn(ActivityRetainedComponent::class)
更改为@InstallIn(SingletonComponent::class)
(以前是ApplicationComponent)
并确保(在撰写本文时)拥有所有这些DEP:
我似乎没有要安装的SingletonComponent::class,而且,单独安装这些类有什么区别?感谢标记singleton并添加ApplicationComponent会返回创建实例的相同错误如果将它们设置为singleton,则它们将与我尝试安装它的组件共享相同的范围。这将减少一些错误的可能性。ApplicationComponent正在重命名为SingletonComponent,它可能因您当前的版本而异。如果HiltViewModelProviderFactory仍然没有被使用,请检查您是否有
应用插件
来实际运行Hilt gradle插件。@EpicPandaForce您能帮我解答一个疑问吗。希尔特不与演示者或查看类一起工作?@RavindraKushwaha我不可能知道您的设置,因此我无法回答您的问题,也无法回答您在寻找什么。“演示者”不是Android开发中的标准。添加kapt'androidx.hilt:hilt编译器:1.0.0-alpha02'
哇,这很有效,谢谢
class DataSourceImpl @Inject constructor(private val tragosDao: TragosDao): DataSource{
...
}
@HiltAndroidApp
class BaseApplication: Application() {
}
@Module
@InstallIn(ApplicationComponent::class)
object AppModule {
@Singleton
@Provides
fun provideRoomInstance(
@ApplicationContext context: Context
) = Room.databaseBuilder(
context,
AppDatabase::class.java,
"tabla_tragos")
.build()
@Singleton
@Provides
fun provideTragosDao(db: AppDatabase) = db.tragoDao()
}
@Module
@InstallIn(ActivityRetainedComponent::class)
abstract class ActivityModule {
@Binds
abstract fun bindDataSource(dataSource:DataSourceImpl): DataSource
@Binds
abstract fun bindRepo(repo: RepoImpl): Repo
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
//Navigation Components
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
//ViewModel y LiveData
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
// KTX - Viewmodel Y Livedata
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0-alpha05"
implementation "androidx.fragment:fragment-ktx:1.2.5"
implementation "androidx.activity:activity-ktx:1.1.0"
//utils
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
//Corutinas
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3"
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
implementation 'com.github.chrisbanes:PhotoView:2.3.0'
//Room
implementation 'androidx.room:room-ktx:2.2.5'
implementation "androidx.room:room-runtime:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"
//Hilt
implementation "com.google.dagger:hilt-android:2.28-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
implementation "androidx.fragment:fragment-ktx:1.2.5"
implementation "androidx.activity:activity-ktx:1.1.0"
implementation "androidx.core:core:1.3.1"
class RepoImpl
@Singleton class RepoImpl
apply plugin: 'dagger.hilt.android.plugin'
implementation 'com.google.dagger:dagger:2.28'
kapt 'com.google.dagger:dagger-compiler:2.28'
// hilt
implementation 'com.google.dagger:hilt-android:2.28-alpha'
kapt 'com.google.dagger:hilt-android-compiler:2.28-alpha'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'
kaptTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'