【问题标题】:Proguard minifyEnabled true for debug build, not working on pre-LollipopProguard minifyEnabled true 用于调试构建,不适用于预棒棒糖
【发布时间】:2016-12-06 12:11:24
【问题描述】:

我的应用程序的方法数高于 64k 限制,所以我使用 Proguard 来减少它。

发布版本一切正常。
另一方面,调试版本仅适用于 Lollipop+ 设备。在棒棒糖之前的设备上启动时,我总是遇到臭名昭著的错误:

com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

这是我在应用级 gradle 文件中的内容:

buildTypes {
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

我尝试在调试版本中删除minifyEnabled true,然后在 Lollipop+ 上的构建也失败了,因此 proguard 正在工作!但不是在棒棒糖之前。

据我所知,minifyEnabled 不应该依赖于平台,所以在棒棒糖之前的设备上,构建过程可能会跳过它(因为我不知道是什么原因)。

有人知道这个问题的解决方案吗?

ps。我知道多重索引的可能性,但我将其作为最后的手段。

编辑:

这是应用级gradle文件的完整代码:

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'me.tatarka.retrolambda'

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'me.tatarka:gradle-retrolambda:3.2.5'
        classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.5.5'
    }
}

android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.pierfrancescosoffritti.shuffly"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 30
        versionName "0.13"
    }
    buildTypes {
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

def dbflow_version = "3.1.1"

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    apt 'com.jakewharton:butterknife-compiler:8.2.1'
    apt "com.github.Raizlabs.DBFlow:dbflow-processor:${dbflow_version}"
    compile "com.github.Raizlabs.DBFlow:dbflow-core:${dbflow_version}"
    compile "com.github.Raizlabs.DBFlow:dbflow:${dbflow_version}"

    compile 'com.android.support:support-v4:24.1.1'
    compile 'com.android.support:design:24.1.1'
    compile 'com.android.support:recyclerview-v7:24.1.1'
    compile 'com.android.support:cardview-v7:24.1.1'
    compile 'com.android.support:palette-v7:24.1.1'
    compile 'com.jakewharton:butterknife:8.2.1'
    compile 'com.github.PierfrancescoSoffritti:AndroidUtils:0.5'
    compile 'com.github.PierfrancescoSoffritti:SlidingDrawer:0.10'
    compile 'com.github.PierfrancescoSoffritti:WebBasedOAuth:0.7'
    compile 'com.github.PierfrancescoSoffritti:ShrinkingImageLayout:0.4'
    compile 'com.github.PierfrancescoSoffritti:ExpandableLayout:0.1'
    compile 'com.google.apis:google-api-services-youtube:v3-rev177-1.22.0'
    compile 'com.google.api-client:google-api-client-android:1.20.0'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.1.8'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.github.Commit451:ElasticDragDismissLayout:1.0.4'
    compile 'com.google.firebase:firebase-core:9.2.1'
    compile 'com.google.firebase:firebase-crash:9.2.1'
    compile 'com.google.firebase:firebase-ads:9.2.1'
    compile 'com.artemzin.rxjava:proguard-rules:1.1.8.0'
}

apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.getkeepsafe.dexcount'

项目级 gradle 文件:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'com.google.gms:google-services:3.0.0'
        classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.5.5'
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

保护规则:

# retrolambda
-dontwarn java.lang.invoke.*

# picasso
-dontwarn com.squareup.okhttp.**

# adMob
-keep public class com.google.android.gms.ads.** {
   public *;
}
-keep public class com.google.ads.** {
   public *;
}

# GoogleApiClient
# Needed to keep generic types and @Key annotations accessed via reflection
-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault
-keepclassmembers class * {
  @com.google.api.client.util.Key <fields>;
}
# Needed by google-http-client-android when linking against an older platform version
-dontwarn com.google.api.client.extensions.android.**
# Needed by google-api-client-android when linking against an older platform version
-dontwarn com.google.api.client.googleapis.extensions.android.**
# Needed by google-play-services when linking against an older platform version
-dontwarn com.google.android.gms.**
# com.google.client.util.IOUtils references java.nio.file.Files when on Java 7+
-dontnote java.nio.file.Files, java.nio.file.Path
# Suppress notes on LicensingServices
-dontnote **.ILicensingService
# Suppress warnings on sun.misc.Unsafe
-dontnote sun.misc.Unsafe
-dontwarn sun.misc.Unsafe

# DBFlow
-keep class * extends com.raizlabs.android.dbflow.config.DatabaseHolder { *; }

【问题讨论】:

  • 您没有仅包含用于调试的依赖项吗?比如,LeakCanary?
  • 不,我也在使用github.com/KeepSafe/dexcount-gradle-plugin 检查调试版本的方法计数,这很好。大约是30k。 (用 proguard 缩小)
  • 请显示您的 gradle.build 文件的完整代码
  • @Divers 刚刚添加到问题中
  • 请同时发布您的根 build.gradle 文件和 proguard-rules.pro 文件

标签: android android-studio android-proguard


【解决方案1】:

如果您想尝试减少方法计数,或者至少您想了解从发布到调试的区别,我建议您尝试查看 AS 2.2 预览版 5 中可用的 dex 计数,他们有一个 youtube 视频 (可能来自 google IO),这使得计算您的方法变得非常容易。

这仅用于计数方法,如果您仍然使用相同的buildToolsVersion "23.0.3",您应该在两个版本的 AS 中获得完全相同的 apk 文件(除了 AS 2.2 附带其自己的 JDK 版本,即不应该妨碍你)。

免责声明:除了玩它之外,我从未使用过该工具,所以在您真正找到罪魁祸首之后,我不知道该推荐什么。

编辑:这是“Apk Analyzer”的视频https://youtu.be/csaXml4xtN8?t=331,位于“Build”菜单中。不要尝试早于 2.2 preview 3 或更高版本的评论,他们预览了一些直到以后才发布的东西。

编辑2:为什么你只在发布时使用shrinkResources?这就是应该消除多余方法的行:"minifyEnabled" vs "shrinkResources" - what's the difference? and how to get the saved space?

【讨论】:

  • 我已经在使用github.com/KeepSafe/dexcount-gradle-plugin 计算我的方法了。在被 proguard 缩小后,方法计数为 30k。所以没关系。
  • 嗯,我会尝试阅读一些关于艺术的东西,在 kit kat 中引入的不是可选的,棒棒糖是稳定的吗?但是,如果您使用与发布相同的信息,您在调试中是否有相同的错误?由于这些是默认变体,因此除了“可调试的真”之外,它们可能具有隐藏的配置
  • 是的,这很奇怪。我会确保通过在没有 AS 的情况下构建和安装 apk 来尝试重现该问题,或者至少如果您正在使用它,请确保关闭即时运行。此外,我会使用第二个脚本/gradle 插件仔细检查您的调试版本的方法计数,以确保,因为该错误消息对于达到 65k 方法计数非常特殊。
  • 我也遇到过这种情况,在调试块上添加 shrinkResources true 就可以了。
【解决方案2】:

在应用级 gradle 文件中用此代码替换您的构建类型代码。 当您在最终发布之前在您的设备上调试或运行代码时,然后选择构建的变体调试,当您要构建签名的 apk 以进行发布时,然后选择构建的变体发布

  buildTypes {
                debug {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                }
                release {
                    shrinkResources true
                    minifyEnabled true
                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                }
}

【讨论】:

    【解决方案3】:

    Enable multidex enabled true 并允许将您的应用程序作为 multidex 应用程序支持。需要遵循的几个步骤是:

     defaultConfig {
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 3
            versionName "1.0.1"
            **multiDexEnabled true**
        }
    

    将此依赖项添加到依赖项列表中

    编译'com.android.support:multidex:1.0.0'

    使用 MultiDexApplication 扩展您的应用程序级类

    在您的应用程序类中编写此方法

    protected void attachBaseContext(Context base)
        {
            super.attachBaseContext(base);
            MultiDex.install(this);
        }
    

    无论调试还是发布版本,都保持 minifyEnabled 为 false

    更新

    dexOptions {
            jumboMode = true
            javaMaxHeapSize "4g" //specify the heap size for the dex process
            preDexLibraries = false
        }
    

    【讨论】:

    • 正如我所说:“ps。我知道多重索引的可能性,但我将其作为最后的手段。”
    猜你喜欢
    • 2019-11-18
    • 2016-07-04
    • 2015-01-14
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2015-02-25
    相关资源
    最近更新 更多