美文网首页
Error: Cannot fit requested clas

Error: Cannot fit requested clas

作者: Android卡卡西 | 来源:发表于2020-12-29 11:56 被阅读0次

使用studio报如下错误:

Error: Cannot fit requested classes in a single dex file (# methods: 149346 > 65536

问题原因:

该项目中定义的方法太多了,一个dex已经装不下了,需要个多个dex,也就是multidex ,因为Android系统定义总方法数是一个short int,short int 最大值为65536

解决方法

在 app 的 build.gradle 文件中

android {
    defaultConfig {
        // 这里添加
        multiDexEnabled true
    }
}
dependencies {
    // 引入multidex库
    implementation 'com.android.support:multidex:1.0.3'
}

在自定义的 application 中初始化 MultiDex

public class App extends Application{
    
    private static App mApp;
    
    @Override
    public void onCreate() {
        super.onCreate();
        mApp = this;
        // 初始化MultiDex
        MultiDex.install(this);
    }

    public static App getApp() {
        return mApp;
    }
}

相关文章

网友评论

      本文标题:Error: Cannot fit requested clas

      本文链接:https://www.haomeiwen.com/subject/gjefoktx.html