使用 google-play-services 进行 Proguard

Posted

技术标签:

【中文标题】使用 google-play-services 进行 Proguard【英文标题】:Proguard with google-play-services 【发布时间】:2014-05-18 00:14:44 【问题描述】:

我有一个 android 项目 (target=Google APIs:17)。 我正在使用 Proguard,一切正常,直到我添加 google-play-services 库作为依赖项。 现在我收到以下错误:

java.lang.IllegalArgumentException: Value is not a reference value [proguard.evaluation.value.InstructionOffsetValue]
at proguard.evaluation.value.Value.referenceValue(Value.java:97)
at proguard.optimize.evaluation.SimpleEnumUseSimplifier.isPoppingSimpleEnum(SimpleEnumUseSimplifier.java:530)
at proguard.optimize.evaluation.SimpleEnumUseSimplifier.isPoppingSimpleEnum(SimpleEnumUseSimplifier.java:520)
at proguard.optimize.evaluation.SimpleEnumUseSimplifier.visitVariableInstruction(SimpleEnumUseSimplifier.java:229)
at proguard.classfile.instruction.VariableInstruction.accept(VariableInstruction.java:306)
at proguard.optimize.evaluation.SimpleEnumUseSimplifier.visitCodeAttribute(SimpleEnumUseSimplifier.java:131)
at proguard.classfile.attribute.CodeAttribute.accept(CodeAttribute.java:101)
at proguard.classfile.ProgramMethod.attributesAccept(ProgramMethod.java:79)
at proguard.classfile.attribute.visitor.AllAttributeVisitor.visitProgramMember(AllAttributeVisitor.java:95)
at proguard.classfile.util.SimplifiedVisitor.visitProgramMethod(SimplifiedVisitor.java:91)
at proguard.classfile.ProgramMethod.accept(ProgramMethod.java:71)
at proguard.classfile.ProgramClass.methodsAccept(ProgramClass.java:504)
at proguard.classfile.visitor.AllMethodVisitor.visitProgramClass(AllMethodVisitor.java:47)
at proguard.classfile.ProgramClass.accept(ProgramClass.java:346)
at proguard.classfile.ClassPool.classesAccept(ClassPool.java:124)
at proguard.optimize.Optimizer.execute(Optimizer.java:373)
at proguard.ProGuard.optimize(ProGuard.java:306)
at proguard.ProGuard.execute(ProGuard.java:115)
at proguard.ProGuard.main(ProGuard.java:483)

我什么都试过了,什么都没发生:(

这是我的 proguard cfg 文件:

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


-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable

-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 android.support.**
-dontwarn org.apache.commons.codec.binary.**
-dontwarn com.google.common.collect.Maps

-keepclassmembers class * 
    native <methods>;


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


-keepclassmembers 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 android.support.v4.app.**  *; 
-keep interface android.support.v4.app.**  *; 
-keep class com.actionbarsherlock.**  *; 
-keep interface com.actionbarsherlock.**  *; 

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


-keep class **.R$*

-keepattributes *Annotation*

-keep class com.tjeannin.apprate.** 
   *;



-dump class_files.txt 
-printseeds seeds.txt 
-printusage unused.txt 
-printmapping mapping.txt 

-keep class com.aviary.android.feather.headless.AviaryEffect
-keep class com.aviary.android.feather.headless.media.ExifInterfaceWrapper
-keep class com.aviary.android.feather.headless.moa.Moa
-keep class com.aviary.android.feather.headless.moa.MoaResult
-keep class com.aviary.android.feather.headless.moa.MoaHD
-keep class com.aviary.android.feather.headless.filters.NativeFilterProxy
-keep class com.aviary.android.feather.headless.utils.CameraUtils
-keep class com.aviary.android.feather.headless.gl.GLUtils

-keep class com.aviary.android.feather.opengl.AviaryGLSurfaceView
-keep class com.aviary.android.feather.widget.ScrollerRunnable
-keep class com.aviary.android.feather.library.services.BaseContextService
-keep class it.sephiroth.android.library.imagezoom.easing.Easing
-keep class com.aviary.android.feather.library.external.tracking.TrackerFactory
-keep class com.aviary.android.feather.library.tracking.AbstractTracker

-keep interface com.aviary.android.feather.library.services.IAviaryController
-keep interface com.aviary.android.feather.headless.filters.IFilter
-keep interface com.aviary.android.feather.widget.ScrollerRunnable$ScrollableView

-keep class * extends com.aviary.android.feather.library.tracking.AbstractTracker
-keep class * extends com.aviary.android.feather.headless.filters.IFilter
-keep class * extends com.aviary.android.feather.headless.filters.INativeFilter
-keep class * implements com.aviary.android.feather.library.services.IAviaryController
-keep class * extends com.aviary.android.feather.library.services.BaseContextService
-keep class * implements com.aviary.android.feather.widget.ScrollerRunnable$ScrollableView  *; 
-keep class * implements java.lang.Runnable

-keepclasseswithmembers class * 
    protected <init>( com.aviary.android.feather.library.services.BaseContextService );


-keepclasseswithmembers class * 
    public <init>( com.aviary.android.feather.library.services.IAviaryController );


-keepclassmembers class com.aviary.android.feather.library.tracking.AbstractTracker  *; 

-keepclassmembers interface it.sephiroth.android.library.imagezoom.easing.Easing  *; 

-keepclassmembers class com.aviary.android.feather.library.external.tracking.TrackerFactory  *; 

-keepclassmembers class * implements it.sephiroth.android.library.imagezoom.easing.Easing  *; 

# This class should not be obfuscated at all
-keepclassmembers class com.aviary.android.feather.headless.moa.MoaResult 
    public java.lang.String inputString;
    public android.graphics.Bitmap inputBitmap;
    public java.lang.String outputString;
    public android.graphics.Bitmap outputBitmap;
    public volatile int active;
    public void cancel();
    public synchronized void execute();


# Keep all classes with a native method

-keepclassmembers class * 
   public static native <methods>;
   private static native <methods>;
   static native <methods>;
   private native <methods>;
   native <methods>;


-keepattributes Signature

请帮我解决这个问题,

谢谢。

【问题讨论】:

【参考方案1】:

如果您使用 Android Studio,则包含 ProGuard 指令

注意:ProGuard 指令包含在 Play 服务客户端库中以保留所需的类。 Gradle 的 Android 插件会自动将 ProGuard 配置文件附加到 AAR (Android ARchive) 包中,并将该包附加到您的 ProGuard 配置中。在项目创建期间,Android Studio 会自动创建 ProGuard 配置文件和 build.gradle 属性以供 ProGuard 使用。要将 ProGuard 与 Android Studio 结合使用,您必须在 build.gradle buildTypes 中启用 ProGuard 设置。有关详细信息,请参阅 ProGuard 主题。

参考:Setting Up Google Play Services

【讨论】:

我到底该怎么做?似乎文本说 proguard 规则是通过 gradle 自动设置的,但这不会发生在我身上? 它仍然失败***.com/q/55329060/9766649,直到我手动执行-keepnames class com.google.android.gms.** *;【参考方案2】:

您没有包含 Google Play Service Proguard 例外: http://developer.android.com/google/play-services/setup.html#Proguard

【讨论】:

我该如何添加这个例外?似乎只是说异常是自动添加的? 答案是从 2014 年开始的。当时使用 Eclipse 必须自己添加异常。今天情况并非如此,在合并模块和依赖项时应该自动添加异常。此外,Android Studio 在每个名为“proguard-rules.pro”的新模块中创建一个 proguard 配置文件。您可以在那里添加手动例外。 所以问题仍然存在***.com/q/55329060/9766649 所以 1-2 周前一切都很好

以上是关于使用 google-play-services 进行 Proguard的主要内容,如果未能解决你的问题,请参考以下文章

Google-Play-Services:创建自定义候诊室 UI

将 google-play-services 库导入 Intellij IDEA 12(和 13)

导入 google-play-service 库,在此参考 android 旁边显示一个红色 X

为啥 Localytics 需要 google-play-services:ads?

串联使用 Google SettingsAPI 和 PermissionModel API

admob广告后如何减小应用程序大小?