7

One of our beta testers using a T-mobile Galaxy S4 running Android 4.4.4 is experiencing very strange NoClassDefFoundError crashes. I'm completely at a loss for why these could be happening.

The most recent one happens when a static method is called in onCreateView in a fragment.

MyCustomClass.getNumberOfItems(getActivity());

fails with

Fatal Exception: java.lang.NoClassDefFoundError com.mycompany.myapp.utilities.MyCustomClass$5

Here's the method in question:

public static List<Item> getNumberOfItems(final Context context)
{
    List<Item> allFinishedItems = getAllFinishedItems(context);

    CollectionUtils.filter(allFinishedItems, new Predicate<Item>()
    {
        @Override
        public boolean evaluate(Item item)
        {
            // TODO Auto-generated method stub
            return isNonCustomItem(context, item);
        }
    });

    return allFinishedItems;
}

1.) what is the "$5" after the class name? (answer: reference to anonymous class for filtering with CollectionUtils.filter) 2.) this user had another crash of similar nature, but with a completely different static method call that's in a library that's included via gradle. The crash I'm referencing in this question is coming from code that is part of my own library project, and the same static method call works in other places in the app. This problem seems to be spreading, or at least not contained to 1 class or 1 library.

We're targeting the following Android versions in build.gradle:

minSdkVersion 14
targetSdkVersion 22  (android 5.1)

What could possibly be going on here? One other thing to note is that the signed APKs were generated using Android Studio 2.0 preview 4. However, the app works fine for 20-30 other beta testers, so I'm hesitant to point the finger at using a preview version of Studio.

DiscDev
  • 38,652
  • 20
  • 117
  • 133
  • "1.) what is the "$5" after the class name?" `MyCustomClass$5` is the name of an anonymous class defined in `MyCustomClass`. It is not necessarily the 5th anonymous class to appear in the source code of that class, but it is likely that you have at least 5 in there. – Andy Turner Jan 25 '16 at 16:50
  • @AndyTurner I'm guessing it's the CollectionUtils.filter call that's inside the "getNumberOfItems" method. I'll update my question now with the code for that method. – DiscDev Jan 25 '16 at 16:52
  • 2
    Here is one potential reason related to multidex setup incorrectly http://stackoverflow.com/questions/27698287/noclassdeffounderror-with-android-studio-on-android-4. Not sure if you are using it but maybe worth a check. – George Mulligan Jan 25 '16 at 16:56
  • @GeorgeMulligan you may be on to something. Really good find! I'll report back if that helps. – DiscDev Jan 25 '16 at 16:58
  • did you enable proguard? if yes put your `proguard-rules.pro` above – Rod_Algonquin Jan 25 '16 at 17:02
  • @GeorgeMulligan you were right! Thanks!!! – DiscDev Jan 25 '16 at 20:12

2 Answers2

8

The NoClassDefFoundErrors were happening because multidex was only partially implemented in this app - for some reason, this works fine on Android 5/6 but on Android 4.x it makes your app crash with NoClassDefFoundError in random places. It seems to me like this situation should be detected by Android Studio and you should be warned you've improperly implemented multidex support.

To fix it, make sure the following is true for your project:

  1. multiDexEnabled = true in the defaultConfig section of your app-level build.gradle

  2. compile 'com.android.support:multidex:1.0.0' in your project-level build.gradle

  3. Call MultiDex.install() in attachBaseContext() of your Application class.

More details on multidex can be found here.

DiscDev
  • 38,652
  • 20
  • 117
  • 133
  • 6
    the above three points i already added in my project. Still am getting the same error. Also error was in Android OS 6. – Rameshbabu Jun 01 '17 at 04:20
  • 1
    same with me, the above approach already added, but still showing error. I'm afraid this is due to AAB that installed via other than playstore? is it related to this [GloballyDynamic tools](https://github.com/jeppeman/GloballyDynamic) – mochadwi Dec 29 '20 at 06:19
0

Make sure to setup the pre-requisites as others answer pointed out here, if the crash still occurred you might want to try this approach as well https://developer.android.com/studio/build/multidex#keep

When building each DEX file for a multidex app, the build tools perform complex decision-making to determine which classes are needed in the primary DEX file so that your app can start successfully. If any class that's required during startup is not provided in the primary DEX file, then your app crashes with the error java.lang.NoClassDefFoundError

using proguard

EDITED:

Or you can try this approach for ease of use registering your classes in the primary DEX

https://medium.com/androidiots/androidiots-podcast-6-the-black-magic-behind-android-runtime-part-2-9390f751eef0

mochadwi
  • 1,190
  • 9
  • 32
  • 87