java 如何为 React Native 启用 multidex?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44140496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:58:35  来源:igfitidea点击:

How do I enable multidex for react native?

javaandroidreact-native

提问by Kloar

I'm pretty sure its a built in feature but I cant find anything when searching or in the docs. Is there a flag for building with multidex enabled?

我很确定它是一个内置功能,但在搜索或在文档中找不到任何东西。是否有启用 multidex 的构建标志?

On another note: Any way to see which libraries are messing with your method count? Hitting the 64k limit came as quite a surprise.

另一个注意事项:有什么方法可以查看哪些库与您的方法计数混淆了?达到 64k 的限制是非常令人惊讶的。

采纳答案by Kloar

Found the answer somewhere else. It's no different than enabling it for any regular Android project.

在别处找到了答案。这与为任何常规 Android 项目启用它没有什么不同。

android {
    ....
    defaultConfig {
        ...
        multiDexEnabled true
    }

As for method count, this site does the trick: http://inloop.github.io/apk-method-count/

至于方法计数,这个站点可以解决问题:http: //inloop.github.io/apk-method-count/

回答by Brandon

For RN 0.59+ and using Gradle 3.4.1, none of the answers here had the complete solution. I did the following and it worked:

对于 RN 0.59+ 和使用 Gradle 3.4.1,这里的答案都没有完整的解决方案。我做了以下事情,它奏效了:

  1. In android/app/build.gradle, update the dependencyblock:

    dependencies {
        // ... your other dependencies
    
        // Multidex
        implementation 'com.android.support:multidex:1.0.3'
    }
    

    And also update the defaultConfigin the androidblock:

    defaultConfig {
        // ... your `applicationId`, etc.
        multiDexEnabled true
    }
    
  2. In MainApplication.java, replace
    import android.app.Application;
    
    at the top with
    import android.support.multidex.MultiDexApplication;
    
    ORif you're on RN 0.60+ or have manually upgraded to AndroidXthen use this instead:
    import androidx.multidex.MultiDexApplication;
    
  3. Still in MainApplication.java, replace
    public class MainApplication extends Application implements ReactApplication {
    
    with
    public class MainApplication extends MultiDexApplication implements ReactApplication {
    
  1. 在 中android/app/build.gradle,更新dependency块:

    dependencies {
        // ... your other dependencies
    
        // Multidex
        implementation 'com.android.support:multidex:1.0.3'
    }
    

    并同时更新defaultConfigandroid块:

    defaultConfig {
        // ... your `applicationId`, etc.
        multiDexEnabled true
    }
    
  2. MainApplication.java,替换
    import android.app.Application;
    
    在顶部与
    import android.support.multidex.MultiDexApplication;
    
    或者,如果您使用的是 RN 0.60+ 或已手动升级到AndroidX,请改用它:
    import androidx.multidex.MultiDexApplication;
    
  3. 还在MainApplication.java,替换
    public class MainApplication extends Application implements ReactApplication {
    
    public class MainApplication extends MultiDexApplication implements ReactApplication {
    

回答by Alex Lévy

android/app/build.gradle

android/app/build.gradle

android {
    ....
    defaultConfig {
        ...
        multiDexEnabled true
    }

If you are using Multidex, your Application should extends MultiDexApplication instead of Application.

如果您使用 Multidex,您的应用程序应该扩展 MultiDexApplication 而不是应用程序。

MyApplication.java

我的应用程序

import android.support.multidex.MultiDexApplication;

public class MyApplication extends MultiDexApplication{
     ...
}

回答by LordParsley

Since Applicationmust extend ReactApplication, I used a similar approach to the one suggested herewhich is documented in point 2 in the Android Developer reference on multidexto attach to the base context:

由于Applicationmust extend ReactApplication,我使用了与此处建议的方法类似的方法,方法在 Android 开发人员参考 multidex中的第 2 点中记录,以附加到基本上下文:

app/build.gradle

应用程序/build.gradle

android {
    compileSdkVersion projectCompileSdkVersion // Or your version

    defaultConfig {
        minSdkVersion projectMinSdkVersion // Or your version
        targetSdkVersion projectTargetSdkVersion // Or your version
        multiDexEnabled true // *
    }
    dexOptions {
        jumboMode true // *
    }
}

dependencies {
  compile 'com.android.support:multidex:1.0.3' // https://mvnrepository.com/artifact/com.android.support/multidex
}

MainApplication.java

主应用程序

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

回答by Aung Myint Thein

However, if your minSdkVersion is set to 20 or lower, then you must use the multidex support library as follows:

但是,如果您的 minSdkVersion 设置为 20 或更低,那么您必须使用 multidex 支持库,如下所示:

android {
    defaultConfig {
        ...
        minSdkVersion 15 
        targetSdkVersion 26
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.3'
}

https://developer.android.com/studio/build/multidex

https://developer.android.com/studio/build/multidex