问题描述
我可以在调试和发布变体中成功构建和运行我的 Android 应用程序,没有任何问题.然而,当我尝试运行我的新单元测试时(我以前从未有过它们),我遇到了可怕的 DexIndexOverflowException.我怀疑 ProGuard
没有与我的单元测试一起运行,但它与我的正常调试和发布 buildTypes 一起运行.
我需要做什么才能在我的单元测试运行配置中运行 ProGuard
?我搜索了 Gradle 文档、ProGuard
文档和 Android Studio 文档来解决这个问题,但我一无所获.
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
Android 应用程序 (APK) 文件包含可执行的字节码文件Dalvik Executable (DEX) 文件的形式,其中包含已编译的用于运行您的应用程序的代码.Dalvik Executable 规范限制单个 DEX 中可以引用的方法总数文件到 65,536,包括 Android 框架方法、库方法、和方法在您自己的代码中.超过这个限制需要您将应用程序构建过程配置为生成多个 DEX文件,称为 multidex 配置.
Android SDK Build Tools 21.1 及更高版本中提供的适用于 Gradle 的 Android 插件支持 multidex 作为构建配置的一部分.在尝试为 multidex 配置您的应用之前,请确保您使用 SDK 管理器将 Android SDK 构建工具工具和 Android 支持库更新到最新版本.
设置您的应用开发项目以使用 multidex 配置需要您对应用开发项目进行一些修改.具体来说,您需要执行以下步骤:
- 更改您的 Gradle 构建配置以启用 multidex
- 修改您的清单以引用 MultiDexApplication 类
修改您的应用 Gradle 构建文件配置以包含支持库并启用多索引输出.
android {compileSdkVersion 25构建工具版本25.0.2"默认配置{...minSdk 版本 14目标SDK版本25...//启用多索引支持.multiDexEnabled true}...}依赖{编译'com.android.support:multidex:1.0.3'}
在您的清单中,将 multidex 支持库中的 MultiDexApplication
类添加到应用程序元素中.
</应用程序></清单>
阅读官方文档关于MultiDex
如果您的 Application 类正在扩展某个其他类并且您不想或无法更改它,请覆盖 attachBaseContext()
,如下所示:>
public class MyApplication extends MultiDexApplication {@覆盖protected void attachBaseContext(Context base) {super.attachBaseContext(base);MultiDex.install(this);}}
然后
</应用程序></清单>
结论
虽然该库在大多数情况下修复了 DEX 64K 问题,但它应该被当作最后的手段.在尝试使用它之前,您应该审核您的项目是否有不需要的依赖项并删除尽可能多的未使用的尽可能使用 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
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:
- Change your Gradle build configuration to enable multidex
- 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 25
buildToolsVersion "25.0.2"
defaultConfig {
...
minSdkVersion 14
targetSdkVersion 25
...
// Enabling multidex support.
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.3'
}
In your manifest add the MultiDexApplication
class from the multidex support library to the application element.
<?
Read Official Document aboutMultiDex
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);
}
}
Then
<?
Conclusion
Conclusion
这篇关于仅在运行测试时出现 DexIndexOverflowException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!