【问题标题】:Multidex problem on API 14 - API 19 (Android 4.X) with targetSdkVersion 27API 14 上的 Multidex 问题 - API 19 (Android 4.X) 与 targetSdkVersion 27
【发布时间】:2019-08-08 09:14:40
【问题描述】:

我用过

compileSdkVersion 23
buildToolsVersion "23.0.3"

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

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

在app级build.gradle和

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

在manifest 中很长时间了,因为我的应用程序肯定引用了超过 64K 的方法。现在我已将其更改为新的 SDK 和 Android Studio 3.3.2

下的 multidex lib
compileSdkVersion 27
buildToolsVersion "27.0.3"


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

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

在所有可用的 Android 4.X 设备上安装时,应用程序已开始崩溃。大多数崩溃都有如下堆栈跟踪:

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)

我发现解决问题的唯一方法是排除

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

来自app-level build.gradle 和变化

public class AppContext extends Application {
...}

到

public class AppContext extends MultiDexApplication {
...}

这是build tools 27.x.x 中的错误还是什么!?

【问题讨论】:

标签: android android-4.4-kitkat android-multidex android-4.0.3-ice-cream-sandwich


【解决方案1】:

你可以试试这个方法,

1) 删除

multiDexEnabled 真

来自应用程序 gradle 并且只添加

实现'com.android.support:multidex:1.0.3'

到您的应用依赖项

这样

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'
}

【讨论】:

  • 结果: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
  • 从 android studio 菜单尝试使缓存无效并重新启动。 @isabsent
【解决方案2】:

我在我的一个应用程序中遇到了同样的问题。像这样使用扩展MultiDexApplication 的应用程序:

public class AppContext extends MultiDexApplication {

  ...

}

并像这样使用 multidex 依赖项:

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

没用。

我通过在 Application 类中使用 MultiDex.install 来解决它。像这样的:

import android.support.multidex.MultiDex;

public class AppContext extends Application {

   ...

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

【讨论】:

  • 您的应用程序的minSdkVersion 是什么,崩溃设备的 API 是什么?您是同时使用extends MultiDexApplication 和com.android.support:multidex:1.0.3 还是一种又一种尝试?
  • 我已经尝试了所有的可能性。首先扩展 MultiDexApplication。二、在AndroidManifest.xml中使用android:name="android.support.multidex.MultiDexApplication"。并使用 MultiDex.install()。 Only MultiDex.install() 适用于我们的测试设备(三星 s3、s4、小米、Vivo、华为、Lava 等)。我的应用使用 android 14 作为minSdkVersion
  • 您所有的崩溃设备是否都有 API
  • 是的,所有低于 21 的设备都会崩溃。尤其是 Android 版本 4.4 的设备。您需要自己尝试确认。
  • 我已经在谷歌开发者控制台中得到了很多用户的确认 :) 我已经给谷歌写了bug report。请给它加星标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多