2

Not always but sometimes i see this exception in logcat while running my app

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.classname" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

The app doesn't crash and this com.example.classname is present in my project. I am using android studio 2.2 and I am using multidex for our project. my doubt is this issue is because of multidex.

Any idea about how can i fix this exception?

our build.gradle file is big. i am adding some part of it

     compileSdkVersion 23
        buildToolsVersion '23.0.3'

        defaultConfig {
            minSdkVersion 17
            targetSdkVersion 22
    multiDexEnabled true
    ......
    }
dependencies {
    compile 'com.android.support:multidex:1.0.1'
        testCompile('org.robolectric:shadows-multidex:3.0') {
            exclude group: 'org.robolectric', module: 'robolectric'
        }
}

And in custom Application class

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}
Vivart
  • 14,900
  • 6
  • 36
  • 74

1 Answers1

-1

Trying using these few lines in your application class which would be

extend android.app.Application

and this will definately work :

Because of low memory , android created two dex files, so

   attachBaseContext() 

mangage this issue

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

and in gradle of your application

    defaultConfig {
    applicationId "com.example.YourApplication"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"

    multiDexEnabled true

    }

   dexOptions {
    javaMaxHeapSize "4g"
    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Vishal Thareja
  • 128
  • 1
  • 6
  • Multidex is enabled but using attachBaseContext() function will solve this issue – Vishal Thareja Jun 14 '16 at 05:01
  • But how do you know that hasn't already been done? I'm just saying, it seems like you're guessing and have provided the steps necessary to enable multidex even though the question said that was already done – OneCricketeer Jun 14 '16 at 05:11
  • I had seen this issue many time in many of my projects and of my colleagues , they had already enabled their multidexEnabled true in their gradle, but by adding attachBaseContext() function in Application class fixed their issue .As Android creates more than one dex files due to which classnotfound excption come – Vishal Thareja Jun 14 '16 at 05:36
  • Sure, but that is a necessary step to enable Multidex anyway. Just adding that one line to Gradle only means that Gradle will *support* multidex building. – OneCricketeer Jun 14 '16 at 05:39
  • Besides, extending the Application class isn't completely necessary if `android:name="android.support.multidex.MultiDexApplication"` is in the manifest – OneCricketeer Jun 14 '16 at 05:40
  • we already have attachBaseContext() in our custom Application class – Vivart Jun 14 '16 at 06:22