'Kotlin's JvmDefault - still need to declare the method ?

I am building my Spring Boot 1.5 + Kotlin 1.2.41 project into a jar. One of the interfaces in the jar has the @JvmDefault and it compiles fine with the flag (if I remove the flag, it fails).

Now, I am trying to use this interface in another java project, in which I define the Kotlin project as a dependency.

In one implementing class, I don't override the default method. Intellij seems to be OK with it, as it doesn't complain. However, when I compile with Maven, I get :

[ERROR] attempting to assign weaker access privileges; was public

If I implement the method (with some dummy implementation), then it compiles... but it defeats the purpose of the default interface.

Any idea what could be wrong ?

When opening the Kotlin interface code from the java project, here's the decompiled code I see :

public interface CrawlerOutput {
    @kotlin.jvm.JvmDefault public open fun finalize(): kotlin.Unit { /* compiled code */ }

    public abstract fun output(analyzedRepository: com.myCompany.Repository): kotlin.Unit
}

My java code implementing the interface :

public class CsvOutput implements CrawlerOutput {

    @Override
    public void output(Repository repository) throws IOException {
        log.info("own output is receiving some data !");
    }

    /**
    * IF I REMOVE BELOW METHOD, MAVEN CAN'T COMPILE IT ANYMORE, 
    * COMPLAINING OF WEAKER ACCESS PRIVILEGE
    */      
    @Override
    public void finalize(){

    }

}

Am I missing something ?

Thanks

Vincent



Solution 1:[1]

Android Studio and JVM always update its versions. As a result of that some of you may experience this error message.

Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option

Don't worry . The solution is very simple. Just add below code part to the end of android block of your app level build.gradle file and sync.

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
 kotlinOptions {
 freeCompilerArgs += [
"-Xjvm-default=all",
]
}
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Revan siddappa