gson 和 jackson 的 Proguard 问题

Posted

技术标签:

【中文标题】gson 和 jackson 的 Proguard 问题【英文标题】:Proguard Issue with gson and jackson 【发布时间】:2016-02-11 02:30:49 【问题描述】:

我没有在我的项目中使用杰克逊。我只使用 gson 库,当我尝试构建发布 apk 时,proguard 显示很多警告。

以下是警告:-

Warning:org.immutables.gson.adapter.ExpectedSubtypesAdapter$JsonParserReaderSupplier: can't find referenced class com.fasterxml.jackson.databind.util.TokenBuffer
Warning:org.immutables.gson.adapter.FieldNamingTranslator: can't find referenced class sun.reflect.ReflectionFactory
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$GsonProviderOptions: can't find referenced class org.immutables.value.Value$Immutable
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$GsonProviderOptions: can't find referenced class org.immutables.value.Value$Style
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$GsonProviderOptions: can't find referenced class org.immutables.value.Value$Style$ImplementationVisibility
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$GsonProviderOptions: can't find referenced class org.immutables.value.Value
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$GsonProviderOptions: can't find referenced class org.immutables.value.Value$Immutable
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$GsonProviderOptions: can't find referenced class org.immutables.value.Value$Style
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$GsonProviderOptions: can't find referenced class org.immutables.value.Value$Style$ImplementationVisibility
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonFactory
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonGenerator
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonFactory
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonParser$Feature
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonFactory
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonGenerator$Feature
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonFactory
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonGenerator
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonParser$Feature
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonGenerator$Feature
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonParser
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonFactory
Warning:org.immutables.gson.stream.GsonMessageBodyProvider$JacksonStreamer: can't find referenced class com.fasterxml.jackson.core.JsonGenerator
Warning:org.immutables.gson.stream.JsonGeneratorWriter: can't find referenced class com.fasterxml.jackson.core.JsonGenerator
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonParser
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonToken
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonParser
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonToken
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonParser
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonToken
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonParser
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonToken
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.databind.util.TokenBuffer
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonToken
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonParser
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonToken
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonParser
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonToken
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonParser
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonToken
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonParser
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.databind.util.TokenBuffer
Warning:org.immutables.gson.stream.JsonParserReader: can't find referenced class com.fasterxml.jackson.core.JsonParser
Warning:org.immutables.gson.stream.JsonParserReader$2: can't find referenced class com.fasterxml.jackson.core.JsonToken
Warning:org.parceler.ParcelAnnotationProcessor: can't find referenced method 'void init(javax.annotation.processing.ProcessingEnvironment)' in program class org.parceler.transfuse.AnnotationProcessorBase
Warning:org.parceler.ParcelAnnotationProcessor: can't find referenced class com.google.auto.service.AutoService
Warning:org.parceler.apache.commons.beanutils.MappedPropertyDescriptor: can't find referenced method 'void setName(java.lang.String)' in program class org.parceler.apache.commons.beanutils.MappedPropertyDescriptor
Warning:org.parceler.apache.commons.logging.impl.AvalonLogger: can't find referenced class org.parceler.apache.avalon.framework.logger.Logger
Warning:org.parceler.apache.commons.logging.impl.Log4JLogger: can't find referenced class org.parceler.apache.log4j.Category
Warning:org.parceler.apache.commons.logging.impl.Log4JLogger: can't find referenced class org.parceler.apache.log4j.Priority
Warning:org.parceler.apache.commons.logging.impl.Log4JLogger: can't find referenced class org.parceler.apache.log4j.Logger
Warning:org.parceler.apache.commons.logging.impl.Log4JLogger: can't find referenced class org.parceler.apache.log4j.Category
Warning:org.parceler.apache.commons.logging.impl.Log4JLogger: can't find referenced class org.parceler.apache.log4j.Logger
Warning:org.parceler.apache.commons.logging.impl.Log4JLogger: can't find referenced class org.parceler.apache.log4j.Priority
Warning:org.parceler.apache.commons.logging.impl.Log4JLogger: can't find referenced class org.parceler.apache.log4j.Logger
Warning:org.parceler.apache.commons.logging.impl.LogKitLogger: can't find referenced class org.parceler.apache.log.Hierarchy
Warning:org.parceler.apache.commons.logging.impl.LogKitLogger: can't find referenced class org.parceler.apache.log.Logger
Warning:org.parceler.codemodel.util.SingleByteEncoder: can't find referenced class sun.nio.cs.Surrogate$Parser
Warning:org.parceler.codemodel.util.SingleByteEncoder: can't find referenced class sun.nio.cs.Surrogate
Warning:org.parceler.codemodel.util.SingleByteEncoder: can't find referenced class sun.nio.cs.Surrogate$Parser
Warning:org.parceler.codemodel.util.SingleByteEncoder: can't find referenced class sun.nio.cs.Surrogate
Warning:org.parceler.codemodel.util.SingleByteEncoder: can't find referenced class sun.nio.cs.Surrogate$Parser
Warning:org.parceler.guava.cache.Striped64: can't find referenced class sun.misc.Unsafe
Warning:org.parceler.guava.cache.Striped64$1: can't find referenced class sun.misc.Unsafe
Warning:org.parceler.guava.cache.Striped64$Cell: can't find referenced class sun.misc.Unsafe
Warning:org.parceler.guava.primitives.UnsignedBytes$LexicographicalComparatorHolder$UnsafeComparator: can't find referenced class sun.misc.Unsafe
Warning:org.parceler.guava.primitives.UnsignedBytes$LexicographicalComparatorHolder$UnsafeComparator$1: can't find referenced class sun.misc.Unsafe
Warning:org.parceler.transfuse.bootstrap.BootstrapProcessor: can't find referenced field 'javax.annotation.processing.ProcessingEnvironment processingEnv' in program class org.parceler.transfuse.bootstrap.BootstrapProcessor
Warning:org.parceler.transfuse.gen.FilerResourceWriter: can't find referenced class javax.tools.JavaFileManager$Location
Warning:org.parceler.transfuse.gen.FilerResourceWriter: can't find referenced class javax.tools.JavaFileManager
Warning:org.parceler.transfuse.gen.FilerResourceWriter: can't find referenced class javax.tools.StandardLocation
Warning:org.parceler.transfuse.gen.FilerResourceWriter: can't find referenced class javax.tools.FileObject
Warning:org.parceler.transfuse.gen.FilerSourceCodeWriter: can't find referenced class javax.tools.JavaFileObject
Warning:org.parceler.transfuse.util.MessagerLogger: can't find referenced class javax.tools.Diagnostic$Kind
Warning:org.parceler.transfuse.util.MessagerLogger: can't find referenced class javax.tools.Diagnostic
Warning:org.parceler.transfuse.util.MessagerLogger: can't find referenced class javax.tools.Diagnostic$Kind
Warning:org.parceler.transfuse.validation.Validator: can't find referenced class javax.tools.Diagnostic$Kind
Warning:org.parceler.transfuse.validation.Validator: can't find referenced class javax.tools.Diagnostic
Warning:org.parceler.transfuse.validation.Validator: can't find referenced class javax.tools.Diagnostic$Kind
Warning:org.parceler.transfuse.validation.Validator$ValidationBuilder: can't find referenced class javax.tools.Diagnostic$Kind
Warning:org.parceler.transfuse.validation.Validator$ValidationBuilder: can't find referenced class javax.tools.Diagnostic
Warning:org.parceler.transfuse.validation.Validator$ValidationBuilder: can't find referenced class javax.tools.Diagnostic$Kind

这是我完整的 proguard 文件

# Apache
-keep class org.apache.http.**  *; 
-dontwarn org.apache.http.**
-dontwarn android.net.**

# ButterKnife 7

-keep class butterknife.**  *; 
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder  *; 

-keepclasseswithmembernames class * 
    @butterknife.* <fields>;


-keepclasseswithmembernames class * 
    @butterknife.* <methods>;


# Crashlytics 1.+

-keep class com.crashlytics.**  *; 
-keepattributes SourceFile,LineNumberTable

# Deeplink dispatch
-keep class com.airbnb.deeplinkdispatch.**  *; 
-keepclasseswithmembers class *  @com.airbnb.deeplinkdispatch.DeepLink <methods>;

# Configuration for Fabric Twitter Kit
# See: https://dev.twitter.com/twitter-kit/android/integrate

-dontwarn com.squareup.okhttp.**
-dontwarn com.google.appengine.api.urlfetch.**
-dontwarn rx.**
-dontwarn retrofit.**
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.**  *; 
-keep interface com.squareup.okhttp.**  *; 
-keep class retrofit.**  *; 
-keepclasseswithmembers class * 
    @retrofit.http.* *;


# Facebook 3.2

-keep class com.facebook.**  *; 
-keepattributes Signature

## Google Play Services 4.3.23 specific rules ##
## https://developer.android.com/google/play-services/setup.html#Proguard ##

-keep class * extends java.util.ListResourceBundle 
    protected Object[][] getContents();


-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable 
    public static final *** NULL;


-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * 
    @com.google.android.gms.common.annotation.KeepName *;


-keepnames class * implements android.os.Parcelable 
    public static final ** CREATOR;


## GSON 2.2.4 specific rules ##

# 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*

-keepattributes EnclosingMethod

# Gson specific classes
-keep class sun.misc.Unsafe  *; 
-keep class com.google.gson.stream.**  *; 

# MoPub Proguard Config
# NOTE: You should also include the Android Proguard config found with the build tools:
# $ANDROID_HOME/tools/proguard/proguard-android.txt

# Keep public classes and methods.
-keepclassmembers class com.mopub.**  public *; 
-keep public class com.mopub.**
-keep public class android.webkit.javascriptInterface 

# Explicitly keep any custom event classes in any package.
-keep class * extends com.mopub.mobileads.CustomEventBanner 
-keep class * extends com.mopub.mobileads.CustomEventInterstitial 
-keep class * extends com.mopub.nativeads.CustomEventNative 
-keep class * extends com.mopub.mobileads.GooglePlayServicesBanner 
-keep class * extends com.mopub.nativeads.AvocarrotNativeMopub 

# Support for Android Advertiser ID.
-keep class com.google.android.gms.common.GooglePlayServicesUtil *;
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient *;
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient$Info *;

# Simple-fb
-keep class com.sromku.simple.fb.entities.**  *; 

# OkHttp
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.**  *; 
-keep interface com.squareup.okhttp.**  *; 
-dontwarn com.squareup.okhttp.**

## Square Otto specific rules ##
## https://square.github.io/otto/ ##

# Otto
-keepattributes *Annotation*
-keepclassmembers class ** 
    @com.squareup.otto.Subscribe public *;
    @com.squareup.otto.Produce public *;


## Square Picasso specific rules ##
## https://square.github.io/picasso/ ##

-dontwarn com.squareup.okhttp.**

# Retrofit 1.X

-keep class com.squareup.okhttp.**  *; 
-keep class retrofit.**  *; 
-keep interface com.squareup.okhttp.**  *; 

-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
-dontwarn retrofit.**
-dontwarn rx.**

-keepclasseswithmembers class * 
    @retrofit.http.* <methods>;


# If in your rest service interface you use methods with Callback argument.
-keepattributes Exceptions

# If your rest service methods throw custom exceptions, because you've defined an ErrorHandler.
-keepattributes Signature

# Also you must note that if you are using GSON for conversion from JSON to POJO representation, you must ignore those POJO classes from being obfuscated.
# Here include the POJO's that have you have created for mapping JSON response to POJO for example.

-keep class com.myproject.model.**  *; 

# Android design
-dontwarn android.support.design.**
-keep class android.support.design.**  *; 
-keep interface android.support.design.**  *; 
-keep public class android.support.design.R$*  *; 

-keep public class android.support.v7.widget.**  *; 
-keep public class android.support.v7.internal.widget.**  *; 
-keep public class android.support.v7.internal.view.menu.**  *; 

-keep public class * extends android.support.v4.view.ActionProvider 
    public <init>(android.content.Context);


# app specific
-keepclassmembers class com.myproject.** 
   public *;


# Removing logs
-assumenosideeffects class android.util.Log 
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);


# Keep the support library

-keep class android.support.v4.**  *; 
-keep interface android.support.v4.**  *; 

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**
## ---------------- End Project specifics ---------------- ##

请帮忙,如何解决。

【问题讨论】:

你能发布你的 progaurd 文件吗?如果您在 proguard 中添加了规则或没有看到它,我们不能说什么。 @Shvet 我已经添加了完整的 proguard 配置 尝试@human123 的解决方案,因为您已经添加了必要的规则。 @Shvet 通过添加“parceler”库并添加“-dontwarn com.fasterxml.jackson.**”使其工作 【参考方案1】:

如果按照 Emanuel Seidinger 的建议添加 keepattributes 行后仍然出现错误,请为引起问题的包添加 dontwarn 指令。

-dontwarn org.codehaus.jackson.**

【讨论】:

通过添加“parceler”库并添加“-dontwarn com.fasterxml.jackson.**”使其正常工作【参考方案2】:

我会从不同的角度来解决这个问题。我在这里看到的是,您提供的警告可能表明仅编译工件意外泄漏到 ProGuard 处理的实际编译/运行时类路径。 特别是注解处理器(如ParcelAnnotationProcessor)和可选注解不应包含在常规编译范围内。

通常您可以做的是将此类依赖项的范围设置为 providedcompileOnly,具体取决于您使用的 apt 插件(假设您使用 Gradle 构建 android 应用程序)

作为org.immutables 库的作者之一,我已经面临并讨论了ProGuard 的一些困难,它可能无法识别特殊的仅编译范围等。在您的特定情况下,您使用包含可选Jackson 的org.immutables:gson 工件-传播以构建的 Gson 集成桥类。 Android 上通常不需要这些类。作为一种解决方案,我建议将org.immutables:gson 工件替换为使用annotations 分类器(使用provided "org.immutables:gson:2.1.11:annotations")可用的仅注释版本。该工件不包括任何不需要的杰克逊相关类。 使用 Immutables 注解处理器时查看示例 Gradle 配置:http://immutables.github.io/getstarted.html#android

【讨论】:

【参考方案3】:

我还在一个 Android 项目中使用 GSON。您可以尝试在android-proguard-example 找到的 proguard 配置。您必须为项目中的模型类调整示例中的最后一行,这些模型类将使用 GSON 进行序列化。 由于 GSON,我的 proguard 配置中确实有以下几行:

-keepattributes Signature
-keepattributes *Annotation*

【讨论】:

以上是关于gson 和 jackson 的 Proguard 问题的主要内容,如果未能解决你的问题,请参考以下文章

启用 ProGuard 规则时 Gson 解析不起作用

Android 上的 ProGuard 和 Gson (ClassCastException)

GsonSpringMVC+Gson/JackSon总结

使用 EnumTypeAdapter 时,将 proguard 与 GSON 和 RoboGuice 一起使用会失败

使用启用了 proguard 的 GSON

使用 ProGuard 时是不是可以混淆 GSON 注释中的字符串?