解决Android中单个dex文件中方法数越界的问题
来源:互联网 发布:大连电视台直播软件 编辑:程序博客网 时间:2024/05/20 19:28
通常一个Android应用中单个dex文件所能包含的最大方法数为65536个,这包含FrameWork、依赖的第三方jar包以及应用本身代码中的所有方法。
一般来说一个简单的应用里面的方法很难达到65536这个值,但是对于一些比较大型的项目来说,65536也是比较容易达到的,一旦应用的方法数达到65536后,编译器就无法正常完成编译工作了并会抛出类似这样的异常:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexIndexOverflowException:method ID not in [0, 0xffff]:65536
还有一种情况是,方法数没有达到65536,编译也正常编译通过了,但是在低版本的手机上安装时却出现了异常终止:
E/dalvikvm : Optimization failed
E/installd : dexopt failed on '/data/dalvik-cache/data@app@com.xxx.xxx.multidextest-2.apk@classes.dex' res=63282
...
这是因为应用在安装时会通过一个dexopt的程序来优化dex文件,并且在优化过程中dexopt通常会给应用一个8M或16M的缓存区,但是在Android2.2和2.3中通常只有5MB,所以会出现尽管应用中的方法没有超过65536个,但是其存储空间可能有超过5MB,这时候就会出现这样的问题,导致安装失败。
所以如何解决这样的问题呢?
在Android5.0以前需要引入Google提供的android-support-multidex.jar,这个jar包可以在SDK目录下的extras/android/support/multidex/library/libs下面找到。
5.0以后:(AndroidStudio)
一、修改工程目录下的build.gradle文件:
defaultConfig { applicationId "com.xxx.xxx" minSdkVersion 14 targetSdkVersion 23 versionCode 1 versionName "1.0" multiDexEnabled true//解决方法数量超过65536个时无法编译的问题}
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:multidex:1.0.0'}二、在代码中添加支持multidex的功能(三种方法)
1、在manifest文件中指定Application为MultiDexApplication,如:
<application android:name="android.support.multidex.MultiDexApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"2、让应用的Application继承MultiDexApplication,如:
public class App extends MultiDexApplication {......}3、重写Application的attachBaseContext方法(该方法比onCreate要先执行):
@Overrideprotected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this);}
通过以上修改,则可以顺利编译通过了。
- 解决Android中单个dex文件中方法数越界的问题
- 解决android中单个dex方法数超过65535的方法
- 解决单个DEX文件方法数超过65536的解决方案
- 解决Android单个dex文件不能超过65536个方法问题
- 解决Android单个dex文件不能超过65536个方法问题
- 解决Android单个dex文件不能超过65536个方法问题
- 解决Android单个dex文件不能超过65536个方法问题
- 解决Android单个dex文件不能超过65535个方法问题
- 解决android打包dex文件方法数溢出的问题
- 安卓解决65535dex文件方法数越界,IllegalArgumentException问题
- 解决Android单个dex文件不能超过65536个方法问题,DexIndexOverflowException
- Android 解决Dex超出方法数的限制问题
- Android解决Dex超出方法数限制的问题
- Android 使用 Multidex 解决 65536 方法数越界问题
- android-support-multidex解决Dex 文件方法数超过65536
- Android Studio解决65536Dex超出方法数问题
- Android 使用android-support-multidex解决Dex超出方法数的限制问题
- Android 使用android-support-multidex解决Dex超出方法数的限制问题
- MAVEN项目标准目录结构
- FolderSizes v8.0.91
- 【AC自动机】hdu2222 hdu2896 hdu3065 zoj3430 poj2778 hdu2243
- JDBC的学习--尚硅谷
- Unity3D资源相关API介绍
- 解决Android中单个dex文件中方法数越界的问题
- Leetcode: Maximum Product of Word Lengths
- 各种利益导致关注
- 028_CoreAPI_对象三种状态
- 零基础免费搭建个人博客-hexo+github
- 2016-1-7
- 支付系统返回值,错误码,code
- 跟我学习dubbo-简介
- MongoDB数据逻辑结构