Invalid Method Declaration in Kotlin Enum Class building with gradle wrapper

2.1k Views Asked by At

I have the following Kotlin enum class:

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

    abstract val displayName: String
}

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() {
        ^

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

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?

3

There are 3 best solutions below

2
On BEST ANSWER

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.

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.

0
On

I also encountered this problem and just downloaded Java 8 and changed Java location in Project Structure (File\Project Structure) here:

enter image description here

0
On

I had a similar error with an abstract enum function. Gradle was using JDK 11. Switching to JDK 8, by adding an org.gradle.java.home entry to gradle.properties (in your HOME/.gradle/gradle.properties or in your project specifig gradle.properties resolved the issue.

 echo 'org.gradle.java.home=PATH_TO_JDK8' >> ~/.gradle/gradle.properties