我正试图在我的Android应用程序中使用Kotlin,并且无法调用扩展功能。应用程序立即崩溃,并显示以下堆栈跟踪:
04-23 13:31:22.415 32606-32606/com.example.kotlin.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.kotlin.test, PID: 32606
java.lang.ExceptionInInitializerError
at org.jetbrains.kotlin.load.kotlin.reflect.RuntimeModuleData$Companion.create(RuntimeModuleData.kt:34)
at kotlin.reflect.jvm.internal.InternalPackage$moduleByClassLoader$cff17278.getOrCreateModule(moduleByClassLoader.kt:54)
at kotlin.reflect.jvm.internal.KClassImpl$descriptor$1.invoke(KClassImpl.kt:31)
at kotlin.reflect.jvm.internal.KClassImpl$descriptor$1.invoke(KClassImpl.kt:26)
at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.get(ReflectProperties.java:84)
at kotlin.reflect.jvm.internal.KClassImpl.getDescriptor(KClassImpl.kt)
at kotlin.reflect.jvm.internal.KClassImpl.getScope(KClassImpl.kt:43)
at kotlin.reflect.jvm.internal.KClassImpl.getProperties(KClassImpl.kt:84)
at kotlin.reflect.jvm.internal.KClassImpl.getProperties(KClassImpl.kt:72)
at kotlin.reflect.ReflectPackage$KClassExtensions$6beb0f9b.getDeclaredProperties(KClassExtensions.kt:25)
at kotlin.reflect.ReflectPackage.getDeclaredProperties(KClassExtensions.kt:1)
at com.example.test.kotlin.api.model.DatabaseObject$$TImpl.deflate(DatabaseObject.kt:40)
at com.example.test.kotlin.api.model.impl.Model.deflate(Model.kt)
at com.example.test.kotlin.api.model.impl.Model.<init>(Model.kt:28)
at com.example.test.kotlin.api.model.impl.TestModel.<init>(TestModel.kt:23)
at com.example.app.TestModelIndexFragment.onCreate(TestModelIndexFragment.java:122)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
at android.app.Activity.performStart(Activity.java:5949)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: java.lang.IllegalStateException: Built-in library initialization failed. Please ensure you have kotlin-runtime.jar in the classpath: java.lang.IllegalStateException: Resource not found in classpath: kotlin/.kotlin_string_table
at org.jetbrains.kotlin.builtins.KotlinBuiltIns.initialize(KotlinBuiltIns.java:75)
at org.jetbrains.kotlin.builtins.KotlinBuiltIns.getInstance(KotlinBuiltIns.java:93)
at org.jetbrains.kotlin.platform.JavaToKotlinClassMapBuilder.init(JavaToKotlinClassMapBuilder.java:35)
at org.jetbrains.kotlin.platform.JavaToKotlinClassMap.<init>(JavaToKotlinClassMap.java:44)
at org.jetbrains.kotlin.platform.JavaToKotlinClassMap.<clinit>(JavaToKotlinClassMap.java:35)
at org.jetbrains.kotlin.load.kotlin.reflect.RuntimeModuleData$Companion.create(RuntimeModuleData.kt:34)
at kotlin.reflect.jvm.internal.InternalPackage$moduleByClassLoader$cff17278.getOrCreateModule(moduleByClassLoader.kt:54)
at kotlin.reflect.jvm.internal.KClassImpl$descriptor$1.invoke(KClassImpl.kt:31)
at kotlin.reflect.jvm.internal.KClassImpl$descriptor$1.invoke(KClassImpl.kt:26)
at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.get(ReflectProperties.java:84)
at kotlin.reflect.jvm.internal.KClassImpl.getDescriptor(KClassImpl.kt)
at kotlin.reflect.jvm.internal.KClassImpl.getScope(KClassImpl.kt:43)
at kotlin.reflect.jvm.internal.KClassImpl.getProperties(KClassImpl.kt:84)
at kotlin.reflect.jvm.internal.KClassImpl.getProperties(KClassImpl.kt:72)
at kotlin.reflect.ReflectPackage$KClassExtensions$6beb0f9b.getDeclaredProperties(KClassExtensions.kt:25)
at kotlin.reflect.ReflectPackage.getDeclaredProperties(KClassExtensions.kt:1)
at com.example.test.kotlin.api.model.DatabaseObject$$TImpl.deflate(DatabaseObject.kt:40)
at com.example.test.kotlin.api.model.impl.Model.deflate(Model.kt)
at com.example.test.kotlin.api.model.impl.Model.<init>(Model.kt:28)
at com.example.test.kotlin.api.model.impl.TestModel.<init>(TestModel.kt:23)
at com.example.app.TestModelIndexFragment.onCreate(TestModelIndexFragment.java:122)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
at android.app.Activity.performStart(Activity.java:5949)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: java.lang.IllegalStateException: Resource not found in classpath: kotlin/.kotlin_string_table
at org.jetbrains.kotlin.builtins.BuiltinsPackageFragment.getStream(BuiltinsPackageFragment.kt:86)
at org.jetbrains.kotlin.builtins.BuiltinsPackageFragment.<init>(BuiltinsPackageFragment.kt:55)
at org.jetbrains.kotlin.builtins.KotlinBuiltIns.<init>(KotlinBuiltIns.java:114)
at org.jetbrains.kotlin.builtins.KotlinBuiltIns.initialize(KotlinBuiltIns.java:70)
at org.jetbrains.kotlin.builtins.KotlinBuiltIns.getInstance(KotlinBuiltIns.java:93)
at org.jetbrains.kotlin.platform.JavaToKotlinClassMapBuilder.init(JavaToKotlinClassMapBuilder.java:35)
at org.jetbrains.kotlin.platform.JavaToKotlinClassMap.<init>(JavaToKotlinClassMap.java:44)
at org.jetbrains.kotlin.platform.JavaToKotlinClassMap.<clinit>(JavaToKotlinClassMap.java:35)
at org.jetbrains.kotlin.load.kotlin.reflect.RuntimeModuleData$Companion.create(RuntimeModuleData.kt:34)
at kotlin.reflect.jvm.internal.InternalPackage$moduleByClassLoader$cff17278.getOrCreateModule(moduleByClassLoader.kt:54)
at kotlin.reflect.jvm.internal.KClassImpl$descriptor$1.invoke(KClassImpl.kt:31)
at kotlin.reflect.jvm.internal.KClassImpl$descriptor$1.invoke(KClassImpl.kt:26)
at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.get(ReflectProperties.java:84)
at kotlin.reflect.jvm.internal.KClassImpl.getDescriptor(KClassImpl.kt)
at kotlin.reflect.jvm.internal.KClassImpl.getScope(KClassImpl.kt:43)
at kotlin.reflect.jvm.internal.KClassImpl.getProperties(KClassImpl.kt:84)
at kotlin.reflect.jvm.internal.KClassImpl.getProperties(KClassImpl.kt:72)
at kotlin.reflect.ReflectPackage$KClassExtensions$6beb0f9b.getDeclaredProperties(KClassExtensions.kt:25)
at kotlin.reflect.ReflectPackage.getDeclaredProperties(KClassExtensions.kt:1)
at com.example.test.kotlin.api.model.DatabaseObject$$TImpl.deflate(DatabaseObject.kt:40)
at com.example.test.kotlin.api.model.impl.Model.deflate(Model.kt)
at com.example.test.kotlin.api.model.impl.Model.<init>(Model.kt:28)
at com.example.test.kotlin.api.model.impl.TestModel.<init>(TestModel.kt:23)
at com.example.app.TestModelIndexFragment.onCreate(TestModelIndexFragment.java:122)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
at android.app.Activity.performStart(Activity.java:5949)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
我的项目由两个模块test-kt
和app
组成,分别用于新的Kotlin内容和现有的Java代码。 test-kt/build.gradle
有以下内容:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 9
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.kotlin.srcDirs += 'src/main/kotlin'
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}
buildscript {
ext.kotlin_version = '0.11.91.2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
repositories {
mavenCentral()
}
我尝试在app
内添加Kotlin库作为依赖项,但没有运气。有什么我想念的吗?
编辑:进一步调查后,看起来只有在我使用属性引用(例如::x
)时才会发生这种情况,而不是在我使用扩展方法时。我绝对包括kotlin-reflect
......任何想法?
答案 0 :(得分:0)
我在升级到Kotlin版本0.11.91.2时也遇到过这种情况。显然存在一个问题,即Android工具集没有将带有点的文件打包到资源中。 (请注意,异常引用的文件.kotlin_string_table前面带有一个点)。
A fix已经编写但尚未发布。您可以尝试使用Kotlin的快照版本来利用它,或者只是等待下一次更新。