35

i try obfuscate my code of android app with ProGuard. But after this my app give exception at running:

11-15 01:46:26.818: W/System.err(21810): java.lang.RuntimeException: Missing type parameter.
11-15 01:46:26.828: W/System.err(21810):    at da.<init>(Unknown Source)
11-15 01:46:26.828: W/System.err(21810):    at gc.<init>(Unknown Source)
11-15 01:46:26.828: W/System.err(21810):    at fx.f(Unknown Source)
11-15 01:46:26.828: W/System.err(21810):    at com.yourshows.activity.UnwatchedActivity.onResume(Unknown Source)

I checked a mapping file and found this:

com.google.gson.reflect.TypeToken -> da:

I think it's lines in my app like:

        Type mapType = new TypeToken<Map<Integer, WatchedEpisodes>>(){}.getType(); // define generic type
        jsData = gson.fromJson(r, mapType);

I can not understand what conclusions should I do? Do not use variable name less then three characters or what?

UPD: answer

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Mrusful
  • 1,503
  • 1
  • 18
  • 32
  • You can post an answer of your own, because questions like these may be automatically deleted. – Reno Nov 15 '11 at 02:02

3 Answers3

64

answer is: use this proguard.cfg

##---------------Begin: proguard configuration common for all Android apps ----------
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-dump class_files.txt 
-printseeds seeds.txt 
-printusage unused.txt 
-printmapping mapping.txt 
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*


-allowaccessmodification
-keepattributes *Annotation*
-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.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService


# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
-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();
}


# Preserve all native method names and the names of their classes.
-keepclasseswithmembernames class * {
    native <methods>;
}


-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}


-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}


# Preserve static fields of inner classes of R classes that might be accessed
# through introspection.
-keepclassmembers class **.R$* {
  public static <fields>;
}


# Preserve the special static methods that are required in all enumeration classes.
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}


-keep public class * {
    public protected *;
}


-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
##---------------End: proguard configuration common for all Android apps ----------


##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature


# 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 com.google.gson.examples.android.model.** { *; }


##---------------End: proguard configuration for Gson  ----------

Big thanks to owner this project --> google-gson


UPD: google/gson has their example of proguard configuration for android applications.

see on github

They propose to use this template of configuration

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-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 com.google.gson.examples.android.model.** { *; }

##---------------End: proguard configuration for Gson  ----------
Mrusful
  • 1,503
  • 1
  • 18
  • 32
  • lint says: use -keepclasseswithmembers instead of -keepclasseswithmembernames – Shine May 13 '13 at 22:19
  • 2
    I really don't know why but I have copied all this and pasted to proguard-project.txt but the same error still occurred! – huong Aug 10 '13 at 09:32
  • I don't know what proguard-project.txt is. But i use this settings in `proguard.cfg`. Maybe you used not correct file for settings up your obfuscator? Try to ask a new question with more detail. – Mrusful Aug 10 '13 at 20:24
  • @Mrusful I obfuscate my code with Eclipse that's why the config is saved in proguard-project.txt in default. And here is my question http://stackoverflow.com/questions/18168388/android-parsing-gson-not-working-after-obfuscating – huong Aug 12 '13 at 02:23
44

Since I didn't notice the link in the question at first, here are the specific lines mentioned in the thread needed to get GSON to work with Proguard:

# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# 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 com.google.gson.examples.android.model.** { *; }
koljaTM
  • 10,064
  • 2
  • 40
  • 42
3

This issue can be solved by using a different way to create the TypeToken instance (for the parameterized type Map<Integer, WatchedEpisodes>):

Type collectionType = 
  TypeToken.get(
    $Gson$Types.newParameterizedTypeWithOwner(null,
      Map.class, Integer.class, WatchedEpisodes.class)).getType();

The next version of gson (I assume 2.8), will allow you to type this more easily:

Type collectionType =
  TypeToken.getParameterized(Map.class,
                             Integer.class,
                             WatchedEpisodes.class).getType();
T. Neidhart
  • 6,060
  • 2
  • 15
  • 38
  • I was having obfuscation problems recently, when I upgraded my Gradle Plugin from `7.x` to `8.x`. Changing from `new TypeToken...` to `TypeToken.getParameterized...` solved these issues! – Ícaro Jul 20 '23 at 19:00