Android: NoSuchMethodError with static method in i

2019-07-16 05:18发布

问题:

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.

回答1:

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.