Android开发 Error(建议收藏下来以备不时之需):The number of method references in a .dex file cannot exceed 64K.

Android开发 Error(建议收藏下来以备不时之需):The number of method references in a .dex file cannot exceed 64K.
前言android

我一直都知道app里面的方法数是有限制的差很少64000,具体的就不曾考证了
在遇到这个问题以前,一直觉得这个一个多么遥远的距离
其实并非的,稍有不慎这个异常出来了
当前并非你真的有编写了64k的方法数量了
大部分都是由于包的重复导入,当前就算是真的超过64k的方法,本文也将提出解决方案
当出现这个状况别慌,咱们一步一步来app

去除重复包框架

咱们项目中经常都会用到几个LIbrary,然而LIbrary里面的build.gradle和咱们app的build.gradle引用了相同类型不一样版本的包,来张图给你们看看,方便理解ide


项目包重复
其中的V4包和annotations包引用了两个不一样的版本,增长方法数量的同时也增长了apk包的大小
通常出现The number of method references in a .dex file cannot exceed 64K.先看External Libraries里面是否有大量的重复包,若是有把版本都设置成一致的基本能解决这个异常
若是还有是有的话,多是项目自己就比较大,也大量的使用了第三方框架等等gradle

分割 Dex 文件解决方法限制ui

首先app的 build.gradle 中
(1)在dependencies 中添加this

compile 'com.android.support:multidex:1.0.1'
(2)在 defaultConfig 中添加.net

multiDexEnabled true
好比xml

android { 
   compileSdkVersion 23  
   buildToolsVersion '23.0.2'   继承

   defaultConfig { 
       applicationId "XXXXXX"  
       minSdkVersion 11 
       targetSdkVersion 23  
       versionCode 29  
       versionName "2.66"  
       multiDexEnabled true  
  }
buildType{
       release { 
            minifyEnabled false  
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  
        } 

dependencies { 
        compile fileTree(include: ['*.jar'], dir: 'libs') 
        .......
        com.android.support:multidex:1.0.1' 
}
(3)在 AndroidManifest.xml 中的 application 标签中添加

1<?xml version="1.0" encoding="utf-8"?>2
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="com.example.android.multidex.myapplication">
      <application
      ...
       android:name="android.support.multidex.MultiDexApplication">
        ...
     </application>
 </manifest>
若是你的应用程序继承 Application , 那么你须要重写Application attachBaseContext方法

@Override  protected void attachBaseContext(Context base) {        super.attachBaseContext(base);          MultiDex.install(this) ; } 这样的话就能够和64K说拜拜了

相关文章
相关标签/搜索