适用于 Android 和 GSON 的 ProGuard

2023-12-03

我正在为我的 Android 项目设置 ProGuard。我的项目也使用GSON。

我研究了 ProGuard 配置与 GSON 和 Android 的兼容性,并发现了 google-gson 提供的这个示例https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg.

ProGuard 配置复制如下:

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

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

问题:

  1. 我看到这个文件自2011年以来就没有更新过,是否仍然建议使用?我问这个问题是因为 Android/GSON 从那时起已经发生了很大的变化,所以我不知道上面的内容有多少是不必要的或不正确的。

  2. 如果不推荐,Android 中是否有新的 GSON 推荐 ProGuard 配置?


我认为默认情况下,您拥有的大部分设置已经包含在 Android SDK 中。

因此,您可以删除其中的大部分内容,只保留专门用于 GSON 的部分。


我正在 Eclipse 中使用 Android SDK Tools 22.6.3 以及附带的 ProGuard 的任何版本进行开发。

这是我用于 GSON 2.2.4 的内容(按照他们的例子):

##---------------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 mypersonalclass.data.model.** { *; }

它看起来与您所拥有的完全相同,只是我不需要有关注释的行。


你可以看到我注释掉了一些我自己添加的类。如果您序列化/反序列化自己的类,则需要在此处声明它们以代替对mypersonalclass.data.model。这非常重要,因为您不希望 ProGuard 混淆 GSON 用于序列化的字段或类名称。

我总是在那里留下这些类型的注释,所以我知道如何配置下一个库或应用程序。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

适用于 Android 和 GSON 的 ProGuard 的相关文章

随机推荐

  • 从另一个类执行segue

    我试图从不同的类 即 NSObject 类 调用 PerformSegueWithIdentifier 但收到此错误 由于未捕获的异常 NSInvalidArgumentException 而终止应用程序 原因 Receiver
  • WordPress 调用未定义函数 get_header();

    首先 我查找了有关该主题的以前的帖子 但没有一个有我需要的答案 所以我将改写以最适合我的问题 我正在尝试创建一个非常基本的 WordPress 主题 我已经获得了所需的所有模板文件 在检查文件是否有错误时 我发现每个文件都出现错误 致命错误
  • Android 项目中 git 应该忽略哪些文件夹

    我正在使用 Eclipse 和 git 构建 Android 应用程序 每次我进行提交时 我都会看到文件中的更改 但我不确定是否需要跟踪 例如 bin 文件夹内的一些更改 在这种项目中 git 可以安全地忽略哪些文件夹 您可以使用gitig
  • 带有自定义标头的 Ruby 中的 HTTP.post_form

    我正在尝试使用 Nets HTTP 来使用POST并放入自定义用户代理 我通常用过open uri但它做不到POST can it I use resp data Net HTTP post form url query 我该如何更改它以放
  • 将 json 字符串作为参数传递给 webmethod

    我正在向 webmethod 发送 ajax 帖子EmailFormRequestHandler 我可以在客户端 通过 firebug 看到请求的状态为 200 但它没有到达我的 webmethod 中的停止点 webmethod 的第一行
  • 汉字DomPDF生成

    我正在尝试使用 dompdf 生成包含中文字符的 PDF 这是我的代码 require dompdf dompdf config inc php dompdf new DOMPDF mb internal encoding UTF 8 de
  • C# JSON.Net 使用 LINQ 解析并获取与某个值匹配的所有元素的列表

    我无法找到从 JObject 获取 json 数组列表的正确方法 name数组内的元素应该等于foo 这是示例 json doc bob tom frank category name foo letters abc name foo
  • 无法为 solr/haystack 构建索引:未知字段“django_id”

    我正在尝试遵循干草堆教程 当我运行manage py重建索引时遇到错误 我收到以下错误 WARNING This will irreparably remove EVERYTHING from your search index Your
  • 从用户控件退出应用程序

    我的应用程序中有一个主窗口 MainWindow 在其 ContentControl 中托管一个 UserControl 我称之为 MainPage MainPage 承载另一个 UserControl 其中包含各种控件 KiviPage
  • 异步运行 mocha js(类似 AND)

    我可以在浏览器中异步加载 mocha 模块吗 我用柴肯定可以做到 有什么解决方法可以让 mocha 以类似 amd 的风格工作吗 require config baseUrl scripts paths mocha framework mo
  • 查找并打印 DIV 中的所有链接

    我试图找到 div 中的所有链接 然后打印这些链接 我正在使用 Simple HTML Dom 来解析 HTML 文件 这是我到目前为止所得到的 请阅读内联评论并让我知道哪里出错了 include simple html dom php h
  • 解析python中的嵌套括号,按级别抓取内容

    显然这个问题经常出现 读完后 思考了一会儿这个问题 我写了一个函数来返回任意数量的嵌套 中包含的内容 该函数可以轻松扩展到任何正则表达式对象 在此发布以供您的想法和考虑 任何重构建议将不胜感激 注意 我还是Python新手 不想弄清楚如何引
  • 检测到自引用循环 - 将数据从 WebApi 返回到浏览器

    我正在使用实体框架 并且在将父级和子级数据获取到浏览器时遇到问题 这是我的课程 public class Question public int QuestionId get set public string Title get set
  • 删除所有行并保留最新的 x left

    我有一张像这样的桌子 entryid roomid 1 1 2 55 3 1 4 12 5 1 6 44 7 1 8 3 9 1 现在我想删除 roomid 1 的所有条目并保留 剩余 roomid 1 中的最新 3 个 最好只用一个命令
  • 如何在通过括号表示法访问的元素上使用 jQuery 方法?

    jQuery 对象中的元素可以通过括号表示法访问 如下所示 div 0 但在这种情况下不能使用 jQuery 方法 还有其他方法可以访问某些元素或使用 jQuery 方法吗 如果您尝试获取集合中的第一个 jQuery 对象 请使用 jQue
  • 使用 pyfpdf 将 Base64 图像插入 pdf

    我在 python 中使用 pyfpdf 来生成 pdf 文件 我有一个 Base64 我想将其插入到 pdf 文件中 而不必将其另存为文件系统中的图像 但 pyfpdf 图像函数仅接受文件路径 fpdf image name x None
  • 选择组合的子集

    假设我有一个 20 X 5 矩阵 我想选择矩阵的子集并用它们进行一些计算 进一步假设每个子矩阵是 7 X 5 我当然可以这样做 ncomb lt combn 20 7 这给了我 7 个行索引的所有可能组合 我可以使用它们来获取子矩阵 但对于
  • 将 System.Web.UI.WebControls.Image 转换为 System.Drawing.Image?

    我似乎无法转换它 有什么帮助吗 它们是两个完全不同的对象 System Web UI WebControls Image是一个能够渲染 HTML 的控件 它将使浏览器下载并显示指定的图像 System Drawing Image是一个类 能
  • RODBC 和 Access - 加载数据

    我正在尝试将一些数据从 Access 数据库加载到 R 中 我能找到的所有说明都说使用 odbcConnectAccess file mdb 但我似乎无法加载此函数 是否已被替换或重命名 还有其他方法可以做到这一点吗 我已经用库 RODBC
  • 适用于 Android 和 GSON 的 ProGuard

    我正在为我的 Android 项目设置 ProGuard 我的项目也使用GSON 我研究了 ProGuard 配置与 GSON 和 Android 的兼容性 并发现了 google gson 提供的这个示例https code google