简体   繁体   English

什么是 Android MultiDex?

[英]What is Android MultiDex?

There are many posts about MultiDex.有很多关于 MultiDex 的帖子。 I have experienced, sometimes, errors solved including multiDexEnabled true in the defaultConfig section of my build.gradle.有时,我遇到过错误解决,包括multiDexEnabled truedefaultConfig部分中的multiDexEnabled true

But, what exactly is this feature?但是,这个功能究竟是什么? What are the scenarios for using it?有哪些使用场景?

Quoting the documentation :引用文档

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. Android 应用程序 (APK) 文件包含 Dalvik 可执行文件 (DEX) 文件形式的可执行字节码文件,其中包含用于运行您的应用程序的编译代码。 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. Dalvik Executable 规范将单个 DEX 文件中可以引用的方法总数限制为 65,536,包括 Android 框架方法、库方法和您自己代码中的方法。 Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.要突破这一限制,您需要将应用程序构建过程配置为生成多个 DEX 文件,称为 multidex 配置。

So, the feature is: it allows your complex app to compile.所以,特点是:它允许你的复杂应用程序编译。 The scenarios for using it are when your app fails to compile due to hitting the 64K DEX method reference limit.使用它的场景是当您的应用程序由于达到 64K DEX 方法引用限制而无法编译时。 This appears as a build error, such as:这显示为构建错误,例如:

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

It's as simple as this就这么简单

A single .dex file can have 65,536 methods(references) so if the number of references exceeds 65,536, you go with multidex.单个 .dex 文件可以有 65,536 个方法(引用),因此如果引用的数量超过 65,536,则使用 multidex。

More Explanation!更多解释!

An android application program is compiled into a .dex file which in turn zipped to a single .apk file.一个 android 应用程序被编译成一个 .dex 文件,该文件又被压缩成一个 .apk 文件。
DVM (Dalvik Virtual Machine) uses .dex file/files to execute bytecodes. DVM(Dalvik 虚拟机)使用 .dex 文件/文件来执行字节码。

What causes the number of references to exceed 65,536 limits?是什么导致引用数量超过 65,536 个限制?
Methods written by you + Android Framework methods + Third party library(eg Volley,Retrofit, Facebook SDK etc) methods.您编写的方法 + Android 框架方法 + 第三方库(例如 Volley、Retrofit、Facebook SDK 等)方法。
I have read "somewhere"我读过“某处”
App Compat 24.2.1 contains 16.5k methods App Compat 24.2.1 包含 16.5k 方法
Google Play Services GCM 9.6.1 contains 16.7k methods Google Play Services GCM 9.6.1 包含 16.7k 方法
So if you have created a simple Hello world application which has App Compat 24.2.1, you are already 1/4 way to cross the single dex methods limit所以如果你创建了一个简单的 Hello world 应用程序,它有 App Compat 24.2.1,你已经有1/4 的方式来跨越单一 dex 方法的限制

There are many posts about MultiDex.关于MultiDex的帖子很多。 I have experienced, sometimes, errors solved including multiDexEnabled true in the defaultConfig section of my build.gradle.有时,我遇到了已解决的错误,包括在multiDexEnabled truedefaultConfig部分中包含multiDexEnabled true的问题。

But, what exactly is this feature?但是,此功能到底是什么? What are the scenarios for using it?使用它的场景是什么?

来自 Android 开发者官方网站。

If your minSdkVersion is set to 21 or higher, multidex is enabled by default and you do not need the multidex support library.

When your app and the libraries it references exceed 65,536 methods, you encounter a build error that indicates your app has reached the limit of the Android build architecture当您的应用及其引用的库超过 65,536 个方法时,您会遇到构建错误,表明您的应用已达到 Android 构建架构的限制

How to enable MultiDex in your project如何在项目中启用 MultiDex

Bulid.gradle构建.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'
}

inherit MultiDexApplication继承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);
    }
}

In your Manifest在您的清单中

     <application
     android:name=".App"
 </application>

Thank You谢谢你

它还允许谷歌(和其他人)在代码片段中应用 DRM,比如 ..widevine,在它会给大多数非开发人员带来问题的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM