在android库项目中停止proguard破坏注释

时间:2012-11-04 07:28:12

标签: android annotations proguard

我在游戏中使用GREE sdk作为社交内容。在导出签名的apk时,proguard在GREE sdk中搞砸了一些自定义注释。 (我在游戏中使用GREE sdk作为图书馆项目)

如果我没有添加任何proguard配置。它也模糊了GREE SDK,当我尝试安装并运行游戏时,它会让我“注入(格式sdk中的自定义注释)”缺少注释异常。

首先,我添加了这个keep语句来跳过整个GREE SDK的混淆。

-keep class net.gree.** {
  public protected private *;
}

这有助于我避免sdk代码混淆,但“注释”仍然被删除,我仍然收到相同的异常。

然后我尝试逐个添加这些-keepattributes语句,但没有人帮助我。

a) -keepattributes *Annotation*
b) -keepattributes Inject
c) -keepattributes net.gree.asdk.core.inject.Inject
d) -keepattributes @net.gree.asdk.core.inject.Inject

(d)选项抛出找不到文件/CLASSPATH OF PRJECT/net.gree.asdk.core.inject.Inject

这是我的proguard配置文件:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keepattributes *Annotation*

-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

-dontwarn org.mockito.cglib.**
-dontwarn sun.reflect.**
-dontwarn android.test.**


-keepclasseswithmembernames class * {
    native <methods>;
}

-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.app.Activity {
   public void *(android.view.View);
}

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

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keep class net.gree.** {
  public protected private *;
}

我试图摆脱的例外情况:

11-06 12:25:41.718: E/AndroidRuntime(10764): java.lang.RuntimeException: Unable to create application com.mygame.Application: java.lang.RuntimeException: Could not find a suitable constructor in net.gree.asdk.core.track.TrackItemStorageImpl. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
11-06 12:25:41.718: E/AndroidRuntime(10764):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4154)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at android.app.ActivityThread.access$1300(ActivityThread.java:130)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at android.os.Looper.loop(Looper.java:137)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at android.app.ActivityThread.main(ActivityThread.java:4745)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at java.lang.reflect.Method.invokeNative(Native Method)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at java.lang.reflect.Method.invoke(Method.java:511)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at dalvik.system.NativeStart.main(Native Method)
11-06 12:25:41.718: E/AndroidRuntime(10764): Caused by: java.lang.RuntimeException: Could not find a suitable constructor in net.gree.asdk.core.track.TrackItemStorageImpl. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
11-06 12:25:41.718: E/AndroidRuntime(10764):    at net.gree.asdk.core.inject.Binder.getConstructorOf(Unknown Source)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at net.gree.asdk.core.inject.Binder.createInstance(Unknown Source)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at net.gree.asdk.core.inject.Binder.getOrCreateInstance(Unknown Source)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at net.gree.asdk.core.inject.Binder.createInstance(Unknown Source)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at net.gree.asdk.core.inject.Binder.finishBinding(Unknown Source)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at net.gree.asdk.core.inject.InternalInjector.createInjector(Unknown Source)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at net.gree.asdk.core.Injector.init(Unknown Source)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at net.gree.asdk.core.Core.initialize(Unknown Source)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at net.gree.asdk.api.GreePlatform.initialize(Unknown Source)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at com.aqif.speedymind.application.SpeedyApplication.onCreate(Unknown Source)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
11-06 12:25:41.718: E/AndroidRuntime(10764):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)

3 个答案:

答案 0 :(得分:5)

您应该在proguard文件中添加一些配置,以使用Inject注释保留带注释的类(构造函数和字段):

 # keeps all fields and Constructors with @Inject
 -keepclassmembers,allowobfuscation class * {
    @com.google.inject.Inject <fields>;
    @com.google.inject.Inject <init>(...);
 }

答案 1 :(得分:3)

这对我有用

################### GREE SDK ##################
-keepclassmembers,allowobfuscation class * {
    @net.gree.asdk.core.inject.Inject <fields>;
    @net.gree.asdk.core.inject.Inject <init>(...);
}


##################### GSON #####################
-keepattributes Signature
-keep class sun.misc.Unsafe { *; }

 # Model classes parsed by GSON
-keep class net.gree.asdk.** {
    public protected private *;
}

它类似于另一个响应,但使用GREE特定注释。

答案 2 :(得分:0)

而不是这样做     -keep class net.gree。** {       公共保护私人*;     }

我必须这样做

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