1

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!

Community
  • 1
  • 1
PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
  • can you please provide both class file where the intent is coming from and going to? – jace Dec 13 '16 at 01:56
  • have you tried solving the compatibility issue with android version before launching app to store? – jace Dec 13 '16 at 01:59
  • @jace I unfortunately cannot provide the class as I am under NDA to not show the source code. I will try to reconstruct it best I can tomorrow without pertinent data. With regards to compatibility issue, can you clarify what you mean? – PGMacDesign Dec 13 '16 at 06:10

2 Answers2

4

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.

  • This was, definitely, the problem. I was setting the respective Marshmallow only interface (implements) in the activity itself, which was causing previous versions (Pre-API 21, but not all phones) to crash when opened. Good catch. – PGMacDesign Dec 14 '16 at 18:52
0

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.

Patrick Lee
  • 1
  • 1
  • 1