更新到 Google Play Services 11.8.0 后 Proguard 不工作

Proguard not working after update to Google Play Services 11.8.0

将 Google Play 服务更新到 11.8.0(从 11.6.2)后,我的构建停止工作。

这是我得到的:

Unexpected error while computing stack sizes: Class = [com/google/android/gms/internal/zzao] Method = [zzf(Ljava/lang/String;)J] Exception = [java.lang.IllegalArgumentException] (Stack size becomes negative after instruction [23] invokestatic #146 in [com/google/android/gms/internal/zzao.zzf(Ljava/lang/String;)J]) FAILURE: Build failed with an exception.

我正在使用 Android Studio 3.0.1 和 Gradle 4.4.1

我的应用build.gradle文件

buildscript {
    repositories {
        maven { url "https://maven.fabric.io/public" }
    }

    dependencies {
        classpath "io.fabric.tools:gradle:1.25.1"
    }
}

repositories {
    maven { url "https://maven.fabric.io/public" }
}

apply plugin: "com.android.application"
apply plugin: "io.fabric"
apply plugin: "let"
apply plugin: "realm-android"

android {
    compileSdkVersion project.androidSDKVersion
    buildToolsVersion("$androidBuildToolsVersion")

    defaultConfig {
        versionCode project.versionCode
        versionName project.versionName

        minSdkVersion project.androidMinSdkVersion
        targetSdkVersion project.androidTargetSdkVersion

        vectorDrawables.useSupportLibrary = true
        resConfigs "pl"
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources false
            crunchPngs false
            proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro", "proguard-fresco.pro"
            signingConfig signingConfigs.release
        }
        debug {
            ext.enableCrashlytics = false
            ext.alwaysUpdateBuildId = false
        }
    }

    flavorDimensions "tier", "minApi"

    productFlavors {
        minApi21 {
            minSdkVersion project.androidMinDevSdkVersion
            dimension "minApi"
        }

        minApi16 {
            minSdkVersion project.androidMinSdkVersion
            dimension "minApi"
        }

        dev {
            multiDexEnabled true
            dimension "tier"
        }

        prod {
            multiDexEnabled false
            dimension "tier"
        }
    }

    variantFilter { variant ->
        def names = variant.flavors*.name

        if (names.contains("prod") && names.contains("minApi21")) {
            setIgnore(true)
        }
    }

    applicationVariants.all { variant ->
        appendVersionNameVersionCode(variant, defaultConfig)
    }

    lintOptions {
        checkReleaseBuilds false
        textReport true
        textOutput "stdout"
        fatal "UnusedResources"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    debugImplementation("com.android.support:multidex:$multidexVersion")

    implementation("com.android.support:support-fragment:$androidSupportVersion")
    implementation("com.android.support:support-annotations:$androidSupportVersion")
    implementation("com.android.support:appcompat-v7:$androidSupportVersion")
    implementation("com.android.support:design:$androidSupportVersion")
    implementation("com.android.support:recyclerview-v7:$androidSupportVersion")
    implementation("com.android.support:cardview-v7:$androidSupportVersion")
    implementation("com.android.support:customtabs:$androidSupportVersion")
    implementation("com.android.support.constraint:constraint-layout:$constraintLayoutVersion")
    implementation("com.android.installreferrer:installreferrer:$installReferrerVersion")

    implementation("com.google.android.gms:play-services-analytics:$playServicesVersion")
    implementation("com.google.android.gms:play-services-location:$playServicesVersion")
    implementation("com.google.android.gms:play-services-ads:$playServicesVersion")
    implementation("com.google.android.gms:play-services-auth:$playServicesVersion")

    implementation("com.google.firebase:firebase-core:$playServicesVersion")
    implementation("com.google.firebase:firebase-messaging:$playServicesVersion")
    implementation("com.google.firebase:firebase-config:$playServicesVersion")
    implementation("com.google.firebase:firebase-auth:$playServicesVersion")
    implementation("com.google.firebase:firebase-invites:$playServicesVersion")

    (...) // I had removed other dependencies from the list
}

def appendVersionNameVersionCode(variant, defaultConfig) {
    variant.outputs.all { output ->

        def versionCode = android.defaultConfig.versionCode

        output.versionCodeOverride = versionCode
        outputFileName = "${rootProject.name}-${variant.name}-${variant.versionName}-${versionCode}.apk"
    }
}

apply plugin: "com.google.gms.google-services"

我有类似的问题。按照建议 ,您只需要在 build.gradle 文件中添加这些行:

...
release {
   debuggable false
   useProguard true
   ...
}
...

我还必须通过替换来删除 pro guard 优化:

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

来自

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

您不需要完全禁用优化,只需针对问题 class 进行优化。我遇到了同样的问题并通过将以下内容添加到 proguard-rules.pro 文件中解决了这个问题:

-keep class com.google.android.gms.internal.** { *; }