Android 更新到最新appcompat和支持库后出现DexIndexOverflowException问题

Android 更新到最新appcompat和支持库后出现DexIndexOverflowException问题,android,gradle,android-5.0-lollipop,android-support-library,build.gradle,Android,Gradle,Android 5.0 Lollipop,Android Support Library,Build.gradle,我正在通过gradle使用以下设置: compileSdkVersion 21 ANDROID_BUILD_MIN_SDK_VERSION=14 ANDROID_BUILD_TARGET_SDK_VERSION=21 ANDROID_BUILD_TOOLS_VERSION=21.0.2 ANDROID_BUILD_SDK_VERSION=21 android { defaultConfig { ... // Enabling multidex sup

我正在通过
gradle
使用以下设置:

compileSdkVersion 21
ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.0.2
ANDROID_BUILD_SDK_VERSION=21
android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.config
    }
    debug {
        signingConfig signingConfigs.config
    }
}
我的gradle文件中还有以下设置:

compile 'com.android.support:support-annotations:21.0.0'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:support-v4:21.0.0'
compile 'com.google.android.gms:play-services:11.0.2'
我总是收到错误
意外的顶级异常

但是当我将
21.0.0
设置为
20.0.0
时,它工作正常……但是我无法访问任何Android API 21选项。我有什么地方做错了吗?如何在没有此异常的情况下编译它?除了其他年级项目(facebook等),我没有其他地方的支持JAR

以下是完整的堆栈跟踪:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
    at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:502)
    at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:283)
    at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:491)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:168)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:302)
    at com.android.dx.command.dexer.Main.run(Main.java:245)
    at com.android.dx.command.dexer.Main.main(Main.java:214)
    at com.android.dx.command.Main.main(Main.java:106)

此消息听起来好像您的项目太大了

你的方法太多了。dex只能有65536种方法

由于gradle插件0.14.0和构建工具21.1.0,您可以使用

只需将这些行添加到
build.gradle

compileSdkVersion 21
ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.0.2
ANDROID_BUILD_SDK_VERSION=21
android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.config
    }
    debug {
        signingConfig signingConfigs.config
    }
}
另外,在
清单
中,将多索引支持库中的
多索引应用程序
类添加到应用程序元素中

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

...

如果您正在使用自己的
应用程序
类,请将父类从
应用程序
更改为
多索引应用程序
此消息听起来好像您的项目太大了

你的方法太多了。dex只能有65536种方法

由于gradle插件0.14.0和构建工具21.1.0,您可以使用

只需将这些行添加到
build.gradle

compileSdkVersion 21
ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.0.2
ANDROID_BUILD_SDK_VERSION=21
android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.config
    }
    debug {
        signingConfig signingConfigs.config
    }
}
另外,在
清单
中,将多索引支持库中的
多索引应用程序
类添加到应用程序元素中

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

...
如果您使用的是自己的
应用程序
类,请将父类从
应用程序
更改为
多索引应用程序

请遵循解决65K限制的指南

我在gradle中通过设置
multiDexEnabled true
并使用
类MyApp extends MultiDexApplication扩展我的应用程序来解决这个问题
请遵循解决65K限制的指南

我在gradle中解决了这个问题,方法是设置
multiDexEnabled true
并使用
类MyApp extends MultiDexApplication扩展我的应用程序

compile 'com.android.support:multidex:1.0.2'
2) 如果不重写应用程序类,请编辑清单文件以在标记中设置android:name,如下所示:

<?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>
public class MyApplication extends MultiDexApplication { ... }
或者,如果确实重写了应用程序类,但无法更改基类,则可以重写attachBaseContext()方法并调用MultiDex.install(此)以启用MultiDex:

public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}
3) 在build.gradle中将多索引支持添加到defaultConfig

defaultConfig {
        ...
        minSdkVersion 16
        targetSdkVersion 26
        ...
        // Enabling multidex support.
        multiDexEnabled true
    }
...
1) 将依赖项添加到application build.gradle文件

compile 'com.android.support:multidex:1.0.2'
2) 如果不重写应用程序类,请编辑清单文件以在标记中设置android:name,如下所示:

<?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>
public class MyApplication extends MultiDexApplication { ... }
或者,如果确实重写了应用程序类,但无法更改基类,则可以重写attachBaseContext()方法并调用MultiDex.install(此)以启用MultiDex:

public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}
3) 在build.gradle中将多索引支持添加到defaultConfig

defaultConfig {
        ...
        minSdkVersion 16
        targetSdkVersion 26
        ...
        // Enabling multidex support.
        multiDexEnabled true
    }
...
在搞乱MultiDex之前 要么解决方案太大(使用MultiDex),要么编译并在项目中包含一些不需要的东西。我在我的小项目中得到了这个问题。以下是我所做的修复:

我从
build.gradle
中删除了这个:

compileSdkVersion 21
ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.0.2
ANDROID_BUILD_SDK_VERSION=21
android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.config
    }
    debug {
        signingConfig signingConfigs.config
    }
}
我还手动从应用程序根目录中删除了构建文件夹。问题已解决。

在使用MultiDex之前 要么解决方案太大(使用MultiDex),要么编译并在项目中包含一些不需要的东西。我在我的小项目中得到了这个问题。以下是我所做的修复:

我从
build.gradle
中删除了这个:

compileSdkVersion 21
ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.0.2
ANDROID_BUILD_SDK_VERSION=21
android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.config
    }
    debug {
        signingConfig signingConfigs.config
    }
}

我还手动从应用程序根目录中删除了构建文件夹。问题已修复。

我只是注释掉了compile
文件树(包括:['*.jar'],dir:'libs')

build.gradle
文件中(如上所述),重新生成项目。编译时没有任何错误。我不需要移动任何文件夹。

我只是注释掉了compile
文件树(包括:['*.jar'],dir:'libs')

build.gradle
文件中(如上所述),重新生成项目。编译时没有任何错误。我不需要移动任何文件夹。

首先确保在
app/build.bradle中启用了缩小:

compileSdkVersion 21
ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.0.2
ANDROID_BUILD_SDK_VERSION=21
android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.config
    }
    debug {
        signingConfig signingConfigs.config
    }
}

如果这不能解决问题,请为您的应用程序启用。

首先确保在
app/build.bradle中启用了缩小:

compileSdkVersion 21
ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.0.2
ANDROID_BUILD_SDK_VERSION=21
android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.config
    }
    debug {
        signingConfig signingConfigs.config
    }
}

如果这不能解决问题,请为您的应用程序启用。

当我将整个Google Play Services API包编译到我的应用程序中时,我遇到了这个问题。我通过使用选择性API解决了这个问题

例如,要仅包括Google Fit和Android Wear API,请在build.gradle文件中替换以下行:

compile 'com.android.support:support-annotations:21.0.0'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:support-v4:21.0.0'
compile 'com.google.android.gms:play-services:11.0.2'
以下几行:

compile 'com.google.android.gms:play-services-fitness:11.0.2'
compile 'com.google.android.gms:play-services-wearable:11.0.2'

下面将解释这个问题

当我将整个Google Play Services API包编译到我的应用程序中时,我遇到了这个问题。我通过使用选择性API解决了这个问题

例如,要仅包括Google Fit和Android Wear API,请在build.gradle文件中替换以下行:

compile 'com.android.support:support-annotations:21.0.0'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:support-v4:21.0.0'
compile 'com.google.android.gms:play-services:11.0.2'
以下几行:

compile 'com.google.android.gms:play-services-fitness:11.0.2'
compile 'com.google.android.gms:play-services-wearable:11.0.2'

下面将解释这个问题

您是否从SDK管理器中加载了所有库和工具?是的,我只是将我的应用程序从API 19更新到21。以前版本的所有库和21的所有新库都是重复的。您是否从SDK管理器中加载了所有库和工具?是的,我只是将我的应用程序从API 19更新到21。以前版本的所有库和21的所有新库。重复的
multi-dex
是原因。将multi-dexenabled true添加到构建的默认配置中。gradle文件我不使用build-gradle fil