24

I just experienced an awkward bug in my App.

On my Nexus 5/7, running android 5.0.1/5.0.2, everything works just fine. However if i try running the exact same code on a device with an earlier version (tested 4.4.4 and 4.3) I get the following error:

03-13 13:49:41.140  21714-21714/? E/dalvikvm﹕ Could not find class 'com.default.package.application.model.Appcomponent', referenced from method com.default.package.application.controller.DatabaseHandler.getScreenComponents
03-13 13:49:41.140  21714-21714/? E/dalvikvm﹕ Could not find class 'android.support.v7.app.ActionBarActivityDelegate$1', referenced from method android.support.v7.app.ActionBarActivityDelegate.<init>
03-13 13:49:41.140  21714-21714/? E/dalvikvm﹕ Could not find class 'android.support.v7.app.ActionBarActivityDelegateHC', referenced from method android.support.v7.app.ActionBarActivityDelegate.createDelegate
03-13 13:49:41.140  21714-21714/? E/dalvikvm﹕ Could not find class 'android.support.v7.app.ActionBarActivityDelegateBase', referenced from method android.support.v7.app.ActionBarActivityDelegate.createDelegate
03-13 13:49:41.150  21714-21714/? E/dalvikvm﹕ Could not find class 'android.support.v7.app.ActionBarActivityDelegate$ActionBarDrawableToggleImpl', referenced from method android.support.v7.app.ActionBarActivityDelegate.getDrawerToggleDelegate
03-13 13:49:41.150  21714-21714/? E/dalvikvm﹕ Could not find class 'android.support.v7.internal.view.SupportMenuInflater', referenced from method android.support.v7.app.ActionBarActivityDelegate.getMenuInflater
03-13 13:49:41.150  21714-21714/? E/dalvikvm﹕ Could not find class 'android.support.v7.app.ActionBarActivityDelegate$ActionBarDrawableToggleImpl', referenced from method android.support.v7.app.ActionBarActivityDelegate.getV7DrawerToggleDelegate
03-13 13:49:41.150  21714-21714/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.default.package, PID: 21714
    java.lang.NoClassDefFoundError: android.support.v7.app.ActionBarActivityDelegateHC

I've already tried adding the support.v7 library as jar, but makes no difference. But since it works on Lollipop devices this wouldn't make sense anyway.

Could it be that there's some issue concerning the Dalvik/Art change? Or maybe the fact that I had to use com.android.support:multidex:1.0.0 since it's a rather large app.

Update: I tried removing some dependencies to get under the 65k method limit. After that the app ran on 4.4.4 and 4.3 devices. All I did for enabling multidex support was setting

multiDexEnabled true

in the defaultConfig section and adding

compile 'com.android.support:multidex:1.0.0'

below in the dependencies section of my build.gradle.

Any idea why this causes these issues on the older android versions?

user2700475
  • 827
  • 2
  • 7
  • 16
  • 1
    create new lollipop project in eclipse and add its appcompat to this project – karan Mar 13 '15 at 13:47
  • 1
    Where does that `com.default.package.application.model.appcomponent` come from ? – Jonas Czech Mar 13 '15 at 14:21
  • I tried creating a new project in Android Studio and adding all the existing sources, but i still get the same issue. com.default.package is just a placeholder for my actual package name here. – user2700475 Mar 13 '15 at 20:04

3 Answers3

43

I resolved the issue by adding this to my Application Class.

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

Seems to be neccessary for android versions prior 5.

user2700475
  • 827
  • 2
  • 7
  • 16
  • 1
    are you extending your Application class by MultiDexApplication? "public class App extends MultiDexApplication "...aswell? – sirvon Apr 01 '15 at 11:56
  • 3
    public class MyApplication extends MultiDexApplication { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } – sonida Apr 20 '15 at 10:27
  • Ahh...mistakenly upvoted it :( Its still not working for pre-kitkat versions – Narendra Singh Jan 22 '16 at 10:47
  • Fixed it for me, however, if you're using Robolectric then unittests will probably fail. I've answered a similar question [here](https://stackoverflow.com/questions/37998266/android-studio-many-error-could-not-find-class-android-xxx#38666791), with some extra links and info to solve several issues around Multidex. – P Kuijpers Jul 29 '16 at 20:26
  • Thanks for your solution, I'm trying to use same thing in my react-native project. But I get errors regarding appOpsManager. Do you have nay idea why? – Niloufar Dec 21 '19 at 12:56
12

There are three ways how this is going to work.

  1. As @FireZenk noted you can declare this class as the application in your AndroidManifest.xml.
<application android:name="android.support.multidex.MultiDexApplication">

And actually with the android grade plugin version 0.14.0 and build tools version 21.1.0 when you specify the multiDexEnabled true in the defaultConfig, ProductFlavor, or BuildTypethen it automatically includes dependence to the com.android.support:multidex:1.0.0. You can check that with the androidDependencie task or you run ./gradlew -q android:dependencies command (android: is my android module). So there is no need to explicitly add the

compile 'com.android.support:multidex:1.0.0'

If you have extended Application class in your app:

  1. Define that your Application extends MultiDexApplication class, like:
public class MyApplication extends MultiDexApplication {
    ...
}

or like @user2700475 posted

  1. Have your Application override attachBaseContext:
public class MyApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);}
}

With the help of previous answers I manage to find the solution to this problem. I just want to put all this together.

RenatoIvancic
  • 1,798
  • 3
  • 21
  • 36
  • Thanks the first step helped! – Sourav Chandra Nov 27 '15 at 12:41
  • I'd recommend the 3rd option, however, if you're using Robolectric then unittests will probably fail. I've answered a similar question [here](https://stackoverflow.com/questions/37998266/android-studio-many-error-could-not-find-class-android-xxx#38666791), with some extra links and info to solve several issues around Multidex. – P Kuijpers Jul 29 '16 at 20:24
6

If you don't have/use Application class, you can put this:

android:name="android.support.multidex.MultiDexApplication"

Into your tag on AndroidManifest.xml

If you already have implemented an Application class, @user2700475 + @sirvon responses are your best choice.

Also obviously, you need to add the Gradle dependency:

compile 'com.android.support:multidex:1.0.0'

More info about 65k methods problem: https://developer.android.com/tools/building/multidex.html

FireZenk
  • 1,056
  • 1
  • 16
  • 28