如何收缩代码 - dex 中的 65k 方法限制 [英] How to shrink code - 65k method limit in dex

查看:28
本文介绍了如何收缩代码 - dex 中的 65k 方法限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的 Android 应用程序,它依赖于许多库项目.Android 编译器对每个 .dex 文件有 65536 个方法的限制,我已经超过了这个数字.

I have a rather large Android app that relies on many library projects. The Android compiler has a limitation of 65536 methods per .dex file and I am surpassing that number.

当你达到方法限制时,基本上有两条路径可以选择(至少我知道).

There are basically two paths you can choose (at least that I know of) when you hit the method limit.

1) 缩小代码

2) 构建多个 dex 文件(见这篇博文)

2) Build multiple dex files (see this blog post)

我查看了两者并试图找出导致我的方法计数如此之高的原因.Google Drive API 占据了 Guava 依赖项的最大部分,超过 12,000 个.Drive API v2 的库总数超过 23,000 个!

I looked into both and tried to find out what was causing my method count to go so high. The Google Drive API takes the biggest chunk with the Guava dependency at over 12,000. Total libs for Drive API v2 reach over 23,000!

我想我的问题是,你认为我应该怎么做?我应该删除 Google Drive 集成作为我的应用程序的一项功能吗?有没有办法缩小 API(是的,我使用 proguard)?我应该走多 dex 路线吗(这看起来相当痛苦,尤其是处理第三方 API)?

My question I guess is, what do you think I should do? Should I remove Google Drive integration as a feature of my app? Is there a way to shrink the API down (yes, I use proguard)? Should I go the multiple dex route (which looks rather painful, especially dealing with third party APIs)?

推荐答案

看起来谷歌终于实现了一个解决方法/修复超过 dex 文件的 65K 方法限制.

It looks like Google has finally implementing a workaround/fix for surpassing the 65K method limit of dex files.

关于 65K 参考限制

Android 应用程序 (APK) 文件包含Dalvik Executable (DEX) 形式的可执行字节码文件文件,其中包含用于运行您的应用程序的编译代码.这Dalvik Executable 规范限制了方法的总数可以在单个 DEX 文件中引用到 65,536,包括Android框架方法、库方法、自己的方法代码.超过此限制需要您配置您的应用程序生成多个 DEX 文件的构建过程,称为 multidex配置.

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. 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. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.

Android 5.0 之前的 Multidex 支持

Android 5.0 之前的平台版本使用 Dalvik 运行时用于执行应用程序代码.默认情况下,Dalvik 将应用程序限制为单个每个 APK 的 classes.dex 字节码文件.为了解决这个问题限制,您可以使用 multidex 支持库,它变成应用程序的主要 DEX 文件的一部分,然后管理对额外的 DEX 文件及其包含的代码.

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 及更高版本的 Multidex 支持

Android 5.0 及更高版本使用一个名为 ART 的运行时,它本机支持从应用 APK 文件中加载多个 dex 文件.艺术在应用程序安装时执行预编译,扫描classes(..N).dex 文件并将它们编译成单个 .oat 文件由安卓设备执行.有关 Android 的更多信息5.0 运行时,请参阅 介绍 ART.

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.

请参阅:使用超过 65K 方法构建应用程序

Multidex 支持库

这个库为构建提供支持具有多个 Dalvik 可执行 (DEX) 文件的应用程序.引用的应用使用 multidex 配置需要超过 65536 种方法.有关使用 multidex 的更多信息,请参阅 使用 Over 构建应用65K 方法.

This library provides support for building apps with multiple Dalvik Executable (DEX) files. Apps that reference more than 65536 methods are required to use multidex configurations. For more information about using multidex, see Building Apps with Over 65K Methods.

这个库位于/extras/android/support/multidex/下载 Android 支持库后的目录.这库不包含用户界面资源.将其包含在您的应用程序项目,请按照 添加库的说明进行操作没有资源.

This library is located in the /extras/android/support/multidex/ directory after you download the Android Support Libraries. The library does not contain user interface resources. To include it in your application project, follow the instructions for Adding libraries without resources.

此库的 Gradle 构建脚本依赖标识符为如下:

The Gradle build script dependency identifier for this library is as follows:

com.android.support:multidex:1.0.+ 这个依赖符号指定发布版本 1.0.0 或更高版本.

com.android.support:multidex:1.0.+ This dependency notation specifies the release version 1.0.0 or higher.

<小时>

您仍应通过积极使用 proguard 并检查您的依赖项来避免达到 65K 方法限制.


You should still avoid hitting the 65K method limit by actively using proguard and reviewing your dependencies.

这篇关于如何收缩代码 - dex 中的 65k 方法限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆