带有 gradle 包装器的 Kotlin 枚举类构建中的无效方法声明 [英] Invalid Method Declaration in Kotlin Enum Class building with gradle wrapper

查看:55
本文介绍了带有 gradle 包装器的 Kotlin 枚举类构建中的无效方法声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Kotlin 枚举类:

I have the following Kotlin enum class:

enum class DurationModifier {
    GreaterThan {
        override val displayName = "≥"
    },
    LessThan {
        override val displayName = "≤"
    };

    abstract val displayName: String
}

它长期以来一直是我项目的一部分,并且编译得很好.使用 IntelliJ 或 Android Studio 可以很好地编译,但是当我从命令行 (./gradlew assembleDebug) 手动运行 gradle build 时,我得到了这个:

It has been part of my project for a long time and has been compiling just fine. This compiles just fine using IntelliJ or Android Studio, but when I run the gradle build manually from the command line (./gradlew assembleDebug) I get this:

e: {projectDir}/build/tmp/kapt3/stubs/{package}/search/DurationModifier.java:17: error: invalid method declaration; return type required
        DurationModifier() {
        ^

我已经彻底清理了我能想到的所有内容(构建目录、gradle 缓存等).

I've completely cleaned everything I can think of (build directories, gradle cache, etc).

我最近进行了很多更改,但由于 IDE 中一切正常,我不知道是什么原因导致了这种情况.这里有什么问题?为什么这在 IDE 中有效,而在命令行中无效?

I've made lots of changes recently, but since everything has been working fine from the IDE I have no clue what might have caused this. What is wrong here? Why does this work in the IDE but not from the command line?

推荐答案

弄清楚发生了什么.Android Studio &IntelliJ 都使用捆绑的 JDK(AS 3.2.1 使用 1.8.0_152),因此 gradle 在该环境中执行 kapt.但是,在我的机器上,我将 Java 11 设置为默认 java.我使用 JENV 来管理多个 java 版本,所以我预感我将本地 java 版本设置为 1.8 而不是 11.之后工作正常.

Figured out what was going on. Android Studio & IntelliJ both are using a bundled JDK (AS 3.2.1 uses 1.8.0_152), so gradle was executing kapt in that environment. On my machine however I have Java 11 set as the default java. I use JENV to manage multiple java versions, so on a hunch I set the local java version to 1.8 rather than 11. Works fine after that.

我的理解是,Kotlin 编译器应该发出 Java 11 编译器应该理解的 Java 8 字节代码(我在 build.gradle 中配置了 Kotlin 编译器来执行此操作),但显然在这种情况下并非如此.

My understanding is that the Kotlin compiler is supposed to emit Java 8 byte code that the Java 11 compiler should understand (I do the Kotlin compiler configured to do so in build.gradle), but apparently that's not true in this case.

并不是对为什么发生这种情况的真正答案,但它是一个解决方案.

Not really an answer to why it is happening, but it is a solution.

这篇关于带有 gradle 包装器的 Kotlin 枚举类构建中的无效方法声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆