Android: NoSuchMethodError with static method in interface (using Retrolambda)
Asked Answered
B

1

2

In my project I am trying to use code written in Java 8 in Android app using Retrolambda plugin and Lightweight-Stream-API to bypass code compatibility. The source project runs well on Java platform (clarified).

After porting the code to Android, I only had to make slight modifications in code (mostly about getting instance of com.annimon.stream.Stream class, because arrays and collections in Android lack method .stream() so I need to use static methods of Stream class with array/collection as parameter), but this shouldn't even be the concern of my problem. Currently there are no compilation errors, the application starts, works, but is crashing upon calling:

Optional<Room> result = INamed.getO(name, Stream.of(rooms));

With an error:

FATAL EXCEPTION: main

Process: cz.alois_seckar.vseadventrura, PID: 8109

java.lang.NoSuchMethodError: No static method getO(Ljava/lang/String;Lcom/annimon/stream/Stream;)Lcom/annimon/stream/Optional; in class Lcz/alois_seckar/vseadventrura/eu/pedu/adv16s_fw/game_txt/INamed; or its super classes (declaration of 'cz.alois_seckar.vseadventrura.eu.pedu.adv16s_fw.game_txt.INamed' appears in /data/data/cz.alois_seckar.vseadventrura/files/instant-run/dex/slice-slice_1-classes.dex)
    at cz.alois_seckar.vseadventrura.eu.pedu.adv16s_fw.test_util.default_game_txt.game.Apartment.getORoom(Apartment.java:166)
    at cz.alois_seckar.vseadventrura.eu.pedu.adv16s_fw.test_util.default_game_txt.game.Room$$Lambda$1.apply(Unknown Source)
    at com.annimon.stream.Stream$12.nextIteration(Stream.java:539)
    ...

And the class Room implements INamed through its ancestors (Room extends AItemContainer that extends ANamed that implements INamed).

So I dont really understand, what shall be the problem. I would suspect Retrolambda plugin doesnt work exactly same as Java 8 does, but unable to tell what to change and how... Also the problem may be completely elsewhere, I am only beginning with Android...

I can provide more code, if needed, but I think everything important has been told. Thanks in advance for any help.

Bethesda answered 22/4, 2016 at 7:58 Comment(2)
Is the stream from INamed.getO() the same com/annimon/stream/Stream ? and not the one from JDKExhale
in all my classes com.annimon.stream.Stream; is imported...JDK one cannot even work as it is not included in Android library...Bethesda
B
6

Problem solved: I have found out it is Retrolambda's fault - or rather my fault. I overlooked the fact, that I have to enable support for default and static methods in Retrolambda's config in build.gradle

The simple code goes like:

retrolambda {
    defaultMethods true
}

After I did that, Retrolambda did all the back-end stuff and the method is being found now.

Bethesda answered 25/4, 2016 at 7:35 Comment(2)
It's worth reading what you lose by enabling this feature. It impacted my build times so significantly that I disabled defaultMethods again. github.com/orfjackal/retrolambda#known-limitationsAndrel
Thanks for pointing this out this @MauriceGavin. Very important for anyone who is considering using statics or default interface methods in Android.Ikhnaton

© 2022 - 2024 — McMap. All rights reserved.