运行与androidstudio混合java的代码失败
我的项目结构
(“ 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
我在build.gradle中添加下面的代码并修复这个问题。
链接: 用Kotlin配置Gradle
jar {manifest {attributes'Main-Class':'KotMainKt'}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}