Gradle Project: java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics

前端 未结 8 1447
抹茶落季
抹茶落季 2020-12-14 14:50

I\'m working on a Java project and within this project I did my first try with Kotlin. I started converting some classes to Kotlin with the JavaToKoltin converter provided i

相关标签:
8条回答
  • 2020-12-14 15:20

    You need to configure your project with kotlin. So in Android Studio:

    1. click on Tools => kotlin => Configure kotlin in project

    2. Then in dialog check : All module containing kotlin files

    3. and select version

    4. press ok

    Done.

    0 讨论(0)
  • 2020-12-14 15:23

    Had the same problem, compiling my project with Ant in console. I've edded kotlin-stdlib.jar into classpath and problem has gone.

    0 讨论(0)
  • 2020-12-14 15:24

    Thanks for the comment. Indeed compile is deprecated. However the accepted answer does not work with implementation. So I looked up the java library plugin configuration and implementation depends on compileClasspath.

    So my solution for now is to add

    jar {
        manifest {
            attributes ...
        }
        // This line of code recursively collects and copies all of a project's files
        // and adds them to the JAR itself. One can extend this task, to skip certain
        // files or particular types at will
        from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    }
    

    with

    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.50"
        //...
    }
    

    I feel like this should be done by the org.jetbrains.kotlin.jvm plugin.

    Using compile instead of implementation in the dependencies of the build.gradle file solved it for me.

    0 讨论(0)
  • 2020-12-14 15:27

    This error is likely due to the fact that the simple jar task doesn’t take all its runtime dependencies.

    From gradle documentation, In your build.gradle.kts you can either create a "fatJar" task or add that to your jar task:

    tasks.withType<Jar> {
        // Otherwise you'll get a "No main manifest attribute" error
        manifest {
            attributes["Main-Class"] = "com.example.MainKt"
        }
    
        // To add all of the dependencies
        from(sourceSets.main.get().output)
    
        dependsOn(configurations.runtimeClasspath)
        from({
            configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
        })
    }
    
    0 讨论(0)
  • 2020-12-14 15:27

    Adding the following solved the issue for me:

    dependencies {
        "kotlinCompilerClasspath"(fileTree("libs/gradle-plugins/kotlin"))
    }
    

    Here is the content of libs/gradle-plugins/kotlin:

    annotations-13.0.jar
    commons-codec-1.9.jar
    commons-logging-1.2.jar
    gradle-download-task-3.4.3.jar
    gson-2.8.5.jar
    httpclient-4.5.3.jar
    httpcore-4.4.6.jar
    kotlin-android-extensions-1.3.40.jar
    kotlin-annotation-processing-gradle-1.3.40.jar
    kotlin-build-common-1.3.40.jar
    kotlin-compiler-1.3.40.jar
    kotlin-compiler-embeddable-1.3.40.jar
    kotlin-compiler-runner-1.3.40.jar
    kotlin-daemon-client-1.3.40.jar
    kotlin-gradle-plugin-1.3.40.jar
    kotlin-gradle-plugin-api-1.3.40.jar
    kotlin-gradle-plugin-model-1.3.40.jar
    kotlin-reflect-1.3.40.jar
    kotlin-runtime-1.2.71.jar
    kotlin-script-runtime-1.3.40.jar
    kotlin-scripting-common-1.3.40.jar
    kotlin-scripting-compiler-embeddable-1.3.40.jar
    kotlin-scripting-compiler-impl-embeddable-1.3.40.ja
    kotlin-scripting-jvm-1.3.40.jar
    kotlin-stdlib-1.3.40.jar
    kotlin-stdlib-common-1.3.40.jar
    kotlin-stdlib-jdk7-1.3.40.jar
    kotlin-stdlib-jdk8-1.3.40.jar
    kotlinx-coroutines-core-1.1.1.jar
    org.jetbrains.kotlin.jvm.gradle.plugin-1.3.40.jar
    trove4j-1.0.20181211.jar
    

    The complete gradle.build.kts (offline setup):

    buildscript {
        dependencies {
            classpath(fileTree("libs/gradle-plugins/kotlin"))
        }
    }
    
    plugins {
        java
        `java-library`
    }
    
    apply(plugin = "kotlin")
    
    version = "2019.06.1"
    
    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
        kotlinOptions.jvmTarget = "12"
    }
    
    repositories {
        flatDir {
            dirs("libs/compile")
            dirs("libs/provided")
        }
    }
    
    dependencies {
        "kotlinCompilerClasspath"(fileTree("libs/gradle-plugins/kotlin"))
        compileOnly(":javaee-api-8.0")
        api(":kotlin-stdlib-common-1.3.40")
        api(":kotlin-stdlib-1.3.40")
        api(":kotlin-stdlib-jdk7-1.3.40")
        api(":kotlin-stdlib-jdk8-1.3.40")
        api(":gson-2.8.5")
    }
    
    0 讨论(0)
  • 2020-12-14 15:31

    Adding all project files to the jar fixed the problem for me. I added the following line to my build.gradle

    jar {
        manifest {
            attributes ...
        }
        // This line of code recursively collects and copies all of a project's files
        // and adds them to the JAR itself. One can extend this task, to skip certain
        // files or particular types at will
        from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    }
    

    Update: Changed configurations.compile.collect to configurations.compileClasspath.collect according to this answer below.

    0 讨论(0)
提交回复
热议问题