Invalid Method Declaration in Kotlin Enum Class building with gradle wrapper
Asked Answered
T

3

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?

Timms answered 13/11, 2018 at 20:48 Comment(2)
Have you tried refactoring your enum so it doesn't generate anonymous classes? enum class DurationModifier(val displayName: String){ GreaterThan("≥"), LessThan("≤") }.Walkin
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.Timms
T
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.

Timms answered 13/11, 2018 at 22:48 Comment(2)
THis is not about the Java8 byte code. This is about kapt3 generating plain Java code that the jdk 11 does not like.Thordia
Using java 8 instead of 12. Solved my problem. Thank you for posting the answer @Timms BondoAllene
J
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
Juieta answered 21/3, 2019 at 18:0 Comment(0)
N
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

Nev answered 9/4, 2020 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.