Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Method Declaration in Kotlin Enum Class building with gradle wrapper

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?

like image 342
Jordan Avatar asked Nov 13 '18 20:11

Jordan


People also ask

Can enum class have methods Kotlin?

Since Kotlin enums are classes, they can have their own properties, methods, and implement interfaces.

How do I get the enum value on Kotlin?

Get enum by value Kotlin First, you can use values() method of Enum to get all the enum constants of the enum class as an array. Next, you can find the enum constant by using its value. In the below code, Enum class has a variable rgb and each enum constant has a value associated with it.

Can enum implement an interface in Kotlin?

In Kotlin, you can also have enums implement interfaces. In such cases, each data value of enum would need to provide an implementation of all the abstract members of the interface. While calling these interface methods, we can directly use the enum value and call the method.


2 Answers

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.

like image 154
Jordan Avatar answered Oct 17 '22 19:10

Jordan


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
like image 24
Georg Moser Avatar answered Oct 17 '22 18:10

Georg Moser