# Android App 方法超出64K解决方案,即如何启动“多 dex 文件”

# 当您的应用及其引用的库超过 65,536 种方法时,您会遇到一个编译错误,指明您的应用已达到 Android 编译架构规定的引用限制:

# 一、报错如下:

D8: Cannot fit requested classes in a single dex file (# methods: xxxxx > 65536)

trouble writing output:
Too many field references: 131000; max is 65536.
You may try using --multi-dex option.
1
2
3
4
5

较低版本的编译系统会报告一个不同的错误,但指示的是同一问题:

Conversion to Dalvik format failed:
Unable to execute dex: method ID not in [0, 0xffff]: 65536
1
2

# 二、解决方法

# 1、在 android/app/build.gradle, 更新 dependency :

defaultConfig {
    // ... your `applicationId`, etc.
    multiDexEnabled true
}

// ...
dependencies {
    // ... your other dependencies

    // Multidex
    implementation 'com.android.support:multidex:1.0.3'
}
1
2
3
4
5
6
7
8
9
10
11
12

# 2、在 MainApplication.java

import android.app.Application;
1

替换

import android.support.multidex.MultiDexApplication;
1

如果 RN 0.60+ 或者支持 AndroidX :

import androidx.multidex.MultiDexApplication;
1

# 3、在 MainApplication.java

public class MainApplication extends Application implements ReactApplication {
1

替换

public class MainApplication extends MultiDexApplication implements ReactApplication {
1