android - Proguard 重复 Zip 条目

标签 android gradle android-gradle-plugin proguard butterknife

我正在尝试通过 Proguard 运行我的 android 应用程序,但是这一直出现:

Warning:Exception while processing task java.io.IOException: Can't write [C:\Users\Aliaksei\AndroidStudioProjects\SignOutSystem\app\build\intermediates\transforms\proguard\debug\jars\3\1f\main.jar] (Can't read [D:\Users\Aliaksei\.android\build-cache\f7a6034d02d095f18cc21950f131d07fa78b41c0\output\jars\classes.jar(;;;;;;**.class)] (Duplicate zip entry [android/support/v4/os/j.class == classes.jar:android/support/v4/os/ResultReceiver$MyResultReceiver.class]))

我知道这意味着 android.support.v4 在某处被声明了两次,所以我尝试添加 exclude group: 'com.android.support', module: 'support-v4' 到可能包含它的任何可能的依赖项,但是即使在清理和重建之后也没有用。

我想我一定是在某处遗漏了一个依赖项,于是我开始将它添加到所有内容中(Overkill),但仍然没有奏效。

四处搜索,我在类似的帖子中找到了这个 fragment ,并将其添加到 android 部分:

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
}

我的想法已经用完了,寻找答案似乎让我找到了我已经尝试过的东西。有什么我想念的吗?

app build.gradle(一团糟,上面有所有 hack):

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
//apply plugin: 'com.getkeepsafe.dexcount'

android {
    compileSdkVersion 23
    buildToolsVersion '25.0.0'

    defaultConfig {
        applicationId "com.aliakseipilko.signoutsystem"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            zipAlignEnabled true
            proguardFile 'proguard-signoutsystem.txt'
        }
        debug {
            minifyEnabled true
            shrinkResources false
            zipAlignEnabled true
            proguardFile 'proguard-signoutsystem.txt'
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

//dexcount {
//    format = "list"
//    includeClasses = false
//    includeFieldCount = true
//    includeTotalMethodCount = true
//    orderByMethodCount = true
//    verbose = false
//    maxTreeDepth = Integer.MAX_VALUE
//    teamCityIntegration = false
//    enableForInstantRun = false
//}

repositories {
    maven {
        url "https://jitpack.io"
    }
}

dependencies {
    compile('com.google.api-client:google-api-client-android:1.22.0') {
        exclude group: 'org.apache.httpcomponents'
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile('com.google.apis:google-api-services-sheets:v4-rev19-1.22.0') {
        exclude group: 'org.apache.httpcomponents'
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile('com.google.android.gms:play-services-identity:9.2.0') {
        exclude group: 'com.google.guava'
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile ('com.google.android.gms:play-services-auth:9.2.0') {
        exclude group: 'com.android.support', module: 'support-v4'
    }

    compile ('com.android.support:appcompat-v7:23.4.0') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile ('com.android.support:design:23.4.0') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile 'com.android.support:support-v4:23.4.0'
    compile ('com.android.support:percent:23.4.0') {
        exclude group: 'com.android.support', module: 'support-v4'
    }

    compile ('com.squareup.okhttp3:okhttp:3.3.1') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile ('com.squareup.okhttp3:logging-interceptor:3.3.1') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile ('com.squareup.okio:okio:1.8.0') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile ('com.squareup.picasso:picasso:2.5.2') {
        exclude group: 'com.android.support', module: 'support-v4'
    }

    compile ('net.sf.biweekly:biweekly:0.4.6') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile ('com.github.tibolte:agendacalendarview:1.0.4') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile ('com.7heaven.widgets:segmentcontrol:1.14') {
        exclude group: 'com.android.support', module: 'support-v4'
    }

    compile ('com.survivingwithandroid:weatherlib:1.6.0') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    compile 'com.survivingwithandroid:weatherlib_okhttpclient:1.6.0'

    compile files('libs/FDxSDKProAndroid.jar')

    debugCompile ('com.squareup.leakcanary:leakcanary-android:1.5') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    releaseCompile ('com.squareup.leakcanary:leakcanary-android-no-op:1.5') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    testCompile ('com.squareup.leakcanary:leakcanary-android-no-op:1.5') {
        exclude group: 'com.android.support', module: 'support-v4'
    }

    compile ('com.jakewharton:butterknife:8.5.1') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    annotationProcessor ('com.jakewharton:butterknife-compiler:8.5.1') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
}

项目build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.4.0-alpha1'
        classpath "io.realm:realm-gradle-plugin:3.0.0"
//        classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.5.5'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

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

Proguard 规则:

#DEBUGGING
#-dontobfuscate
#-dontoptimize
###############
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-adaptresourcefilenames

-keep class com.aliakseipilko.signoutsystem.** { *; }
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.billing.IInAppBillingService
-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.content.Context {
    public void *(android.view.View);
    public void *(android.view.MenuItem);
}
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-dontwarn sun.misc.Unsafe

-dontwarn okio.**
-dontwarn com.squareup.okhttp.**
-dontwarn biweekly.io.**
-dontwarn com.github.tibolte.**

-keep class com.github.tibolte.**
-keep class okio.**
-keep class biweekly.io.**
-keep class com.squareup.okhttp.**
-keep class SecuGen.FDxSDKPro.**
-keep class com.survivingwithandroid.**

-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-dontwarn android.support.**

-keep public class android.support.v7.widget.** { *; }
-keep public class android.support.v7.internal.widget.** { *; }
-keep public class android.support.v7.internal.view.menu.** { *; }

-keep public class * extends android.support.v4.view.ActionProvider {
    public <init>(android.content.Context);
}

-keepclasseswithmembernames class * {
    native <methods>;
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keep class *.R

-keepclasseswithmembers class **.R$* {
    public static <fields>;
}

# Retain generated class which implement Unbinder.
-keep public class * implements butterknife.Unbinder { public <init>(**, android.view.View); }

# Prevent obfuscation of types which use ButterKnife annotations since the simple name
# is used to reflectively look up the generated ViewBinding.
-keep class butterknife.*
-keepclasseswithmembernames class * { @butterknife.* <methods>; }
-keepclasseswithmembernames class * { @butterknife.* <fields>; }

如有任何帮助,我们将不胜感激。

最佳答案

问题是 butterknife 是使用更高支持版本 (25.1.0) 编译的,因此您可能需要降级 butterknife 或将所有支持库移动到 25.1.0

这里我已经将 butterknife 降级到 8.0.1 :

dependencies {
    compile('com.google.api-client:google-api-client-android:1.22.0') {
        exclude module: 'httpclient'
    }
    compile('com.google.apis:google-api-services-sheets:v4-rev19-1.22.0') {
        exclude module: 'httpclient'
    }

    compile 'com.google.android.gms:play-services-identity:9.2.0'
    compile 'com.google.android.gms:play-services-auth:9.2.0'

    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:percent:23.4.0'

    compile 'com.squareup.okhttp3:okhttp:3.3.1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
    compile 'com.squareup.okio:okio:1.8.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'net.sf.biweekly:biweekly:0.4.6'
    compile 'com.github.tibolte:agendacalendarview:1.0.4'
    compile 'com.7heaven.widgets:segmentcontrol:1.14'
    compile 'com.survivingwithandroid:weatherlib:1.6.0'
    compile 'com.survivingwithandroid:weatherlib_okhttpclient:1.6.0'
    compile files('libs/FDxSDKProAndroid.jar')
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
    testCompile('com.squareup.leakcanary:leakcanary-android-no-op:1.5')

    compile 'com.jakewharton:butterknife:8.0.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.0.1'
}

然后在你的proguard.pro中处理警告:

 -keep class com.fasterxml.** { *; }
 -dontwarn com.fasterxml.**

 -keep class biweekly.io.json.** { *; }
 -dontwarn biweekly.io.json.**

 -dontwarn java.lang.invoke**

 -dontwarn sun.misc.Unsafe

 -dontwarn okio.**

请注意,我还启用了 multidex 以成功构建:我已将 multiDexEnabled true 添加到 defaultConfig

关于android - Proguard 重复 Zip 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42889264/

相关文章:

docker - 在 docker exec -it <container_name> gradle build 之后持久化 gradle 守护进程

Android Wear 应用程序打包失败

java - 在 Android Studio 中获取 "Gradle sync failed: connection reset"

Android (23) - Smack (4.1.8) - XMPP - "SSL23_GET_SERVER_HELLO:unknown protocol"错误

java - 带有 fragment 和自定义列表的 NullPointerException

gradle - 如何从Gradle访问公司 Artifactory 仓库?

android - 如何创建发布关闭? (安卓工作室)

java - Eclipse IDE 频繁关闭

android - 在 Jenkins 上编译 Android 项目时如何修复此 ZipException?

java - 从C++到Java-什么是Gradle