如何使用新的 Android Multidex 支持库启用多索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26609734/
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
How to enable multidexing with the new Android Multidex support library
提问by Janusz
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 的应用程序需要哪些步骤?
回答by Chad Bingham
Edit:
编辑:
Android 5.0 (API level 21) and higher uses ART which supports multidexing. Therefore, if your minSdkVersion
is 21 or higher, the multidex support library is not needed.
Android 5.0(API 级别 21)及更高版本使用支持多索引的 ART。因此,如果您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.
有关更多信息,这是一个很好的指南。
回答by Janusz
The following steps are needed to start multi dexing:
启动多索引需要以下步骤:
Add android-support-multidex.jar to your project. The jar can be found in your Android SDK folder /sdk/extras/android/support/multidex/library/libs
将 android-support-multidex.jar 添加到您的项目中。该 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. The next step is to convince gradle to build a multi 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
现在您的应用程序已准备好使用多 dex。下一步是说服 gradle 构建一个多 dexed apk。构建工具团队正在努力使这更容易,但目前您需要将以下内容添加到应用程序 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 博客上找到。
回答by smoothumut
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>
回答by MohammadReza
In your build.gradleadd this dependency:
在你的build.gradle添加这个依赖:
compile 'com.android.support:multidex:1.0.1'
again in your build.gradlefile add this line to defaultConfigblock:
再次在您的build.gradle文件中将此行添加到defaultConfig块:
multiDexEnabled true
Instead of extending your application class from Applicationextend it from MultiDexApplication; like :
不是从Application扩展您的应用程序类,而是从MultiDexApplication扩展它;喜欢 :
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);
}
}
回答by Isaak Osipovich Dunayevsky
build.gradle
构建.gradle
multiDexEnabled true
implementation 'androidx.multidex:multidex:2.0.1'
AndroidManifest.xml
AndroidManifest.xml
<application
android:name="androidx.multidex.MultiDexApplication"
回答by sonnv1368
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.
它会工作!谢谢。
回答by Malik Abu Qaoud
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'
}
}
回答by Moises Apaza Q
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'
}
}
}
回答by Jameson
Here is an up-to-date approach as of May 2020, with Android X.
这是截至 2020 年 5 月使用 Android X 的最新方法。
For minSdk
>= 21
对于minSdk
>= 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。
For minSdk
< 21
对于minSdk
< 21
In your module-level build.gradle
, ensure that the following configurations are populated:
在您的module-level 中build.gradle
,确保填充了以下配置:
android {
defaultConfig {
multiDexEnabled true
}
}
dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
}
Then, your src/main/AndroidManifest.xml
needs to 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>
回答by pavel
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>