PGMacDesign
PGMacDesign

Reputation: 6312

java.lang.NoClassDefFoundError being thrown In some devices, but not all

I get Crash Analytics from Google for my Android app and I have been seeing this popup once in a while:

java.lang.NoClassDefFoundError

with regards to trying to open an Intent.

The scenario is, user clicks on button, they my code in the app is:

Intent intent = new Intent(nameOfCurrentActivity.this, nameOfNewActivity.class);
startActivity(intent);

Pretty straightforward stuff. It works fine on nearly all devices, including all the ones I own and have tested on. This new class being started isn't unique in that it requires any weird hardware (IE, not a camera activity), but it does access the internet via an Http request.

I have already researched the following links without gaining a hint towards a solution:

My question is, how is it possible that this exception is being thrown on some devices (IE, a Samsung tablet), but not other devices? Shouldn't a new intent work on all devices if it works on one?

Thanks!

Upvotes: 1

Views: 746

Answers (2)

Patrick Lee
Patrick Lee

Reputation: 1

I just wanted to add my own experience today. Apparently Supplier<T> exists in Java 8, however implementation of it and running it on Marshmallow generates NoClassDefFoundError. I was able to solve the problem by declaring custom MySupplier<T>. I tried to add as a comment but failed. Thanks for the explanation given by user7293284 above.

Upvotes: 0

user7293284
user7293284

Reputation:

A lot of times this can be caused by classes running code that is dependent upon it being a certain API level, IE Marshmallow, but the device using it is on a previous API and the check for permissions is either ignored or not included; Like when you click disable inspection.

An example would be, say you are running something like a View.OnScrollChangeListener for a recyclerview. If you are coding and set it:

    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        myRecyclerview.setOnScrollChangeListener(this);
    }

But don't include the build if check, it will throw the error. Another example would be if you are using the class itself as the context (this) for setting the scroll listener. If you use the class itself as the context for extending the Scroll listener but are on a device Pre-API 21, it will throw the error when the class loads. The error it throws is, as you probably guessed, NoClassDefError.

This happens because the Class mentioned in the 'future' API doesn't exist yet in the old phone and so it cannot find the class defined.

Check your code to see if anything in the class is requiring a certain API level to function and if it is, check to confirm you included the if checks for build version. Many times before I have disabled inspection because the red lines were bugging me and forgot to go back and add the checks.

Upvotes: 4

Related Questions