gpt4 book ai didi

android - 如何收缩代码 - dex 中的 65k 方法限制

转载 作者:IT老高 更新时间:2023-10-28 13:06:46 29 4
gpt4 key购买 nike

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

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

1) 缩小代码

2) 构建多个dex文件(see this blog post)

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

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

最佳答案

看起来 Google 终于实现了解决方法/修复超过 dex 文件的 65K 方法限制。

About the 65K Reference Limit

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.

Multidex support prior to Android 5.0

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.

Multidex support for Android 5.0 and higher

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.

见:Building Apps with Over 65K Methods


Multidex Support Library

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.

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.

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

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


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

关于android - 如何收缩代码 - dex 中的 65k 方法限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15471772/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com