Hilt:by viewModels():RuntimeException:无法创建 class ProductsListViewModel 的实例

Hilt: by viewModels(): RuntimeException: Cannot create an instance of class ProductsListViewModel

所有 build.gradle 个文件具有:

    implementation "com.google.dagger:hilt-android:2.28.3-alpha"
    kapt "com.google.dagger:hilt-android-compiler:2.28.3-alpha"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02"
    kapt "androidx.hilt:hilt-compiler:1.0.0-alpha02"
    kapt "com.google.dagger:hilt-android-compiler:2.28.3-alpha"
    implementation "javax.inject:javax.inject:1"

根的build.gradle:

classpath "com.google.dagger:hilt-android-gradle-plugin:2.28.3-alpha"

应用的build.gradle:

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions.annotationProcessorOptions.arguments +=
                ["room.schemaLocation": "$projectDir/schemas".toString()]
    }

我的申请:

@HiltAndroidApp
class MyApplication : MultiDexApplication() {

    override fun onCreate() {
        super.onCreate()
        Timber.plant(Timber.DebugTree())
    }

}

ApiModule:

@InstallIn(SingletonComponent::class) //@InstallIn(ApplicationComponent::class)
@Module
class ApiModule {

    @Provides
    fun products(retrofit: Retrofit): ProductsApi =
        retrofit.create(ProductsApi::class.java)

}

ProductsListViewModel:

class ProductsListViewModel @ViewModelInject constructor(
    private val getProductsListUseCase: GetProductsListUseCase
) : BaseViewModel() {

BaseViewModel:

open class BaseViewModel @ViewModelInject constructor() : ViewModel() {

ProductsListFragment:

import androidx.fragment.app.viewModels

//@AndroidEntryPoint
class ProductsListFragment : Fragment(R.layout.fragment_product_list) {

    private val productsListViewModel: ProductsListViewModel by viewModels()
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    productsListViewModel.getProductsList() //Line 57

ProductsApi:

interface ProductsApi {

    @GET("products")
    fun getProducts(
        @Query("page") page: Int = 1,
        @Query("per_page") perPage: Int = 15
    ): Call<List<ProductResponse>>

}

AppModule:

@InstallIn(SingletonComponent::class) //@InstallIn(ApplicationComponent::class)
@Module
class AppModule {

    @Provides
    @Singleton
    fun resources(application: Application): Resources = application.resources

    @Provides
    @Singleton
    fun gson(): Gson = GsonBuilder().create()

    @Provides
    @Singleton
    fun okHttpClient(baseHttpClient: BaseHttpClient): OkHttpClient = baseHttpClient.okHttpClient

    @Provides
    @Singleton
    fun retrofit(baseRetrofit: BaseRetrofit): Retrofit = baseRetrofit.retrofit

}

存储库模块:

@InstallIn(SingletonComponent::class) //@InstallIn(ApplicationComponent::class)
@Module
class RepositoryModule {

    @Suppress("PrivatePropertyName")
    private val NETWORK_IO = Executors.newFixedThreadPool(5)

    @Provides
    fun products(productsRemoteDataSource: ProductsRemoteDataSource): ProductsRepository =
        ProductsRepositoryImpl(productsRemoteDataSource, NETWORK_IO)

}

ProductsRepositoryImpl:

@Singleton
class ProductsRepositoryImpl @Inject constructor(
    private val productsRemoteDataSource: ProductsRemoteDataSource,
    private val networkExecutor: Executor
) : ProductsRepository {

ProductsRemoteDataSource:

@Singleton
class ProductsRemoteDataSource @Inject constructor(
    private val productsApi: ProductsApi
) : BaseRemoteDataSource() {

主要活动:

//@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

错误:

2020-08-04 18:17:10.575 2047-2047/? E/LoadedApk: Unable to instantiate appComponentFactory
    java.lang.ClassNotFoundException: Didn't find class "androidx.core.app.CoreComponentFactory" on path: DexPathList[[],nativeLibraryDirectories=[/system/priv-app/GoogleSdkSetup/lib/x86, /system/lib, /system/product/lib, /system/lib, /system/product/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.LoadedApk.createAppFactory(LoadedApk.java:256)
        at android.app.LoadedApk.updateApplicationInfo(LoadedApk.java:370)
        at android.app.ActivityThread.handleDispatchPackageBroadcast(ActivityThread.java:5951)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1941)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at com.android.server.SystemServer.run(SystemServer.java:541)
        at com.android.server.SystemServer.main(SystemServer.java:349)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:908)
2020-08-04 18:17:10.582 2047-2047/? E/LoadedApk: Unable to instantiate appComponentFactory
    java.lang.ClassNotFoundException: Didn't find class "androidx.core.app.CoreComponentFactory" on path: DexPathList[[],nativeLibraryDirectories=[/system/priv-app/GoogleSdkSetup/lib/x86, /system/lib, /system/product/lib, /system/lib, /system/product/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.LoadedApk.createAppFactory(LoadedApk.java:256)
        at android.app.LoadedApk.updateApplicationInfo(LoadedApk.java:370)
        at android.app.ActivityThread.handleDispatchPackageBroadcast(ActivityThread.java:5951)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1941)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at com.android.server.SystemServer.run(SystemServer.java:541)
        at com.android.server.SystemServer.main(SystemServer.java:349)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:908)
2020-08-04 18:17:10.987 1799-2440/? E/GnssHAL_GnssInterface: gnssSvStatusCb: a: input svInfo.flags is 8
2020-08-04 18:17:10.987 1799-2440/? E/GnssHAL_GnssInterface: gnssSvStatusCb: b: input svInfo.flags is 8
2020-08-04 18:17:11.442 1817-2087/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2020-08-04 18:17:11.442 1817-2087/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2020-08-04 18:17:11.795 28568-28568/debug E/AndroidRuntime: FATAL EXCEPTION: main
    Process: debug, PID: 28568
    java.lang.RuntimeException: Cannot create an instance of class ProductsListViewModel
        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
        at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
        at ProductsListFragment.getProductsListViewModel(Unknown Source:2)
        at ProductsListFragment.setupViewModel(ProductsListFragment.kt:57)
        at ProductsListFragment.onViewCreated(ProductsListFragment.kt:37)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:332)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1199)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2236)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2009)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1965)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1861)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2641)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2589)
        at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2723)
        at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1200)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1368)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1446)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1509)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2637)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2589)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1425)
        at android.app.Activity.performStart(Activity.java:7825)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3294)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2020-08-04 18:17:11.795 28568-28568/debug E/AndroidRuntime: Caused by: java.lang.InstantiationException: java.lang.Class<ProductsListViewModel> has no zero argument constructor
        at java.lang.Class.newInstance(Native Method)
        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
            ... 42 more
2020-08-04 18:17:11.990 1799-2440/? E/GnssHAL_GnssInterface: gnssSvStatusCb: a: input svInfo.flags is 8

我不得不从没有刀柄的匕首中删除所有 API。我是说这些:

    implementation "com.google.dagger:dagger:$daggerVersion"
    implementation "com.google.dagger:dagger-android-support:$daggerVersion"
    kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$daggerVersion"
    api "com.google.dagger:dagger-android:$daggerVersion"
    api "com.google.dagger:dagger-android-support:$daggerVersion"

然后,取消注释所有 @AndroidEntryPoint 个注释。
(在删除 dagger API 之前,如果我输入 @AndroidEntryPoint,我会收到错误消息):

Did you forget to apply the Gradle Plugin?

即使 +annotationProcessorOptions.arguments +=!