AndroidアプリをProGuard
で難読化しようとしました。しかし、この後、アプリの実行時に例外が発生します。
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)
mapping
ファイルを確認したところ、次のことがわかりました。
com.google.gson.reflect.TypeToken -> da:
私はそれが私のアプリの次のような行だと思います:
Type mapType = new TypeToken<Map<Integer, WatchedEpisodes>>(){}.getType(); // define generic type
jsData = gson.fromJson(r, mapType);
どうすればいいのかわからない? 3文字未満の変数名を使用しないでください。
UPD: 回答
答えは:この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 ----------
このプロジェクトの所有者に感謝します-> google-gson
[〜#〜] upd [〜#〜]:google/gson
には、Androidアプリケーションのproguard構成の例があります。
彼らは、この構成テンプレートを使用することを提案しています
##---------------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 ----------
最初は質問のリンクに気づかなかったので、GSONを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.** { *; }
この問題は、別の方法を使用してTypeToken
インスタンスを作成することで解決できます(パラメーター化されたタイプMap<Integer, WatchedEpisodes>
の場合)。
Type collectionType =
TypeToken.get(
$Gson$Types.newParameterizedTypeWithOwner(null,
Map.class, Integer.class, WatchedEpisodes.class)).getType();
Gsonの次のバージョン(私は2.8と思います)では、これをより簡単に入力できるようになります。
Type collectionType =
TypeToken.getParameterized(Map.class,
Integer.class,
WatchedEpisodes.class).getType();