Android MultiDex 是什么?
What is Android MultiDex?
关于 MultiDex 的帖子很多。有时,我遇到过解决错误,包括 build.gradle 的 defaultConfig
部分中的 multiDexEnabled true
。
但是,这个功能到底是什么?有哪些使用场景?
Android application (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.
因此,特点是:它允许您编译复杂的应用程序。使用它的场景是当您的应用程序由于达到 64K DEX 方法引用限制而无法编译时。这显示为构建错误,例如:
Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536
就这么简单
一个 .dex 文件可以有 65,536 个方法(引用),所以如果引用的数量超过 65,536,你就用 multidex。
更多说明!
一个 android 应用程序被编译成一个 .dex 文件,然后压缩成一个 .apk 文件。
DVM(Dalvik 虚拟机)使用 .dex file/files 来执行字节码。
是什么原因导致引用数超过65536个限制?
你写的方法 + Android 框架方法 + 第三方库(如 Volley,Retrofit, Facebook SDK 等)方法。
我读过“某处”
App Compat 24.2.1 包含 16.5k 个方法
Google Play 服务 GCM 9.6.1 包含 16.7k 个方法
因此,如果您创建了一个具有 App Compat 24.2.1 的简单 Hello world 应用程序,您已经 1/4 方式 跨越单个 dex 方法限制
访问 Android 开发者官方网站。
If your minSdkVersion is set to 21 or higher, multidex is enabled by default and you do not need the multidex support library.
当您的应用及其引用的库超过 65,536 个方法时,您会遇到构建错误,表明您的应用已达到 Android 构建架构的限制
如何在您的项目中启用 MultiDex
Bulid.gradle
defaultConfig {
applicationId "******"
minSdkVersion 21
targetSdkVersion 30
versionCode 8
versionName "05.15.21.8"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
继承MultiDexApplication
public class App extends MultiDexApplication {
private static App instance;
@Override
public void onCreate() {
MultiDex.install(this);
super.onCreate();
instance = this;
}
public static App getInstance ()
{
return instance;
}
@Override
public void onTerminate() {
super.onTerminate();
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
在您的清单中
<application
android:name=".App"
</application>
Thank You
关于 MultiDex 的帖子很多。有时,我遇到过解决错误,包括 build.gradle 的 defaultConfig
部分中的 multiDexEnabled true
。
但是,这个功能到底是什么?有哪些使用场景?
Android application (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.
因此,特点是:它允许您编译复杂的应用程序。使用它的场景是当您的应用程序由于达到 64K DEX 方法引用限制而无法编译时。这显示为构建错误,例如:
Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536
就这么简单
一个 .dex 文件可以有 65,536 个方法(引用),所以如果引用的数量超过 65,536,你就用 multidex。
更多说明!
一个 android 应用程序被编译成一个 .dex 文件,然后压缩成一个 .apk 文件。
DVM(Dalvik 虚拟机)使用 .dex file/files 来执行字节码。
是什么原因导致引用数超过65536个限制?
你写的方法 + Android 框架方法 + 第三方库(如 Volley,Retrofit, Facebook SDK 等)方法。
我读过“某处”
App Compat 24.2.1 包含 16.5k 个方法
Google Play 服务 GCM 9.6.1 包含 16.7k 个方法
因此,如果您创建了一个具有 App Compat 24.2.1 的简单 Hello world 应用程序,您已经 1/4 方式 跨越单个 dex 方法限制
访问 Android 开发者官方网站。
If your minSdkVersion is set to 21 or higher, multidex is enabled by default and you do not need the multidex support library.
当您的应用及其引用的库超过 65,536 个方法时,您会遇到构建错误,表明您的应用已达到 Android 构建架构的限制
如何在您的项目中启用 MultiDex
Bulid.gradle
defaultConfig {
applicationId "******"
minSdkVersion 21
targetSdkVersion 30
versionCode 8
versionName "05.15.21.8"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
继承MultiDexApplication
public class App extends MultiDexApplication {
private static App instance;
@Override
public void onCreate() {
MultiDex.install(this);
super.onCreate();
instance = this;
}
public static App getInstance ()
{
return instance;
}
@Override
public void onTerminate() {
super.onTerminate();
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
在您的清单中
<application
android:name=".App"
</application>
Thank You