在Android中混淆后找不到匿名类

时间:2013-07-02 09:28:22

标签: android obfuscation anonymous-class

我需要一些来自Javascript的项目,所以我使用Gson将JSON字符串转换为值,如下面的代码所示。

来自Java脚本:

 var  Items = [{ "ItemName": "Item1", "ItemPos": "1", "ItemValue":"value1"},{ "ItemName": "Item2", "ItemPos": "2", "ItemValue":"value2"}];

调用原生Android方法将这些值传递为 JSON.parse(items)

在Native类中,我检索了以下值,

 List<Items> ItemsList; 
 Gson gson = new Gson();
 ItemsList= gson.fromJson(args[0].toString(), new TypeToken<List<Items>>(){}.getType());

我已将Item类声明如下。

  public class Items
   {
    public String Name;
    public String Position;
    public String Value;

  }

这些实现方法在本机源中运行良好。但是在对源进行混淆之后,这些实现方法无法正常工作。我收到“查找失败”错误。在反编译混淆代码时,Item类不存在于该位置“找不到匿名类”。甚至 List ItemsList的声明也不存在。如何解决这个问题?

编辑1: MyProguard.pro

-injars       input.jar
-outjars      output.jar
-libraryjars  android.jar
-libraryjars  android-support-v4.jar
-libraryjars  annotations.jar
-libraryjars  gson-2.2.1.jar


-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

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

-keep public class * {
  public protected *;
 }

-keepclassmembernames class * {
  java.lang.Class class$(java.lang.String);
  java.lang.Class class$(java.lang.String, boolean);
}

-keepclasseswithmembernames class * {
   native <methods>;
}

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

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
 }

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

 -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 class * implements android.os.Parcelable {
      static android.os.Parcelable$Creator CREATOR;
   }

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

我通过“ java -jar proguard.jar @ MyProguard.pro ”对来源进行了模糊处理

2 个答案:

答案 0 :(得分:0)

I think that compiler may ignore the item class during Obfuscating

you must manually mention your missed class name in your proguard.txt file like as follows

-keep public class <your_class_name>

请参阅此链接here

它可能对你有帮助

答案 1 :(得分:0)

尝试添加:

-keep class com.xxx.yyy$Items { *; }