【问题标题】:unexpected top-level exception:意外的顶级异常:
【发布时间】:2015-12-22 06:03:51
【问题描述】:

我尝试在我的 build.gradle 文件中添加 Google Play 商店服务,并且同步成功。现在,当我尝试运行该应用程序时,我在 Android Studio 中遇到了以下异常:

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:502)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
    at com.android.dx.command.dexer.Main.run(Main.java:277)
    at com.android.dx.command.dexer.Main.main(Main.java:245)
    at com.android.dx.command.Main.main(Main.java:106)
Error:Execution failed for task ':app:dexDebug'.

com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_60\bin\java.exe'' 完成非零退出值 2

在添加播放服务之前,它工作正常。我的 build.gradle 文件如下所示:

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    defaultConfig {
        applicationId 'com.xyz.app'
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 2
        versionName "1.0.1"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'me.neavo:volley:2014.12.+'
    compile 'com.google.code.gson:gson:2.2.+'
    compile 'com.facebook.android:facebook-android-sdk:4.7.0'
    compile 'com.google.android.gms:play-services:8.4.0'
    compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
        transitive = true;
    }
}

我在谷歌上搜索了issue,发现问题状态超过了dex文件中65536个方法的限制

com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

但我的项目还不够大。

请告诉我我做错了什么并提出解决方法。 谢谢。

【问题讨论】:

  • 使用的是哪个播放服务模块?
  • @Veer3383 我正在使用位置 api 通过 GoogleApiClient 获取当前位置。
  • @Shadow Droid 尝试了您的解决方案,但没有帮助
  • 那么你只需要使用这个编译'com.google.android.gms:play-services-location:8.4.0' 你不需要启用multidex,试试看。

标签: android exception indexing overflow toplevel


【解决方案1】:

您超出了 65K 的最大方法计数限制。 一种解决方案是在 gradle 中使用 multidex 选项。

如下修改你的 build.gradle

android {
  ...

  defaultConfig {
    ...
    minSdkVersion 14
    targetSdkVersion 23
    ...

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

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

Reference Link

其他解决方案是通过 proguard 优化您的方法,可以通过

启用
minifyEnabled true

部分。

此外,您还可以优化依赖项以避免超过 65K 方法计数限制。 查看此site,获取将插件直接安装到 Android Studio 中的说明,该说明将并排显示 build.gradle 中每个依赖项的方法计数。考虑到这一点,您可以决定要保留哪个依赖项,以及可以用不太需要方法的东西替换哪个依赖项。

【讨论】:

  • 这个解决方案对我有用。谢谢。但我仍然想知道我的应用程序是如何超过 65K 的最大方法计数限制的,有什么具体原因吗?
  • @GaganSingh 指的是我在答案底部添加的站点链接。可以选择将插件直接安装到 Android Studio 中。您可以使用它来查找哪个依赖项是罪魁祸首。
  • 我明白了。我认为 Google Play 服务有超过 65k 的方法,所以在使用 play 服务后,我得到了这个错误。谢谢你的帮助。 :)
  • 在这种情况下,您可能想开始使用来自 google play services 的分段模块。看看这里:developers.google.com/android/guides/setup
【解决方案2】:

不要包含整个播放服务依赖项,

替换

compile 'com.google.android.gms:play-services:8.4.0'

仅包含您正在使用的模块。

例如。

 compile 'com.google.android.gms:play-services-location:8.4.0'

请参考this

如果您需要包含播放服务的所有模块,

参考 Radix 的回答

【讨论】:

    【解决方案3】:

    这基本上是因为你超出了方法的限制。你有两个选项来解决这个错误。

    1) 减少项目中方法的数量。基本上,您在项目方法中添加的库也计入您的项目方法中。所以不要使用该库的整个包使用您在项目中使用的模块:例如,您添加整个包的 Google Play 服务添加您想要使用的模块,如地图等。

    2)第二个 Enable the multidex for enable multidex : 现在,您需要做的就是在您的 Application 类中重写它:

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

    在你的渐变中添加多索引支持库

     android {
    compileSdkVersion 22
    buildToolsVersion "23.0.0"
    
         defaultConfig {
             minSdkVersion 14 //lower than 14 doesn't support multidex
             targetSdkVersion 22
    
             // Enabling multidex support.
             multiDexEnabled true
         }
    }
    
    dependencies {
    compile 'com.android.support:multidex:1.0.1'
    }
    

    在您的清单中,将 MultiDexApplication 类从 multidex 支持库添加到应用程序元素,如下所示

    <?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>
    

    希望对你有所帮助....

    【讨论】:

      【解决方案4】:

      请参阅此link

      有时大型库依赖项也会导致符号大小增加。这个链接谈到了避免 65K 限制,通过 proguard 之类的方式。

      或者您也可以简单地使用多重索引,它会在达到较大的符号大小时为您创建 2 个 classes.dex 文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-07
        • 2015-08-29
        • 1970-01-01
        • 2014-11-04
        • 1970-01-01
        相关资源
        最近更新 更多