mvvm dagger 2 ViewModelInjector

时间:2019-01-08 03:14:22

标签: android mvvm dagger-2

我需要帮助,我是.NET开发人员,但现在我想尝试使用android。 我从这里下载了一个项目:https://github.com/gahfy/MVVMPosts

im在BaseViewModel.kt中遇到问题。 我知道DaggerViewMOdelInjector是由dagger 2使用我的界面ViewModelInjector创建的,但未创建,并且我无法构建该项目,请帮帮我!

package net.gahfy.mvvmposts.base

import android.arch.lifecycle.ViewModel
import net.gahfy.mvvmposts.injection.component.DaggerViewModelInjector
import net.gahfy.mvvmposts.injection.component.ViewModelInjector
import net.gahfy.mvvmposts.injection.module.NetworkModule
import net.gahfy.mvvmposts.ui.post.PostListViewModel
import net.gahfy.mvvmposts.ui.post.PostViewModel

abstract class BaseViewModel:ViewModel(){
    private val injector: ViewModelInjector = DaggerViewModelInjector
            .builder()
            .networkModule(NetworkModule)
            .build()

    init {
        inject()
    }

    /**
     * Injects the required dependencies
     */
    private fun inject() {
        when (this) {
            is PostListViewModel -> injector.inject(this)
            is PostViewModel -> injector.inject(this)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

更新方式如下:- implementation "com.google.dagger:dagger:$dagger2_version" kapt "com.google.dagger:dagger-compiler:$dagger2_version" annotationProcessor "com.google.dagger:dagger-android-processor:$dagger2_version" // compileOnly "org.glassfish:javax.annotation:3.1.1"