【问题标题】:Cannot create instance of MainViewModel with Hilt无法使用 Hilt 创建 MainViewModel 的实例
【发布时间】:2020-11-22 07:13:01
【问题描述】:

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

主要活动

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
 ...
}

主片段

@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 {
...
}

DataSourceImpl

class DataSourceImpl @Inject constructor(private val tragosDao: TragosDao): DataSource{
...
}

现在,这是应用程序遵循的架构,这里 RepoDataSource 是我使用的简单接口。

因此,在此之后,我生成了生成实例所需的所有内容

基础应用程序

@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 了解上述接口的实现

@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

依赖关系

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"

没有任何区别

【问题讨论】:

  • 添加kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha02'
  • 哇,成功了,谢谢

标签: android kotlin mvvm dagger dagger-hilt


【解决方案1】:
class RepoImpl

应该是

@Singleton class RepoImpl

DataSourceImpl 也一样

然后把@InstallIn(ActivityRetainedComponent::class)改成@InstallIn(SingletonComponent::class)(以前是ApplicationComponent)

并确保拥有所有这些部门(在撰写本文时):

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'

【讨论】:

  • 我似乎没有 SingletonComponent::class 可以安装,而且,做这些类单例有什么区别?谢谢
  • 标记单例并添加 ApplicationComponent 返回相同的错误创建实例
  • 如果您将它们设为单例,那么它们将与我尝试安装它的组件共享相同的范围。这应该会减少一些错误的可能性。 ApplicationComponent 正在重命名为 SingletonComponent,它可能会因您当前的版本而异。如果 HiltViewModelProviderFactory 仍未被使用,请检查您是否有 apply plugin 来实际运行 Hilt gradle 插件。
  • @EpicPandaForce 你能帮我解决一个疑问吗? Hilt 不适用于演示者或视图类?
  • @RavindraKushwaha 我不可能知道你的设置,所以我不能回答你的问题,也不能回答你在找什么。 “演示者”不是 Android 开发的常态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-09
  • 2021-07-26
  • 2016-06-21
  • 2019-11-21
  • 2021-04-24
  • 2015-10-14
  • 1970-01-01
相关资源
最近更新 更多