简单处理Android 65536方法越界问题

看到这篇文章相信大家都是遇到了这个问题的人,对,Android著名的方法65536越界问题,是什么原因造成的呢?

这是因为在Android的单个dex文件中所能包含的最大方法数为65536,这个包含了Android FrameWork 、依赖jar包积极应用本身的代码中的所有方法;当我们的方法数总量达到了65536的话,编译器就会抛出类似下面的异常:

Information:Gradle tasks [:app:assembleDebug]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2421Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72421Library UP-TO-DATE
:app:prepareComAndroidSupportMultidex100Library
:app:prepareComAndroidSupportSupportCompat2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCoreUi2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCoreUtils2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportFragment2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportMediaCompat2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2421Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:compileDebugJavaWithJavac
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources
:app:prePackageMarkerForDebug
:app:transformClassesWithDexForDebug
**Error:The number of method references in a .dex file cannot exceed 64K.**
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_92\bin\java.exe'' finished with non-zero exit value 2
Information:BUILD FAILED
Information:Total time: 18.62 secs
Information:2 errors
Information:0 warnings
Information:See complete output in console
```这里可能根据你的Android Studio版本不一样抛出的异常有点不一样,上面黑色粗体字的大体的意思就是“这个dex 文件的方法数量引用不能超过了64k”。

  解决方法如下:
 - 删除无用的代码和第三方库
 - 对于这个问题很多应用采用了插件来动态加载部分dex,通过将dex拆分为多个dex,这样可以解决方法越界的问题,但是插件化是一种重量级的技术方案,兼容问题较多,所以这里不用插件化解决
 - 2014年Google提出了multidex的解决方案,也就是本文要实现的一种方案,使用起来更简单!

 - 在我们的IDE Android Studio中的module的gradle中配置如下图所示





<div class="se-preview-section-delimiter"></div>

这里写代码片
“`

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    aaptOptions.cruncherEnabled = false
    aaptOptions.useNewCruncher = false
    defaultConfig {
        applicationId "com.sinodata.method"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:multidex:1.0.0'
}

其中将
defaultConfig {
applicationId “com.sinodata.method”
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName “1.0”
multiDexEnabled true
}
multiDexEnable true 是关键
dependencies {
compile fileTree(dir: ‘libs’, include: [‘*.jar’])
testCompile ‘junit:junit:4.12’
compile ‘com.android.support:appcompat-v7:24.2.1’
compile ‘com.android.support:multidex:1.0.0’
}
compile ‘com.android.support:multidex:1.0.0’使用这个关键库

  • 最后在代码中加入multidex的功能,有三种方式可以选择
  • 1.在manifest中指定Application为MultiDexApplication
  • 这里写图片描述
    2.让自己的应用继承MultiDexApplication
    3.不想让自己的应用继承MultiDexApplication,还可以选择重写Application的attachBaseContext方法,它比Application的onCreate()要先执行,如下所示 @Override
    protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
    }

  • 在这里说明下,因为打包成了多个dex,启动的时候相对正常情况会比较慢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值