运行与androidstudio混合java的代码时失败

时间:2017-06-05 03:07:16

标签: java kotlin

我的项目结构

( “https://i.stack.imgur.com/EykkL.png”)

Build.gradle内容如下

apply plugin: 'java'
apply plugin: 'kotlin'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"
buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
repositories {
    mavenCentral()
}

// override the sourceSet flow other answers.
sourceSets {
    main.java.srcDirs+= 'src/main/java'
    main.kotlin.srcDirs+= 'src/main/kotlin'
    main.resources.srcDirs += 'src/main/resources'
}

我运行它时出现异常

它可能是成功编译的,但是当我运行kotlin代码时。 它抛出了下面的例外。并且java代码运行正常。

Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
    at KotMainKt.main(kotMain.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

1 个答案:

答案 0 :(得分:0)

我在build.gradle中添加以下代码并修复此问题。

链接:Configuring Gradle with Kotlin

jar {     清单{         属性'Main-Class':'KotMainKt'     }

from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }

}