【问题标题】:how to configure build.gradle.kts to fix error "Duplicate JVM class name generated from: package-fragment"如何配置 build.gradle.kts 以修复错误“Duplicate JVM class name generated from: package-fragment”
【发布时间】:2020-08-13 04:02:32
【问题描述】:

我正在尝试遵循本教程https://dev.to/tagmg/step-by-step-guide-to-building-web-api-with-kotlin-and-dropwizard,而是在 Kotlin 的 DSL 中编写我的 gradle.build 文件,我发现从 Groovy 到 Kotlin 没有直接映射,我现在在运行 @987654322 时遇到此错误@:

(4, 1): Duplicate JVM class name 'dropwizard/tut/AppKt' generated from: package-fragment dropwizard.tut, package-fragment dropwizard.tut
plugins {
    // Apply the Kotlin JVM plugin to add support for Kotlin on the JVM.
    id("org.jetbrains.kotlin.jvm").version("1.3.31")

    // Apply the application plugin to add support for building a CLI application.
    application
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    mavenCentral()
    jcenter()
}

dependencies {
    // Use the Kotlin JDK 8 standard library.
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    // Use the Kotlin test library.
    testImplementation("org.jetbrains.kotlin:kotlin-test")

    // Use the Kotlin JUnit integration.
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")

    compile("io.dropwizard:dropwizard-core:1.3.14")
}

application {
    // Define the main class for the application
    mainClassName = "dropwizard.tut.AppKt"
}

tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = application.mainClassName 
    }
    from({
        configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
    })
}

tasks.named<JavaExec>("run") {
    args("server", "config/local.yaml")
}

【问题讨论】:

    标签: kotlin build.gradle gradle-kotlin-dsl


    【解决方案1】:

    我还不能告诉(还)为什么会发生这种情况,但要解决它,请将 @file:JvmName("SomethingUnique") 添加到您的 JVM 文件中。请注意,重命名文件将无济于事并导致相同的错误。只有更改输出名称才能解决它。

    【讨论】:

      【解决方案2】:

      JVM 只知道如何加载类,因此 Kotlin-to-JVM 编译器生成类来保存顶级 valfun 声明。

      当您有两个名称相似的文件时

      // src/commonMain/kotlin/com/example/Foo.kt
      package com.example
      
      val a = 1
      

      // src/jvmMain/kotlin/com/example/Foo.kt
      package com.example
      
      val b = 2
      

      kotlin-to-JVM 编译器生成

      package com.example;
      public class FooKt {
        public static final int a = 1;
      }
      

      public com.example;
      public class FooKt {
        public static final int b = 2;
      }
      

      很明显,这两个文件不能共存于同一个JVM ClassLoader中,因此报错。

      解决方案涉及:

      • 正如@Fleshgrinder 所指出的,向至少一个添加文件级 JvmName 注释以覆盖派生名称 FooKt
      • 尽可能将文件重命名为不同的文件。
      • 将顶级 valfun 声明从这些文件移动到其他文件中,因此 Kotlin 不需要创建 FooKt 类。
      • 将顶级 valfun 声明移动到 objects 或 companion objects。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-26
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 2020-10-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多