Java Android 出现错误“无法在单个 dex 文件中放入请求的类”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51341627/
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
Android gives error "Cannot fit requested classes in a single dex file"
提问by KevinB
I don't know why but it's impossible to launch my app on my mobile this morning. I get this error message:
我不知道为什么,但今天早上无法在我的手机上启动我的应用程序。我收到此错误消息:
Cannot fit requested classes in a single dex file. Try supplying a main-dex list.
# methods: 68061 > 65536 Message{kind=ERROR, text=Cannot fit requested classes in a single dex file. Try supplying a main-dex list.
# methods: 68061 > 65536, sources=[Unknown source file], tool
无法在单个 dex 文件中容纳请求的类。尝试提供一个 main-dex 列表。
# 方法:68061 > 65536 消息{种类=错误,文本=无法在单个 dex 文件中容纳请求的类。尝试提供一个 main-dex 列表。
# 方法:68061 > 65536,sources=[未知源文件],工具
I'm really new to Android and I don't understand the problem and what I need to do? And why I get this problem now and not before?
我对 Android 真的很陌生,我不明白这个问题以及我需要做什么?为什么我现在遇到这个问题而不是以前?
采纳答案by Gokul Nath KP
Add dependency in build.gradle
file:
在build.gradle
文件中添加依赖项:
implementation 'com.android.support:multidex:1.0.3'
In build.gradle
's defaultConfig
section add:
在build.gradle
的defaultConfig
部分添加:
multiDexEnabled true
More details here: Error:Cannot fit requested classes in a single dex file.Try supplying a main-dex list. # methods: 72477 > 65536
更多详细信息:错误:无法在单个 dex 文件中容纳请求的类。尝试提供主 dex 列表。# 方法:72477 > 65536
回答by Gastón Saillén
multidex is not always the solution to the problem, is true that it will generate more dex files to fit your method count, but be sure to not import more methods that you need because this in long term will make your builds slower than before.
multidex 并不总是问题的解决方案,确实它会生成更多的 dex 文件以适应您的方法数量,但请确保不要导入更多您需要的方法,因为从长远来看,这会使您的构建比以前更慢。
For example, if you just need to use the location library from play services, you have two options
例如,如果您只需要使用播放服务中的位置库,您有两种选择
First one is implementing the whole play-services libraries that will come with location
第一个是实现随位置提供的整个游戏服务库
implementation 'com.google.android.gms:play-services:11.8.0'
These whole libraries could have more than 40.000+ methods (is only an estimative, I don't really know the total count), being close to reaching the 65536 limit methods.
这些整个库可能有超过 40.000 多个方法(只是一个估计值,我真的不知道总数),接近达到 65536 个限制方法。
Instead you should be targeting only the libraries you will use instead of the whole bundle of libraries
相反,您应该只针对您将使用的库而不是整个库包
in this case
在这种情况下
implementation 'com.google.android.gms:play-services-location:11.8.0'
could have just 50 - 100 methods to work with, which will be better at build time than loading a whole bunch of methods from the whole library package that you won't ever use.
可能只有 50 - 100 个方法可以使用,这在构建时比从整个库包中加载一大堆你永远不会使用的方法要好。
this is just a tip to avoid getting
这只是避免获得的提示
Cannot fit requested classes in a single dex file.
无法在单个 dex 文件中容纳请求的类。
For minSdkVersion above android 5.0 API 20 +
对于 android 5.0 API 20 + 以上的 minSdkVersion
Android 5.0 and higher uses a runtime called ART which natively supports loading multiple dex files from application APK files. ART performs pre-compilation at application install time which scans for classes(..N).dex files and compiles them into a single .oat file for execution by the Android device. For more information on the Android 5.0 runtime, see Introducing ART.
Android 5.0 及更高版本使用称为 ART 的运行时,该运行时本机支持从应用程序 APK 文件加载多个 dex 文件。ART 在应用程序安装时执行预编译,扫描类 (..N).dex 文件并将它们编译成单个 .oat 文件以供 Android 设备执行。有关 Android 5.0 运行时的更多信息,请参阅 ART 简介。
If you are targeting lower devices (Android 4.1 API 16) or before Android 5 (API 20)
如果您的目标是低端设备 (Android 4.1 API 16) 或 Android 5 (API 20) 之前的设备
Versions of the platform prior to Android 5.0 use the Dalvik runtime for executing app code. By default, Dalvik limits apps to a single classes.dex bytecode file per APK. In order to get around this limitation, you can use the multidex support library, which becomes part of the primary DEX file of your app and then manages access to the additional DEX files and the code they contain.
Android 5.0 之前的平台版本使用 Dalvik 运行时来执行应用程序代码。默认情况下,Dalvik 将应用程序限制为每个 APK 的单个 classes.dex 字节码文件。为了解决这个限制,您可以使用 multidex 支持库,它成为您应用的主要 DEX 文件的一部分,然后管理对其他 DEX 文件及其包含的代码的访问。
You will need to use multidex in this last case
在最后一种情况下,您将需要使用 multidex
回答by Cabdirashiid
Try cleaning project and running the app again.
尝试清理项目并再次运行应用程序。
On Android Studio Windows, Go to Build then Clean Project. Now try running the app.
在 Android Studio Windows 上,转到 Build 然后 Clean Project。现在尝试运行该应用程序。
回答by Anthony
In build.gradle(app) file: Add the following in the dependencies:
在 build.gradle(app) 文件中:在依赖项中添加以下内容:
implementation 'com.android.support:multidex:1.0.3'
And add the follwing in the defaultConfig,
并在 defaultConfig 中添加以下内容,
multiDexEnabled true
Hope you will find the solution.
希望你能找到解决办法。