Java 8 interface with default method invalid method when Jacobo is enabled
We have an interface that uses default methods. We implement this interface in Java and kotlin classes, and we provide the implementation of non default methods
When we run in debug mode (no testoverageenabled = true), the application works as expected However, when we run different configurations with testoverageenabled = true, the application crashes with the following error
java.lang.NoSuchMethodError: No static method $$triggerInterfaceInit()V in class Lcom/ui/viewholders/CAViewContract$$CC; or its super classes (declaration of 'ui.viewholders.CAViewContract$$CC' appears in /data/app/SMCXbiLYvHb1Kk08Kee__g==/base.apk) at home.c.CCFragment.<clinit>(UnkNown Source:0) at home.HomePageCardProvider.getFragment(HomePageCardProvider.java:17) at home.HomeFragment.handleCardFragment(HomeFragment.java:172)
Note: 1 Jacobo version: "0.8.0" 2 Operating system: Android with minsdk 21
If we change minsdk to 24 and testoverageenabled = true itself, it is working We can't figure out the exact problem
Solution
This problem occurs if you want to call the default implementation of a method that does not have a default implementation in the interface where the class explicitly implements it (however, there is a default implementation in the basic (parent, super) interface of this interface)
Example: suppose these definitions:
class A implements DerivedInterface /*,...*/ { @Override public void methodFromBaseInterface() { DerivedInterface.super.methodFromBaseInterface(); // Error: // NoSuchMethodError: No static method methodFromBaseInterface } // ... } interface DerivedInterface extends BaseInterface { // ... // `methodFromBaseInterface` hasn't overriden here. } interface BaseInterface { default void methodFromBaseInterface() { /* ...*/ } // ... }
Then execute:
A a = new A(); a.methodFromBaseInterface(); // This cause above error!
And you get wrong on the above points!
(side note: you may need to define at least one method in derivedinterface to avoid getting NoClassDefFoundError at runtime!)
This is similar to an error! We use super keywords Why expect static methods?!! Another point is that the above code has no problem. You can run it in any Java 8 compatible environment without any problem!
I think this problem is related to the incomplete support of Java 8 Language API in Android platform:
In that page, pay special attention to the Java 8 Language API and the compatible minsdkversion table: