web-dev-qa-db-fra.com

paramètre de type manquant

j'essaie de masquer mon code d'application Android avec ProGuard. Mais après cela, mon application donne une exception lors de l'exécution:

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)

J'ai vérifié un fichier mapping et trouvé ceci:

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

Je pense que les lignes de mon application ressemblent à:

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

Je ne peux pas comprendre quelles conclusions dois-je faire? Ne pas utiliser un nom de variable inférieur à trois caractères ou quoi?

UPD: répondre

30
Mrusful

la réponse est: utilisez ce 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  ----------

Un grand merci au propriétaire de ce projet -> google-gson


UPD: google/gson a son exemple de configuration de proguard pour les applications Android.

voir sur github

Ils proposent d'utiliser ce modèle de 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  ----------
59
Mrusful

Comme je n'avais pas remarqué le lien dans la question au début, voici les lignes spécifiques mentionnées dans le fil de discussion nécessaire pour que GSON fonctionne avec 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.** { *; }
43
koljaTM

Ce problème peut être résolu en utilisant un moyen différent de créer l'instance TypeToken (pour le type paramétré Map<Integer, WatchedEpisodes>):

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

La prochaine version de gson (je suppose 2.8), vous permettra de taper ceci plus facilement:

Type collectionType =
  TypeToken.getParameterized(Map.class,
                             Integer.class,
                             WatchedEpisodes.class).getType();
0
T. Neidhart