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說拜拜了

相關文章
相關標籤/搜索