Android:Proguard 的推荐配置是什么?

2023-12-12

我正在为 Android 开发应用程序并使用 Proguard 来混淆代码。

目前我正在使用 ProGuard 配置:

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

-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 com.android.vending.licensing.ILicensingService

要维护布局 XML 上使用的自定义组件名称:

-keep public class custom.components.**

要删除调试日志:

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

为了避免更改布局的 onClick 上调用的方法的名称:

-keepclassmembers class * {
 public void onClickButton1(android.view.View);
 public void onClickButton2(android.view.View);
 public void onClickButton3(android.view.View);
}

-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);
}

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

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

问题是(是):

还有其他推荐的标签吗?为什么以及为什么?

可以对 proguard.cfg 文件发表评论吗?我希望对某些行正在执行的操作进行评论,以便其他开发人员不会怀疑我添加的原因。

同样在proguard中,是否可以维护文件的注释头(具有版权)?如果不是,或者这不是一个好的政策,我应该在哪里添加版权?


Android SDK(r20 或更高版本)

请检查project.properties中引用的预定义proguard.config

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt

更多信息:http://proguard.sourceforge.net/manual/examples.html#androidapplication

在这里您可以检查我不断更新的混淆“默认”文件: https://medium.com/code-procedure-and-rants/android-my-standard-proguard-ffeceaf65521


Android SDK(r19 或更低版本)

根据我的回答在 Android 版 Eclipse 中启用 ProGuard我最终得到了这个通用文件。我添加了注释来记住每一行的用途。它可能会帮助人们,所以这里是:

-optimizationpasses 5

#When not preverifing in a case-insensitive filing system, such as Windows. Because this tool unpacks your processed jars, you should then use:
-dontusemixedcaseclassnames

#Specifies not to ignore non-public library classes. As of version 4.5, this is the default setting
-dontskipnonpubliclibraryclasses

#Preverification is irrelevant for the dex compiler and the Dalvik VM, so we can switch it off with the -dontpreverify option.
-dontpreverify

#Specifies to write out some more information during processing. If the program terminates with an exception, this option will print out the entire stack trace, instead of just the exception message.
-verbose

#The -optimizations option disables some arithmetic simplifications that Dalvik 1.0 and 1.5 can't handle. Note that the Dalvik VM also can't handle aggressive overloading (of static fields).
#To understand or change this check http://proguard.sourceforge.net/index.html#/manual/optimizations.html
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

#To repackage classes on a single package
#-repackageclasses ''

#Uncomment if using annotations to keep them.
#-keepattributes *Annotation*

#Keep classes that are referenced on the AndroidManifest
-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 com.android.vending.licensing.ILicensingService


#To remove debug logs:
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

#To avoid changing names of methods invoked on layout's onClick.
# Uncomment and add specific method names if using onClick on layouts
#-keepclassmembers class * {
# public void onClickButton(android.view.View);
#}

#Maintain java native methods 
-keepclasseswithmembernames class * {
    native <methods>;
}

#To maintain custom components names that are used on layouts XML.
#Uncomment if having any problem with the approach below
#-keep public class custom.components.package.and.name.**

#To maintain custom components names that are used on layouts XML:
-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

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

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

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

#To keep parcelable classes (to serialize - deserialize objects to sent through Intents)
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

#Keep the R
-keepclassmembers class **.R$* {
    public static <fields>;
}

###### ADDITIONAL OPTIONS NOT USED NORMALLY

#To keep callback calls. Uncomment if using any
#http://proguard.sourceforge.net/index.html#/manual/examples.html#callback
#-keep class mypackage.MyCallbackClass {
#   void myCallbackMethod(java.lang.String);
#}

#Uncomment if using Serializable 
#-keepclassmembers class * implements java.io.Serializable {
#    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();
#}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:Proguard 的推荐配置是什么? 的相关文章

随机推荐

  • 用于下载文档的 Alfresco REST API

    我想使用 Afresco REST API 下载文档 经过一番研究 我发现了这个 REST API alfresco s api node content property store type store id id 但我不知道如何传递参
  • 如何从servlet调用JavaScript函数

    我是网络开发新手 我有一个外部 JavaScript 文件 其中包含一个要显示的函数 包含错误详细信息的提示 我需要将错误消息传递给函数 我已经在servlet中编写了控制器 如何从我的 servlet 调用该 JavaScript 文件的
  • 处理退出状态 popen python

    我试图用 popen 处理状态退出 但它给出了一个错误 代码是 import os try res os popen ping c 4 www google com except IOError print ISPerror popen t
  • 奇怪的单元格地址非连续范围的行为:VBA

    我试图回答这个问题当我在 Excel 中遇到一些奇怪的 VBA 行为时 我写了一个非常简单的子程序来演示这个问题 Sub debugAddresses rng As Range Debug Print Whole range rng Add
  • 从 Android 中的通知开始新活动

    我想从状态栏通知启动一个活动 A 当活动 A 已经在前面时 我想完成该活动并重新启动活动 A 我该怎么做 查看有关创建状态栏通知的文档 这绝对涵盖了使用 Intent 和 PendingIntent 从通知启动和 Activity http
  • 从边列表构建所有哈密顿路径

    我无法找到从相关元组列表构建树路径的方法 我只想要每个节点被访问一次的每个路径的列表 也称为哈密尔顿路径 我不断接近 但错过了一些路径 例如 假设我们有以下连接列表 connections 1 4 1 5 2 5 3 4 4 1 4 3 4
  • expo-auth-session - 返回应用程序时的两个应用程序选项

    SDK版本 43 0 0 平台 Android iOS 网络 所有 Android 您好 我在托管工作流程中使用 expo auth session 通过其官方 API 收集 Instagram 用户数据 它在开发环境中运行良好 通过 ex
  • awk 搜索字符串并设置退出代码(如果存在)

    我想检查两个地址之间是否存在一行并返回退出代码0如果它存在并且1如果没有 我想我已经有了一个可能的例子 但我想知道是否有一种更干净的方法来做到这一点 或者也许是 RedHat 上也常见的不同工具 我的命令 awk BEGIN found 1
  • 管道超级代理响应到快速响应

    我正在尝试使用 Express 应用程序 代理 一些文件 为什么下面的代码不起作用 var app require express var request require superagent app get image function
  • GeoServer 不会写入我的 PostgreSQL 可更新视图

    继从这个先前的问题我使用的是 PostgreSQL 8 4 并且在可更新视图方面遇到问题 我有一个看法 CREATE VIEW filedata view AS SELECT num id ST TRANSFORM the geom 900
  • 单击 vue 组件中的“保存”按钮后如何关闭模式?

    我的 vue 组件是这样的
  • Java listjar 目录中的文件

    有什么办法可以使用吗listFiles 在已打包到 jar 中的目录上 假设我的资源目录中有一个目录 其中包含一些文本文件 texts text1 txt and texts text2 txt 在这个 Java 程序中 我有一个需要使用的
  • 正确使用GetRawInputBuffer

    我正在尝试使用 Win32 原始输入 API 以更高精度收集原始鼠标数据 但我似乎无法理解文档和样本完全为了GetRawInputBuffer 当我的鼠标悬停在窗口上时 似乎什么也没有发生 当我单击或释放窗口标题栏时 我似乎只获得缓冲数据
  • NetBeans 不再为以前生成的代码生成 JAR 文件

    两年前 我使用 Java 编写了一个简单的上传小程序 使用 NetBeans 开发 当我第一次点击 清理和构建 时 以及之后的一段时间 它运行得非常完美 它将运行整个构建过程并生成一个 JAR 文件 然后我可以将其上传到我的 Web 服务器
  • 用于阻止特定日期(假期)的引导日期选择器配置

    有谁知道如何配置日期选择器不显示特定日期 例如 7 月 4 日 这似乎可以使用 beforeShowDay 来完成 但我并不肯定 http jsfiddle net Lr3taznx a array of dates that should
  • 如何启用cookie

    我有这个任务来读取 写入 启用 cookie 以便将用户名存储在变量中 然后写入 cookie 我的问题是代码的最后一部分似乎正在工作 但是用户名应该存储到变量中的第一部分不起作用 我可以看到当我运行代码时 前两个警报框没有显示 它应该以
  • React传单和react-leaflet-draw

    我正在尝试在传单地图上实现绘制功能 我创建了一个仅安装了react leaflet的新应用程序 使用npx create react app并安装了以下软件包 npm install React React dom 传单 npm 安装反应传
  • 复制到剪贴板在 Android 上不起作用

    使用此视图创建标准移动应用程序 public class DebugView extends View ListView
  • 为什么 sys.getrefcount 给出巨大的值?

    import sys a 10 b a print sys getrefcount a b 1 print sys getrefcount b output 22 614 我的Python解释器有问题吗 为什么这会给出像 614 这样巨大的
  • Android:Proguard 的推荐配置是什么?

    我正在为 Android 开发应用程序并使用 Proguard 来混淆代码 目前我正在使用 ProGuard 配置 optimizationpasses 5 dontusemixedcaseclassnames dontskipnonpub