这是主要的应用程序代码。让我注意,它是在AndroidManifest中声明的。
class MainApplication: Application(), KodeinAware{
override val kodein = Kodein.lazy{
bind() from singleton {TestClass()}
import(androidXModule(this@MainApplication))
}
}
当我尝试从Activity(即KodeinAware)访问TestClass()实例时:
override val kodein by closestKodein()
private val test: TestClass by instance()
TestClass()是:
class TestClass{
val x = 5
}
然后,例如在onCreate中
val x = test.x
它崩溃了。注释掉androidXModule的导入后,它可以正常工作。显然,这里没有使用androidXModule的功能,但其他实例需要使用它。在一个没有MainActivity,TestClass和自定义MainApplication的简单项目上对此进行了测试。 这是logcat异常:
2019-02-01 19:16:12.472 20895-20895/com.x.x.x E/AndroidRuntime: FATAL
EXCEPTION: main
Process: com.x.x.x, PID: 20895
java.lang.NoSuchMethodError: No static method importOnce$default(Lorg/kodein/di/Kodein$Builder;Lorg/kodein/di/Kodein$Module;ZILjava/lang/Object;)V in class Lorg/kodein/di/Kodein$Builder; or its super classes (declaration of 'org.kodein.di.Kodein$Builder' appears in /data/app/com.x.x.x-Y3HhfW-NAj95_Rb4y3S51A==/split_lib_dependencies_apk.apk)
at org.kodein.di.android.x.ModuleKt$androidXModule$1.invoke(module.kt:17)
at org.kodein.di.android.x.ModuleKt$androidXModule$1.invoke(Unknown Source:2)
at org.kodein.di.internal.KodeinBuilderImpl.import(KodeinBuilderImpl.kt:46)
at org.kodein.di.Kodein$Builder$DefaultImpls.import$default(Kodein.kt:301)
at com.x.x.x.MainApplication$kodein$1.invoke(MainApplication.kt:21)
at com.x.x.x.MainApplication$kodein$1.invoke(MainApplication.kt:15)
at org.kodein.di.internal.KodeinImpl$Companion.newBuilder(KodeinImpl.kt:22)
at org.kodein.di.internal.KodeinImpl$Companion.access$newBuilder(KodeinImpl.kt:21)
at org.kodein.di.internal.KodeinImpl.<init>(KodeinImpl.kt:19)
at org.kodein.di.Kodein$Companion$lazy$1.invoke(Kodein.kt:414)
at org.kodein.di.Kodein$Companion$lazy$1.invoke(Kodein.kt:396)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at org.kodein.di.LazyKodein.getBaseKodein(Unknown Source:7)
at org.kodein.di.LazyKodein.getContainer(lateinit.kt:29)
at org.kodein.di.KodeinAwareKt$Instance$1.invoke(KodeinAware.kt:157)
at org.kodein.di.KodeinAwareKt$Instance$1.invoke(Unknown Source:8)
at org.kodein.di.KodeinProperty$provideDelegate$1.invoke(properties.kt:42)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.x.x..MainActivity.getTest(Unknown Source:25)
at com.x.x.x.MainActivity.onCreate(MainActivity.kt:22)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
“在com.x.x.x.MainApplication $ kodein $ 1.invoke(MainApplication.kt:21)处”行指向MainApplication内部的androidXModule导入