尝试将这些行添加到您的 build.gradle
android {
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 26
multiDexEnabled true
}
...
}
这将启用 multidex 模式,这将允许您超过 64k 限制。 (阅读更多here)
API 低于 21
如果您使用的 API 级别低于 21,那么您还需要添加支持库
gradle.build:
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
android.manafest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:name="android.support.multidex.MultiDexApplication" >
...
</application>
</manifest>
如果您使用自定义 Application 类,请尝试使用以下之一
解决方案 1
只需覆盖 MultiDexApplication 类
public class MyApplication extends MultiDexApplication { ... }
解决方案 2
覆盖 attachBaseContext 并使用 install(Application) 函数安装 MultiDex
public class MyApplication extends SomeOtherApplication {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}