Proguard 可以保存到 multidex 应用程序吗?

Ant*_*ony 3 android proguard android-gradle-plugin android-multidex android-proguard

我有一个包含几个库的应用程序,达到了 65536 方法计数的红线。

  1. 我实现了将应用程序设置为multidexAPK。
  2. 对于大小优化,我决定使用 Proguard,因为我只使用了Guava和的一些功能common.java.lang,而这些库带来了他们的整个家庭。
  3. 在 Proguard 工作之后,我的应用程序 ref ~ 45 Kmethods
  4. 我经常读到 multidex 应用程序可能会不时崩溃
  5. 而且由于第二个 dex 运行时加载,这需要时间。

4和5是真的吗?

然后我只是尝试不使用mutidex,因为我的结束方法计数是 < 56Kmethods with prodGuard,但它失败了,好像它有更多!

为此,我只是将 gradle 参数设置multiDexEnabled为 false

还有什么要检查/做的吗?

这是我的 Gradle 的一部分:

android {
    compileSdkVersion ANDROID_BUILD_SDK_VERSION
    buildToolsVersion ANDROID_BUILD_TOOLS_VERSION

    defaultConfig {
        applicationId "XXXX"
        targetSdkVersion ANDROID_BUILD_TARGET_SDK_VERSION
        minSdkVersion ANDROID_BUILD_MIN_SDK_VERSION
        versionCode ANDROID_BUILD_VERSION_CODE
        versionName ANDROID_BUILD_APP_VERSION_NAME
        // Enabling multidex support.
        multiDexEnabled false
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            debuggable false
            ext.enableCrashlytics = true
            renderscriptOptimLevel 3
            signingConfig android.signingConfigs.release
            zipAlignEnabled true
            minifyEnabled true
            //  shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro', 'proguard-rules-new.pro', 'proguard-rules-eventbus.pro', 'proguard-rules-firebase.pro', 'proguard-rules-fabric.pro', 'proguard-rules-leakcanary.pro'
        }
        debug {
            debuggable true
            renderscriptOptimLevel 3
            applicationIdSuffix ".debug"
            versionNameSuffix "debug"
        }
    }
Run Code Online (Sandbox Code Playgroud)

Man*_*han 5

  1. 我经常读到 multidex 应用程序可能会不时崩溃

从 android 开发人员文档页面(http://developer.android.com/tools/building/multidex.html#limitations):

由于 Dalvik linearAlloc 限制(问题 78035),使用 multidex 配置的应用程序发出非常大的内存分配请求可能会在运行时崩溃。在 Android 4.0(API 级别 14)中增加了分配限制,但在 Android 5.0(API 级别 21)之前的 Android 版本上,应用程序可能仍会遇到此限制

ART 内置了对多 dex apk 的支持,因此多 dexing 不会在棒棒糖及以上版本中引起任何问题。您可能会在某些运行 kitkat 及更低版本的设备上看到问题,尽管这种情况应该很少见,除非您有非常高的方法数或内存要求。

  1. 而且由于第二个 dex 运行时加载,这需要时间。

是的,multidex 确实会显着减慢应用程序的首次启动时间。(在 yelp 的情况下高达 200%,当他们超过限制 20k 方法时)甚至冷启动时间也会增加。

因此,如果您可以避免多重 dexing,强烈建议您这样做。

即使您超过了限制,您仍然应该尽量减少方法数量,因为越来越多的方法会减慢预棒棒糖设备上的应用程序启动时间。

在您的情况下,如果您的构建成功但如果您看到运行时崩溃(尤其是“未找到类定义”),那么可能是您没有正确配置 proguard,并且它可能会剥离一些所需的组件。

来自 yelp 的 Timothy Meller 对这个问题进行了详细的讨论,他还分享了一些多 dex 优化和 proguard 配置的重要性:

https://www.youtube.com/watch?v=skmOBriQ28E

如果您想更好地了解 android 上的 multi-dexing,我建议您观看此内容