DexIndexOverflowException只有当运行测试 [英] DexIndexOverflowException Only When Running Tests

查看:114
本文介绍了DexIndexOverflowException只有当运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以成功地构建并运行在我调试我的Andr​​oid应用程序,没有任何问题发布变种。然而,当我尝试运行我新的单元测试(我从来没有之前),我得到了可怕的DexIndexOverflowException。我怀疑ProGuard的不被运行我的单元测试,但它与我的正常调试和发布buildTypes。

我需要做什么做在我的单元测试运行配置运行ProGuard的?我在摇篮文档,ProGuard文档,和Android Studio文档搜索摸不着头脑但我什么也没找到。

解决方案

  com.android.dex.DexIndexOverflowException:不是方法ID [0,0xFFFF的]:65536
 

  

Android应用(APK)文件包含在可执行字节code文件   的的Dalvik可执行(DEX)的文件的形式,其含有已编译   code用于运行应用程序。在Dalvik可执行的规范限制   的方法的总数,可以在一个单一的DEX被引用   文件65536,包括Android框架方法,库方法,   并在自己的code的方法。获得过此限制要求,   您配置的应用程序的构建过程中生成多个DEX   文件,被称为multidex配置。

在Android插件的摇篮在Android SDK中提供构建工具21.1及更高版本支持multidex作为构建配置的一部分。请确保您更新使用SDK管理器尝试配置您的应用程序为multidex之前Android SDK的构建工具的工具和Android的支持库到最新版本。

设置你的应用程序开发项目使用multidex配置要求你做一些修改,您的应用程序开发项目。特别需要执行以下步骤:

  1. 更改您的摇篮构建配置,使multidex
  2. 修改你的清单来引用MultiDexApplication类

修改你的应用程序建立摇篮文件配置,包括支持库,使multidex输出。

 安卓{
    compileSdkVersion 21
    buildToolsVersion21.1.0

    defaultConfig {
        ...
        的minSdkVersion 14
        targetSdkVersion 21
        ...

        //启用multidex支持。
        multiDexEnabled真
    }
    ...
}

依赖{
  编译com.android.support:multidex:1.0.0
}
 

阅读有关正式文件 MultiDex

如果您的应用程序类扩展其他类你不想或不能改变它, 覆盖attachBaseContext() 如下图所示:

 公共类MyApplication的扩展MultiDexApplication {
   @覆盖
   保护无效attachBaseContext(上下文基地){
      super.attachBaseContext(基峰);
      MultiDex.install(本);
   }
}
 

结论

  

虽然库固定在大多数情况下对DEX 64K问题,它应该   被视为最后的手段。在尝试使用它,你应该   审计项目不需要的依赖,并删除尽可能多的未使用   code为使用ProGuard的可能。

I can successfully build and run my Android app in my debug and release variants with no problem. Yet, when I try to run my new unit tests (I never had them before), I get the dreaded DexIndexOverflowException. I suspect ProGuard is not being run with my unit tests, but it is with my normal debug and release buildTypes.

What do I need to do to run ProGuard in my unit test run configuration? I searched through the Gradle documentation, the ProGuard documentation, and the Android Studio documentation to figure this out yet I found nothing.

解决方案

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

Android application (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.

The Android plugin for Gradle available in Android SDK Build Tools 21.1 and higher supports multidex as part of your build configuration. Make sure you update the Android SDK Build Tools tools and the Android Support Repository to the latest version using the SDK Manager before attempting to configure your app for multidex.

Setting up your app development project to use a multidex configuration requires that you make a few modifications to your app development project. In particular you need to perform the following steps:

  1. Change your Gradle build configuration to enable multidex
  2. Modify your manifest to reference the MultiDexApplication class

Modify your app Gradle build file configuration to include the support library and enable multidex output .

    android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21
        ...

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

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

Read Official Document about MultiDex

If your Application class is extending some other class and you don’t want to or can’t change it, override attachBaseContext() as shown below:

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

Conclusion

While the library fixes the DEX 64K problem in most cases, it should be treated as a last resort. Before attempting to use it, you should audit your project for unwanted dependencies and remove as much unused code as possible using ProGuard.

这篇关于DexIndexOverflowException只有当运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆