android - 无法使用 Hilt 创建 MainViewModel 的实例

标签 android kotlin mvvm dagger dagger-hilt

我正在用一个简单的项目测试刀柄,我想要实现的是用刀柄生成我的 MainViewModel 的一个实例,这是我到目前为止所做的
主要 Activity

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
 ...
}
主要 fragment
@AndroidEntryPoint
class MainFragment : Fragment(),MainAdapter.OnTragoClickListener {

    private val viewModel by activityViewModels<MainViewModel>()

...
}
主视图模型
class MainViewModel @ViewModelInject constructor(private val repo:Repo):ViewModel(){
...
}
RepoImpl
class RepoImpl @Inject constructor(private val dataSource: DataSource): Repo {
...
}
数据源实现
class DataSourceImpl @Inject constructor(private val tragosDao: TragosDao): DataSource{
...
}
现在,这是应用程序遵循的架构,这里是 RepoDataSource是我使用的简单接口(interface)。
因此,在此之后,我生成了生成实例所需的所有内容
基础应用
@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()

}
上面的模块提供了 tragoDao(),所以我可以在我的 DataSourceImpl 中访问它,因为我需要这个数据库的唯一实例,所以我使用 @Singleton在其提供
然后我只创建另一个模块,让 hilt 知道上面接口(interface)的实现
@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: Cannot create an instance of class com.g.tragosapp.ui.viewmodel.MainViewModel


依赖项
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
DataSourceImpl 也是如此
然后改@InstallIn(ActivityRetainedComponent::class)@InstallIn(SingletonComponent::class) (以前是ApplicationComponent)
并确保拥有所有这些部门(在撰写本文时):
buildscript {
    ext {
        dagger_version = '2.41'
    }

dependencies {
    classpath "com.google.dagger:hilt-android-gradle-plugin:$dagger_version"
}
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'kotlin-kapt'

implementation "com.google.dagger:dagger:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
implementation "com.google.dagger:hilt-android:$dagger_version"
kapt "com.google.dagger:hilt-android-compiler:$dagger_version"
kaptTest "com.google.dagger:hilt-android-compiler:$dagger_version"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:$dagger_version"
kapt 'androidx.hilt:hilt-compiler:1.0.0'

关于android - 无法使用 Hilt 创建 MainViewModel 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63208779/

相关文章:

android - 计时器结束后重定向到其他 fragment

java - 将无限期运行的 Runnable 从 java 转换为 kotlin

wpf - 如何获得 WPF 验证错误

c# - WPF Caliburn.Micro/mvvm 导航

android - 如何确定电脑上安装的android SDK的版本?

android - 是否可以让 setOnClickListener block 等待其中的方法调用?

java - 如何在android中使用SwipeRefreshLayout和imageView进入NestedScrollView

c# - MVVM 对 Silverlight/WPF 是否比 MVP 对 Webforms 更必要/更重要?

java - android.content.res.Resources$NotFoundException : String resource ID #0x2 Database

android - AppCompat 21 lollipop 更新使 Robolectric 单元测试失败并出现VerifyError