question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NoClassDefFoundError within anonymous class when proguarded

See original GitHub issue

Problematic code

    private final List<Listener> listeners = new ArrayList<>();

    @Override
    public void addListener(Listener listener) {
        if (listener == null || listeners.contains(listener))
            throw new IllegalStateException();

        listeners.add(listener);
    }

    @Override
    public void removeListener(Listener listener) {
        if (listener == null || !listeners.contains(listener))
            throw new IllegalStateException();

        listeners.remove(listener);
    }

    public interface Listener {
        void started(Exercise exercise);
    }

    private final Listener notifier = new Listener() {
        @Override
        public void started(Exercise exercise) {
            listeners.forEach(listener -> listener.started(exercise)); // <-- throws NoClassDefFoundError (on proguard)
        }
    };

This works

Instead of listeners.forEach(listener -> listener.started(exercise));, if we iterate like below it works even with proguard:

            for (Listener listener : listeners) {
                listener.started(exercise);
            }

Config

classpath 'me.tatarka:gradle-retrolambda:3.5.0' // in project build.gradle
apply plugin: 'me.tatarka.retrolambda' // last entry in app build.gradle

// https://github.com/orfjackal/retrolambda/issues/25
retrolambda {
      jvmArgs '-noverify'
}

retrolambdaConfig 'net.orfjackal.retrolambda:retrolambda:2.5.1' // within app dependencies. Even without this got the same error.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
technoir42commented, Mar 5, 2017

@rpattabi I think you might be getting NoClassDefFoundError because Iterable.forEach takes an instance of Consumer interface which is only available on API 24+. That seems like pretty normal behaviour because Retrolambda does not backport any Java 8 APIs.

0reactions
rpattabicommented, Mar 7, 2017

Are you building for nougat only?

No. I have min sdk: 16 and target sdk: 24.

forEach takes an argument (java.util.function.Consumer<? super T> action), and that for anything less than Nougat the class won’t exist

I see what you mean. This code compiles which is surprising to me. I guess it is due to latest build tools I am using.

Thanks for the work around. I will close this issue since retrolambda or gradle-retrolambda doesn’t have anything to do here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.lang.NoClassDefFoundError: in anonymous inner class
It sounds like the JVM can't find the class file for the anonymous class. This would be named 'MyClassImpl$1.class' - if it's not...
Read more >
3 ways to solve java.lang.NoClassDefFoundError in Java J2EE
NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available at compile...
Read more >
Crash due synthetic constructor with anonymous inner class ...
Proguarded builds replace Foo$1 parameter for a byte parameter in Bar's synthetic ... java.lang.NoClassDefFoundError: a/a/a/a at java.lang.Class.
Read more >
java.lang.NoClassDefFoundError: in Companion
Somehow your build process is dropping some classes (proguard maybe?). That class in question is an anonymous inner class (likely a ...
Read more >
java.lang.ClassNotFoundException: com.android.billingclient ...
-keep class com.android.billingclient.** {*; } ... I've added that line in proguard but it seems that it does not work for me.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found