gpt4 book ai didi

android - AppModule.provideViewModelFactories(AppModule.kt) 处的 NoClassDefFoundError

转载 作者:行者123 更新时间:2023-11-29 00:52:08 29 4
gpt4 key购买 nike

我仅在 Kitkat 设备上遇到此崩溃。我的 logcat 在下面。在 getViewModel

处崩溃
Fatal Exception: java.lang.NoClassDefFoundError: kotlin.jvm.internal.Intrinsics
at com.transferhome.di.AppModule.provideViewModelFactories(AppModule.kt)
at com.transferhome.di.AppModule_ProvideViewModelFactoriesFactory.provideViewModelFactories(AppModule_ProvideViewModelFactoriesFactory.java:34)
at com.transferhome.di.DaggerAppComponent.getViewModelProviderFactory(DaggerAppComponent.java:281)
at com.transferhome.di.DaggerAppComponent.access$3500(DaggerAppComponent.java:124)
at com.transferhome.di.DaggerAppComponent$SplashActivitySubcomponentImpl.injectSplashActivity(DaggerAppComponent.java:920)
at com.transferhome.di.DaggerAppComponent$SplashActivitySubcomponentImpl.inject(DaggerAppComponent.java:916)
at com.transferhome.di.DaggerAppComponent$SplashActivitySubcomponentImpl.inject(DaggerAppComponent.java:909)
at dagger.android.DispatchingAndroidInjector.maybeInject(DispatchingAndroidInjector.java:113)
at dagger.android.DispatchingAndroidInjector.inject(DispatchingAndroidInjector.java:134)
at dagger.android.AndroidInjection.inject(AndroidInjection.java:63)
at dagger.android.support.DaggerAppCompatActivity.onCreate(DaggerAppCompatActivity.java:41)
at com.transferhome.commons.BaseActivity.onCreate(BaseActivity.kt:41)
at com.transferhome.splash.SplashActivity.onCreate(SplashActivity.kt:38)

飞溅的地方

class SplashActivity : BaseActivity() {

private lateinit var viewModel: SplashViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

viewModel = getViewModel()

viewModel.onMoveToNext.listen(this) {
Handler().postDelayed({
openActivityAndClearTask(it)
}, 2000)
}
}
}

BaseActivity.kt

abstract class BaseActivity : DaggerAppCompatActivity() {

@Inject
lateinit var factories: ViewModelProvider.Factory

inline fun <reified T : ViewModel> getViewModel(): T {
return ViewModelProviders.of(this, factories)[T::class.java]
}
}

其中 AppModule.kt

@Module
class AppModule {

@Provides
fun provideViewModelFactories(viewModels: Map<Class<out ViewModel>,
@JvmSuppressWildcards Provider<ViewModel>>):
ViewModelProvider.Factory {
return object : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
val factory = viewModels[modelClass]?.get() ?: error(
"No factory provided against ${modelClass.name}"
)
@Suppress("UNCHECKED_CAST")
return factory as T
}
}

}
}

编辑

class App : DaggerApplication() {

override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
return DaggerAppComponent.builder().addContext(this).build()
}

override fun onCreate() {
super.onCreate()
MultiDex.install(this)
}
}

最佳答案

如果这只发生在 Kitkat 上,那么它可能是一个多 dexing 问题。

您应该执行以下操作:

android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 28
multiDexEnabled true
}
...
}

dependencies {
implementation 'com.android.support:multidex:1.0.3'
}

在 AndroidManifest 中:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:name="com.acme.myapp.CustomApplication" >
...
</application>
</manifest>

在 CustomApplication 中:

public class CustomApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}

关于android - AppModule.provideViewModelFactories(AppModule.kt) 处的 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58393888/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com