Android方法数methods超过65536

当Android App中的方法数超过65535时,如果往下兼容到低版本设备时,就会报编译错误:

Cannot fit requested classes in a single dex file. Try supplying a main-dex list.
# methods: 86204 > 65536
Message{kind=ERROR, text=Cannot fit requested classes in a single dex file. Try supplying a main-dex list.
# methods: 86204 > 65536, sources=[Unknown source file], tool name=Optional.of(D8)}

原因是Android系统定义总方法数是一个short int,short int 最大值为65536。解决这个问题的方案是:

在Android的模块gradle文件的defaultConfig默认配置里面增加:

multiDexEnabled true

同时在dependencies里面增加:

implementation 'com.android.support:multidex:1.0.3'

另外需要把AndroidMainfest.xml里面添加自定义的App继承自MultiDexApplication。

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/80306301