Android compile ClassCastException with Proguard

2019-08-02 17:24发布

问题:

I'm trying to build a project with proguard and I'm getting a compile time ClassCastException Error, having solved every notes and warnings.

Been trying to solve this for quite a few time and can't find out what can be happening.

The Project SDK is Android 4.4.2.

The proguard file:

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

-printconfiguration out_config.txt

-verbose

-keepattributes Signature
-keepattributes *Annotation*
-keepattributes Exceptions,InnerClasses,Signature,Deprecated, SourceFile,LineNumberTable,EnclosingMethod,SourceFile,LineNumberTable
-renamesourcefileattribute ProGuard

-dontskipnonpubliclibraryclasses



-keep class * extends java.util.ListResourceBundle {
    protected java.lang.Object[][] getContents();
}

# Keep SafeParcelable value, needed for reflection. This is required to support backwards
# compatibility of some classes.
#-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
#    public static final *** NULL;
#}

# Keep the names of classes/members we need for client functionality.
#-keepnames @com.google.android.gms.common.annotation.KeepName class *
#-keepclassmembernames class * {
#    @com.google.android.gms.common.annotation.KeepName *;
#}

# Needed for Parcelable/SafeParcelable Creators to not get stripped
-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

-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);
    public void set*(...);
}

-keep class **.R
-keep class **.R$* {<fields>;}


-keep class com.google.**
-dontwarn com.google.**

-keep class com.android.**
-dontwarn com.android.**

-dontnote com.google.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService

-keep class sun.misc.Unsafe { *; }
-dontnote sun.misc.Unsafe

-dontnote com.android.org.conscrypt.OpenSSLSocketImpl
-dontnote com.squareup.okhttp.OkUrlFactory

#-keep public class com.android.vending.licensing.ILicensingService
########################################################
#Android Support
-keep interface android.support.** { *; }
-keep class android.support.** { *; }
-dontnote android.support.**
########################################################


########################################################
# Apache
-dontwarn org.apache.**
-dontnote org.apache.**


########################################################

########################################################
# Picasso
-dontwarn com.squareup.**
########################################################

########################################################
#Joda Time
-dontwarn org.joda.**
-keep class org.joda.** {*;}
########################################################

########################################################
#Simple XML
-keep public class org.simpleframework.**{ *; }
-keep class org.simpleframework.xml.**{ *; }
-keep class org.simpleframework.xml.core.**{ *; }
-keep class org.simpleframework.xml.util.**{ *; }
-dontwarn javax.xml.stream.**

########################################################

########################################################
#HListView

-keep class it.sephiroth.**
########################################################

########################################################
#Rounded Image View
-keep class com.makeramen.RoundedImageView
-dontwarn com.makeramen.RoundedImageView

########################################################

-keep class android.view.View.**

The result while compiling

ProGuard: [Project] ProGuard, version 4.7
...
    Compilation completed with 20 errors and 0 warnings in 18 sec
    20 errors
    0 warnings
    ProGuard: [Project] java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String
    ProGuard: [Project]     at proguard.obfuscate.MemberObfuscator.newMemberName(MemberObfuscator.java:198)
    ProGuard: [Project]     at proguard.obfuscate.MemberNameCollector.visitAnyMember(MemberNameCollector.java:74)
    ProGuard: [Project]     at proguard.classfile.util.SimplifiedVisitor.visitProgramMember(SimplifiedVisitor.java:79)
    ProGuard: [Project]     at proguard.classfile.util.SimplifiedVisitor.visitProgramMethod(SimplifiedVisitor.java:91)
    ProGuard: [Project]     at proguard.classfile.visitor.MemberAccessFilter.visitProgramMethod(MemberAccessFilter.java:90)
    ProGuard: [Project]     at proguard.classfile.ProgramMethod.accept(ProgramMethod.java:71)
    ProGuard: [Project]     at proguard.classfile.ProgramClass.methodsAccept(ProgramClass.java:504)
    ProGuard: [Project]     at proguard.classfile.visitor.AllMemberVisitor.visitProgramClass(AllMemberVisitor.java:48)
    ProGuard: [Project]     at proguard.classfile.ProgramClass.accept(ProgramClass.java:346)
    ProGuard: [Project]     at proguard.classfile.ProgramClass.hierarchyAccept(ProgramClass.java:359)
    ProGuard: [Project]     at proguard.classfile.LibraryClass.hierarchyAccept(LibraryClass.java:371)
    ProGuard: [Project]     at proguard.classfile.ProgramClass.hierarchyAccept(ProgramClass.java:416)
    ProGuard: [Project]     at proguard.classfile.visitor.ClassHierarchyTraveler.visitProgramClass(ClassHierarchyTraveler.java:75)
    ProGuard: [Project]     at proguard.classfile.visitor.MultiClassVisitor.visitProgramClass(MultiClassVisitor.java:85)
    ProGuard: [Project]     at proguard.classfile.ClassPool.classesAccept(ClassPool.java:116)
    ProGuard: [Project]     at proguard.obfuscate.Obfuscator.execute(Obfuscator.java:217)
    ProGuard: [Project]     at proguard.ProGuard.obfuscate(ProGuard.java:333)
    ProGuard: [Project]     at proguard.ProGuard.execute(ProGuard.java:135)
    ProGuard: [Project]     at proguard.ProGuard.main(ProGuard.java:492)

回答1:

I was finally able to solve it.

The issue was with a class that extended an external library, in this case apache. In my proguard config I added a -keep class notation to this particular class.

import org.apache.http.client.methods.HttpPost;

public class ProjectHttpDelete extends HttpPost {
    public ProjectHttpDelete(String url) {
        super(url);
    }

    @Override
    public String getMethod() {
        return "DELETE";
    }

}


回答2:

I resolved it by doing "-keep" for each class which is being "-dontwarn":

-dontwarn android.**
-keep class android.**

-dontwarn com.android.**
-keep class com.android.**

-dontwarn com.google.**
-keep class com.google.**