Kotlin集合在Android上抛出ClassNotFoundException

前端之家收集整理的这篇文章主要介绍了Kotlin集合在Android上抛出ClassNotFoundException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Android上运行应用程序时,使用下面的任何行都会引发ClassNotFoundException.

val list = listOf("a","b")
val arrayList = arrayListOf("a","b")
val map = mapOf("key" to "value")

例外:

04-28 15:12:00.770 27326-27326/com.example.kotlin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kotlin,PID: 27326
java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/collections/CollectionsKt;
    at com.example.kotlin.MainActivity.onCreate(MainActivity.kt:17)
    at android.app.Activity.performCreate(Activity.java:6237)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.collections.CollectionsKt" on path: DexPathList[[zip file "/data/app/com.example.kotlin-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.kotlin-1/lib/x86,/vendor/lib,/system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    at com.example.kotlin.MainActivity.onCreate(MainActivity.kt:17) 
    at android.app.Activity.performCreate(Activity.java:6237) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
    Suppressed: java.lang.ClassNotFoundException: kotlin.collections.CollectionsKt
    at java.lang.Class.classForName(Native Method)
    at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
    at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 14 more
 Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

以下是我的app模块gradle文件

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.0 rc3"

    defaultConfig {
        applicationId "com.example.kotlin"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    compile fileTree(dir: 'libs',include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:gridlayout-v7:23.3.0'
    compile 'com.android.support:cardview-v7:23.3.0'

    kapt 'com.android.databinding:compiler:2.1.0-beta3'
}

buildscript {
    ext.kotlin_version = '1.0.1-2'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
repositories {
    mavenCentral()
}

kapt {
    generateStubs = true
}

为了使用任何Kotlin集合类,我还需要添加到gradle中吗?

最佳答案
您需要依赖包含这些类的Kotlin标准库:

dependencies {
   /* ... */
   compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

猜你在找的Android相关文章