1

I have used

compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 23
    ...
    multiDexEnabled true
}

dependencies {
    compile 'com.android.support:multidex:1.0.1'
    ...
}

in the app-level build.gradle and

<manifest package="com.example"
...
    <application
        android:name=".AppContext"
        ...
    >
>

in the manifest for the long time because of my app definitely references more than 64K methods. Now I have changed it to fresh SDK and multidex lib under Android Studio 3.3.2

compileSdkVersion 27
buildToolsVersion "27.0.3"


defaultConfig {
    minSdkVersion 14
    targetSdkVersion 27
    ...
    multiDexEnabled true
}

dependencies {
    compile 'com.android.support:multidex:1.0.3'
    ...
}

and app have began to crash when installing on all available Android 4.X devices. Most crashes has stacktrace like below:

java.lang.RuntimeException:
at android.app.ActivityThread.installProvider (ActivityThread.java:5236)
at android.app.ActivityThread.installContentProviders (ActivityThread.java:4828)
at android.app.ActivityThread.handleBindApplication (ActivityThread.java:4711)
at android.app.ActivityThread.access$1600 (ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1368)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:146)
at android.app.ActivityThread.main (ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative (Native Method)
at java.lang.reflect.Method.invoke (Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1099)
at dalvik.system.NativeStart.main (Native Method)
Caused by: java.lang.ClassNotFoundException:
at dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass (ClassLoader.java:497)
at java.lang.ClassLoader.loadClass (ClassLoader.java:457)
at android.app.ActivityThread.installProvider (ActivityThread.java:5221)

The only way I have found to solve the problem is to exclude

//    compile 'com.android.support:multidex:1.0.3'

from app-level build.gradle and changing

public class AppContext extends Application {
...}

to

public class AppContext extends MultiDexApplication {
...}

Is it a bug in build tools 27.x.x or what!?

isabsent
  • 3,683
  • 3
  • 25
  • 46
  • 1
    Possible duplicate of [java.lang.ClassNotFoundException in dalvik.system.BaseDexClassLoader.findClass](https://stackoverflow.com/questions/41426853/java-lang-classnotfoundexception-in-dalvik-system-basedexclassloader-findclass) – Jon Goodwin Mar 18 '19 at 02:32
  • May be you are right, but the solution is absent there and it seems they are using `multidex` when the app uses less than 64K methods... – isabsent Mar 18 '19 at 02:49
  • The bug [is known](https://issuetracker.google.com/issues/118342203) – isabsent Mar 18 '19 at 11:57

2 Answers2

1

You can try this way,

1) Remove

multiDexEnabled true

from app gradle and only add

implementation 'com.android.support:multidex:1.0.3'

to your app dependencies

Like this

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.heizolscout.itclanbd.heizolscout"
    minSdkVersion 18
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard- 
rules.pro'
    }
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-v4:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Muhaiminur Rahman
  • 3,066
  • 20
  • 27
  • 1
    As a result: `org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'` `Caused by: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives` – isabsent Mar 18 '19 at 10:19
  • from android studio menu try invalidate caches and restart. @isabsent – Muhaiminur Rahman Mar 18 '19 at 13:06
1

I have the same problem in one of my apps. Using app extending MultiDexApplication like this:

public class AppContext extends MultiDexApplication {

  ...

}

and using multidex dependencies like this:

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

didn't work.

I solve it by using MultiDex.install in Application class. Something like this:

import android.support.multidex.MultiDex;

public class AppContext extends Application {

   ...

   @Override
   protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
   }
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • What was a `minSdkVersion` of your app and what was an API of crashing devices? Did you use `extends MultiDexApplication` and `com.android.support:multidex:1.0.3` simultaneously or you try one way after another? – isabsent Mar 18 '19 at 10:07
  • I'm already trying all the possibilities. First extending MultiDexApplication. Second, using `android:name="android.support.multidex.MultiDexApplication"` in AndroidManifest.xml. And using MultiDex.install(). `Only MultiDex.install()` is works for our test devices (Samsung s3, s4, Xiaomi, Vivo, Huawei, Lava, etc). My app is using android 14 as `minSdkVersion` – ישו אוהב אותך Mar 18 '19 at 10:16
  • Did all of your crashing devices have API < 21? – isabsent Mar 18 '19 at 10:24
  • Yes, all devices below 21 is crash. Especially devices with Android version 4.4. You need to try it by yourself to confirm it. – ישו אוהב אותך Mar 18 '19 at 10:27
  • I already have a lot of confirmations from our users in google developer console :) I have write [bug report](https://issuetracker.google.com/issues/128782290) to Google. Please star it. – isabsent Mar 18 '19 at 10:33