gson混淆程序错误

时间:2013-10-01 09:13:20

标签: android gson obfuscation proguard classcastexception

所以我在这里和其他地方阅读了几千条线程,它仍然不适合我。 它的错误就像找不到引用类sun.misc.Unsafe,或者如果我在那些上使用dontwarn,它会编译,但gson部分会在某些时候返回ClassCastExecption。

现在 - 我知道序列化类shuold在proguard设置中被标记为-keep,我知道-keepattributes Annotation 和Signature,但是我仍然无法正确地混淆我的代码。

附上我的proguard-project.txt

-optimizationpasses 2
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic 

-allowaccessmodification
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-repackageclasses ''

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

-keepattributes Signature

-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class my.package.model.** { *; }

-keep class com.google.gson.** { *; }

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

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

-keep class net.sqlcipher.** {
    *;
}

-assumenosideeffects class android.util.Log {
    public static *** e(...);
    public static *** w(...);
    public static *** wtf(...);
    public static *** d(...);
    public static *** v(...);
    public static *** i(...);
}

编辑1 我还检查了这个gson example proguard settings

1 个答案:

答案 0 :(得分:0)

显然-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,Annotation,EnclosingMethod sorta帮助。我不知道为什么,虽然我没有从输出中删除Logs,它只是变成了StringBuffer lol。

编辑1 虽然我已经在ant build.xml脚本中添加了一个目标,如果有人需要它就像这样:

<target name="-commentoutlogs">
    <replaceregexp match="(Log\..*?;\s*\n)" replace="/*\1*/" flags="gs" byline="false">
        <fileset dir="src">
            <include name="**/*.java"/>
        </fileset>
    </replaceregexp>
</target>

并取消注释:

    <target name="-uncommentlogsafter">
    <replaceregexp match="\/\*(Log\..*?;\s*\n)\*\/" replace="\1" flags="gs" byline="false">
        <fileset dir="src">
            <include name="**/*.java"/>
        </fileset>
    </replaceregexp>
</target>

所以你可以将它添加到类似的发布目标中,以便它为你修复这些东西并且日志被很好地评论到

/*Log.<blablabla> );
*/