7

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?

yole
  • 92,896
  • 20
  • 260
  • 197
Jordan
  • 4,133
  • 1
  • 27
  • 43
  • 1
    Have you tried refactoring your enum so it doesn't generate anonymous classes? `enum class DurationModifier(val displayName: String){ GreaterThan("≥"), LessThan("≤") }`. – Pawel Nov 13 '18 at 21:14
  • Tried that and it does fix this particular issue, but started dumping out all kinds of other problems. Appears to be some problem with kapt & Java 11. See my answer below. – Jordan Nov 13 '18 at 22:49

3 Answers3

7

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.

Jordan
  • 4,133
  • 1
  • 27
  • 43
2

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
Georg Moser
  • 620
  • 7
  • 14
0

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

Amin Keshavarzian
  • 3,646
  • 1
  • 37
  • 38