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?
Since Kotlin enums are classes, they can have their own properties, methods, and implement interfaces.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With