1. 程式人生 > >Android工程方法數超過64k,The number of method references in a .dex file cannot exceed 64K.

Android工程方法數超過64k,The number of method references in a .dex file cannot exceed 64K.

一個 ref multidex context pre method ips .html conf

最近將一個老的Eclipse項目轉到Android Studio後,用gradle添加了幾個依賴,項目可以make,但是一旦run就報錯

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

原因

項目的方法數超過了64k,需要進行分包處理!

Android Studio解決方案:

至於Eclipse的解決方法,似乎需要采用相應插件!

第一步,添加依賴
implementation 'com.android.support:multidex:1.0.1'`
第二步,添加配置,build.gradle(app)
  1. buildTypes
 dexOptions {
            preDexLibraries false
        }

這裏是需要添加到buildTypes中,註意如果app依賴其他的module,那麽在相應的moudle(build.gradle)中也需要添加!

  1. 在defaultConfig下添加
multiDexEnabled true
第三步,創建MultiDexApplication
  1. 如果你有自定義的Application
    自定義Application 繼承 android.support.multidex.MultiDexApplication;,然後重寫其attachBaseContext方法
/**
     * 方法超過64K,需要采用分包
     * fjj 2019-3-27
     * @param base
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this); // 初始化
    }
  1. 如果你自己的項目沒有自定義Application,也不需要自定義時,可直接在AndroidManifest.xmlapplication下指定:
android:name="android.support.multidex.MultiDexApplication;"

最後

此致,敬禮!

Android工程方法數超過64k,The number of method references in a .dex file cannot exceed 64K.