[英]How to enable multidexing with the new Android Multidex support library
I want to use the new Multidex support library to break the method limit for one of my apps.我想使用新的 Multidex 支持库来打破我的一个应用程序的方法限制。
With Android Lollipop Google introduced a multidex support library that makes it easy to multidex.在 Android Lollipop 中,Google 引入了一个 multidex 支持库,可以轻松实现 multidex。
What steps are needed to use this library and to build my app with multidex support?使用这个库和构建我的支持 multidex 的应用程序需要哪些步骤?
Edit:编辑:
Android 5.0 (API level 21) and higher uses ART which supports multidexing. Android 5.0(API 级别 21)及更高版本使用支持多索引的 ART。 Therefore, if your
minSdkVersion
is 21 or higher, the multidex support library is not needed.因此,如果您的
minSdkVersion
为 21 或更高,则不需要 multidex 支持库。
Modify your build.gradle
:修改你的
build.gradle
:
android {
compileSdkVersion 22
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 14 //lower than 14 doesn't support multidex
targetSdkVersion 22
// Enabling multidex support.
multiDexEnabled true
}
}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
If you are running unit tests, you will want to include this in your Application
class:如果您正在运行单元测试,您需要将其包含在您的
Application
类中:
public class YouApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
Or just make your application
class extend MultiDexApplication
或者只是让你的
application
类扩展MultiDexApplication
public class Application extends MultiDexApplication {
}
For more info, this is a good guide.有关更多信息,这是一个很好的指南。
The following steps are needed to start multi dexing:启动多索引需要以下步骤:
Add android-support-multidex.jar to your project.将 android-support-multidex.jar 添加到您的项目中。 The jar can be found in your Android SDK folder /sdk/extras/android/support/multidex/library/libs
该 jar 可以在您的 Android SDK 文件夹 /sdk/extras/android/support/multidex/library/libs 中找到
Now you either let your apps application class extend MultiDexApplication现在你要么让你的应用程序类扩展 MultiDexApplication
public class MyApplication extends MultiDexApplication
or you override attachBaseContext like this:或者你像这样覆盖 attachBaseContext:
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
I used the override approach because that does not mess with the class hierarchy of your application class.我使用了覆盖方法,因为它不会干扰应用程序类的类层次结构。
Now your app is ready to use multi dex.现在您的应用程序已准备好使用多 dex。 The next step is to convince gradle to build a multi dexed apk.
下一步是说服 gradle 构建一个多 dexed apk。 The build tools team is working on making this easier, but for the moment you need to add the following to the android part of your apps build.gradle
构建工具团队正在努力使这更容易,但目前您需要将以下内容添加到应用程序 build.gradle 的 android 部分
dexOptions {
preDexLibraries = false
}
And the following to the general part of your apps build.gradle以下是您的应用程序 build.gradle 的一般部分
afterEvaluate {
tasks.matching {
it.name.startsWith('dex')
}.each { dx ->
if (dx.additionalParameters == null) {
dx.additionalParameters = ['--multi-dex']
} else {
dx.additionalParameters += '--multi-dex'
}
}
}
More info can be found on Alex Lipovs blog .更多信息可以在Alex Lipovs 博客上找到。
SIMPLY, in order to enable multidex, you need to ...简单地说,为了启用 multidex,您需要...
android {
compileSdkVersion 21
buildToolsVersion "21.1.0"
defaultConfig {
...
minSdkVersion 14
targetSdkVersion 21
...
// Enabling multidex support.
multiDexEnabled true
}
...
}
dependencies {
implementation 'com.android.support:multidex:1.0.0'
}
also you must change your manifest file.您还必须更改清单文件。 In your manifest add the MultiDexApplication class from the multidex support library to the application element like this
在您的清单中,将 MultiDexApplication 类从 multidex 支持库添加到应用程序元素,如下所示
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.myapplication">
<application
...
android:name="android.support.multidex.MultiDexApplication">
...
</application>
</manifest>
Here is an up-to-date approach as of October 2020, with Android X. This comes from Android's documentation, " Enable multidex for apps with over 64K methods ."这是截至 2020 年 10 月使用 Android X 的最新方法。这来自 Android 的文档“ 为具有超过 64K 方法的应用程序启用 multidex ”。
minSdk
>= 21minSdk
>= 21 You do not need to do anything.你不需要做任何事情。 All of these devices use the Android RunTime (ART) VM, which supports multidex natively.
所有这些设备都使用 Android RunTime (ART) VM,它本机支持 multidex。
minSdk
< 21minSdk
< 21 In your module -level build.gradle
, ensure that the following configurations are populated:在您的模块级
build.gradle
,确保填充了以下配置:
android {
defaultConfig {
multiDexEnabled true
}
}
dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
}
You need to install explicit multidex support.您需要安装显式 multidex 支持。 The documentation includesthree methods to do so , and you have to pick one .
该文档包括三种方法,您必须选择一种。
For example, in your src/main/AndroidManifest.xml
, you can declare MultiDexApplication
as the application:name
:例如,在您的
src/main/AndroidManifest.xml
,您可以将MultiDexApplication
声明为application:name
:
<manifest package="com.your.package"
xmlns:android="http://schemas.android.com/apk/res/android">
<application android:name="androidx.multidex.MultiDexApplication" />
</manifest>
In your build.gradle add this dependency:在你的build.gradle添加这个依赖:
compile 'com.android.support:multidex:1.0.1'
again in your build.gradle file add this line to defaultConfig block:再次在您的build.gradle文件中将此行添加到defaultConfig块:
multiDexEnabled true
Instead of extending your application class from Application extend it from MultiDexApplication ;不是从Application扩展您的应用程序类,而是从MultiDexApplication扩展它; like :
喜欢 :
public class AppConfig extends MultiDexApplication {
now you're good to go!现在你可以走了! And in case you need it, all
MultiDexApplication
does is如果您需要它,
MultiDexApplication
所做的就是
public class MultiDexApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
build.gradle构建.gradle
multiDexEnabled true
implementation 'androidx.multidex:multidex:2.0.1'
AndroidManifest.xml AndroidManifest.xml
<application
android:name="androidx.multidex.MultiDexApplication"
Step 1: Change build.grade第 1 步:更改 build.grade
defaultConfig {
...
// Enabling multidex support.
multiDexEnabled true
}
dependencies {
...
compile 'com.android.support:multidex:1.0.0'
}
Step 2: Setting on the Application class第 2 步:在 Application 类上设置
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
MultiDex.install(this);
}
}
Step 3: Change grade.properties第 3 步:更改 grade.properties
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
It will work!.它会工作! Thanks.
谢谢。
First you should try with Proguard (This clean all code unused)首先,您应该尝试使用 Proguard(这会清除所有未使用的代码)
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Add to AndroidManifest.xml:添加到 AndroidManifest.xml:
android:name="android.support.multidex.MultiDexApplication"
OR或者
MultiDex.install(this);
in your custom Application's attachBaseContext method在您的自定义应用程序的 attachBaseContext 方法中
or your custom Application extend MultiDexApplication或您的自定义应用程序扩展 MultiDexApplication
add multiDexEnabled = true in your build.gradle在 build.gradle 中添加 multiDexEnabled = true
defaultConfig {
multiDexEnabled true
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
}
}
With androidx, the classic support libraries no longer work.使用 androidx,经典支持库不再有效。
Simple solution is to use following code简单的解决方案是使用以下代码
In your build.gradle
file在你的
build.gradle
文件中
android{
...
...
defaultConfig {
...
...
multiDexEnabled true
}
...
}
dependencies {
...
...
implementation 'androidx.multidex:multidex:2.0.1'
}
And in your manifest just add name attribute to the application tag在您的清单中,只需将 name 属性添加到应用程序标签
<manifest ...>
<application
android:name="androidx.multidex.MultiDexApplication"
...
...>
...
...
</application>
</manifest>
If your application is targeting API 21 or above multidex is enables by default.如果您的应用程序以 API 21 或更高版本为目标,则默认启用 multidex。
Now if you want to get rid of many of the issues you face trying to support multidex - first try using code shrinking by setting minifyEnabled true
.现在,如果您想摆脱在尝试支持 multidex 时遇到的许多问题 - 首先尝试通过将
minifyEnabled true
设置minifyEnabled true
来使用代码收缩。
If you want to enable multi-dex in your project then just go to gradle.builder如果你想在你的项目中启用 multi-dex 然后去 gradle.builder
and add this in your dependencie并将其添加到您的依赖项中
dependencies {
compile 'com.android.support:multidex:1.0.0'}
then you have to add那么你必须添加
defaultConfig {
...
minSdkVersion 14
targetSdkVersion 21
...
// Enabling multidex support.
multiDexEnabled true}
Then open a class and extand it to Application If your app uses extends the Application class, you can override the oncrete() method and call然后打开一个类,扩展为Application 如果你的app使用扩展了Application类,你可以覆盖oncrete()方法并调用
MultiDex.install(this)
to enable multidex.启用多重索引。
and finally add into your manifest最后添加到您的清单中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.myapplication">
<application
...
android:name="android.support.multidex.MultiDexApplication">
...
</application>
</manifest>
All answers above are awesome以上所有答案都很棒
Also add this otherwise your app will crash like mine without any reason还要添加这个,否则你的应用程序会像我的一样无缘无故崩溃
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
just adding this snipped in the build.gradle also works fine只需在 build.gradle 中添加这个剪辑也可以正常工作
android {
compileSdkVersion 22
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 14 //lower than 14 doesn't support multidex
targetSdkVersion 22
**// Enabling multidex support.
**multiDexEnabled true****
}
}
Multi_Dex.java Multi_Dex.java
public class Multi_Dex extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.