android - 我在 Android studio 3.0 Beta 5 中运行应用程序时遇到 Multidex 问题

标签 android android-multidex

我已点击此解决方案的各种链接,但没有任何效果。 以下是错误:

java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

我还启用了 multidex 并在 build.gradle 文件底部添加了 google services 插件。这是 build.gradle 编译器依赖项:

compile fileTree(include: '*.jar', dir: 'libs')
compile files('libs/universal-image-loader-1.9.5.jar')
compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
    transitive = true;
}

compile 'com.android.support:design:26.0.1'
compile 'com.android.support:mediarouter-v7:26.0.1'
compile 'com.android.support:appcompat-v7:26.0.1'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:recyclerview-v7:26.0.1'
compile 'com.android.support:support-v13:26.0.1'
compile 'com.daimajia.swipelayout:library:1.2.0@aar'
compile 'com.fasterxml.jackson.core:jackson-databind:2.5.+'
compile 'com.fasterxml.jackson.core:jackson-core:2.5.+'
compile('com.googlecode.json-simple:json-simple:1.1.1') {
    exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
compile 'com.google.firebase:firebase-messaging:11.4.0'
compile 'junit:junit:4.12'
compile 'com.nineoldandroids:library:2.4.0'
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:3.1.2'
testCompile 'org.robolectric:shadows-multidex:3.1.2'
testCompile 'org.robolectric:shadows-httpclient:3.1.2'
testCompile 'org.robolectric:shadows-support-v4:3.1.2'
compile files('libs/beacon_sdk.jar')
compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
compile('org.apache.httpcomponents:httpmime:4.3') {
    exclude module: "httpclient"
}

compile 'com.google.android.gms:play-services:11.4.0'
compile 'com.google.firebase:firebase-core:11.4.0'
compile 'org.apmem.tools:layouts:1.10@aar'
compile 'org.apache.commons:commons-lang3:3.5'
compile 'com.android.support:cardview-v7:26.0.1'

最佳答案

Android 5.0(API 级别 21)之前的平台版本使用 Dalvik 运行时来执行应用程序代码。默认情况下,Dalvik 将应用程序限制为每个 APK 的单个 classes.dex 字节码文件。为了绕过这个限制,您可以将 multidex 支持库添加到您的项目中:

添加如下依赖

  android {
    defaultConfig {
        ...
        minSdkVersion 15 
        targetSdkVersion 26
        multiDexEnabled true
    }
    ...
}

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



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
            android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>
</manifest>

如果您确实覆盖了 Application 类但无法更改基类,那么您可以覆盖 attachBaseContext() 方法并调用 MultiDex.install(this) 以启用 multidex:

public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}

关于android - 我在 Android studio 3.0 Beta 5 中运行应用程序时遇到 Multidex 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46424218/

相关文章:

java/android - 无法访问 SD 卡上的文件

android - 返回后的 kotlin android fragment 空回收器 View

android - 当min sdk为5.0以下时,启用multidex创建singledex,而当min sdk为5.0以上时,则启用multidex。

java - Android - MultiDex 安装

java - 如何解决Android MultiDex应用实例空指针异常?

android - 将套接字连接到设备上的本地地址

java - 所有 "com.android.camera. - - -" Intent 过滤器操作都是在哪里定义的?

Android:给所有的 View 都赋予id有什么好处吗?

android - 使用 MultiDexApplication 会导致 applicationContext 变为 null

java - Multidex 安装失败