2

在 Debug 版本中它工作正常,但在 Release 版本中失败。我想这与 Proguard 规则有关。

// Koin
implementation 'org.koin:koin-android:1.0.2'
implementation 'org.koin:koin-android-viewmodel:1.0.2'

模块:

val appModule = module {
    viewModel<IntroVM>()
}


val dataModule = module {
    single { NetworkManager(androidContext().applicationContext) }
    single { LocalFileManager(androidContext().applicationContext) }
    single { ApiModel(get()) }

类定义:

class ApiModel(val networkManager: NetworkManager){}

class LocalFileManager(private var appContext: Context) {}

class NetworkManager(private var appContext: Context) {}

class IntroVM(var apiModel: ApiModel) : CommonVM() {

Koin 初始化:

startKoin(this, listOf(appModule, dataModule))

保护规则:

-keepattributes *Annotation*
-keepattributes SourceFile,LineNumberTable
-keep public class * extends java.lang.Exception
-keepclassmembernames class kotlinx.** {
    volatile <fields>;
}

堆栈跟踪:

Error while resolving instance for class 'com...viewModels.auth.b' -      
error: org.koin.error.BeanInstanceCreationException:
Can't create definition for 'Factory [name='b',class='com..viewModels.auth.b', binds~(androidx.lifecycle.r)]' due to error :'binds~(androidx.lifecycle.r)]
No constructor found for class 'class com...viewModels.auth.b'
4

2 回答 2

0

当您使用 androidX 时,请使用下面的 viewmodel 库 for koin

implementation 'org.koin:koin-androidx-viewmodel:1.0.2'
于 2019-03-18T13:19:12.667 回答
0

这个 问题提供了需要的解决方案

于 2019-03-18T15:13:27.693 回答