使用 proguard 时如何保留/排除特定的包路径?

发布于 2024-10-14 22:42:11 字数 223 浏览 3 评论 0原文

我想从 ProGuard 中排除一些文件路径。示例 com.myapp.customcomponents

我该如何执行此操作?我讨厌为这个目录中的每个自定义组件文件放置 -keep 标志。

我已经尝试过以下方法,但它不起作用:

-keep public class com.myapp.customcomponents.*

I want to exclude some file paths from ProGuard. Example com.myapp.customcomponents

How can I do this? I hate to be placing -keep flags for every single custom component file I have in this directory.

I have tried the following but it doesn't work:

-keep public class com.myapp.customcomponents.*

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

牵你的手,一向走下去 2024-10-21 22:42:11

您没有指定它以何种方式不起作用。您的配置保留指定包中所有公共类的名称:

-keep public class com.myapp.customcomponents.*

以下配置保留指定包及其子包中所有公共类的名称:

-keep public class com.myapp.customcomponents.**

以下配置保留指定包中所有公共/受保护的类/字段/方法的名称指定包及其子包:

-keep public class com.myapp.customcomponents.** {
  public protected *;
}

You don't specify in what way it doesn't work. Your configuration keeps the names of all public classes in the specified package:

-keep public class com.myapp.customcomponents.*

The following configuration keeps the names of all public classes in the specified package and its subpackages:

-keep public class com.myapp.customcomponents.**

The following configuration keeps the names of all public/protected classes/fields/methods in the specified package and its subpackages:

-keep public class com.myapp.customcomponents.** {
  public protected *;
}
杀お生予夺 2024-10-21 22:42:11

在 ProGuard 配置的底部添加以下行:

-keep class com.facebook.** { *; }

相应地替换包名称,此处包 com.facebook 将从 ProGuard 中排除。

Add the following line at the bottom of your ProGuard configuration:

-keep class com.facebook.** { *; }

Replace package name accordingly, here the package com.facebook will be excluded from ProGuard.

感性 2024-10-21 22:42:11

使用 Android Studio 4.0 对我有用的是:

-keepclassmembers class com.myapp.customcomponents.* {
    <fields>;
    <init>();
    <methods>;
}

其他答案中的双星号 (**) 对我不起作用。我也用R8尝试了上述配置,效果很好。

What worked for me using Android Studio 4.0 is:

-keepclassmembers class com.myapp.customcomponents.* {
    <fields>;
    <init>();
    <methods>;
}

Double asterisks (**) in other answers did not work for me. I also tried the above configuration with R8, works fine.

夏九 2024-10-21 22:42:11

很多人似乎推荐 -keep class com.myapp.customcomponents.** { *; } 作为从处理中排除路径的一种方式。请参阅此处:

  1. 从 proguard 中排除软件包
  2. 让 Proguard 完全忽略包
  3. 防止目录受到 proguard 混淆

此解决方案的问题是仍然存在一定程度的混淆,这可能会破坏您的代码。您可以在映射打印输出中看到映射:

java.lang.String toString() -> toString
int getMemoizedSerializedSize() -> getMemoizedSerializedSize
void setMemoizedSerializedSize(int) -> setMemoizedSerializedSize
int getSerializedSize() -> getSerializedSize
boolean equals(java.lang.Object) -> equals
int hashCode() -> hashCode

我选择的解决方案是一个两步过程。首先,使用 injars 和过滤器来选择我想要处理的包路径。可以将其他包路径添加为库。

-injars       artifacts/in.jar(org/toprocess/**.class)
-outjars      out/processed.jar
-libraryjars  artifacts/in.jar(org/skipped/**.class)
-libraryjars  artifacts/in.jar(org/moreskipped/**.class)

其次,将处理后的 jar 与原始 jar 合并,但仅合并那些跳过的路径。

-injars  out/processed.jar
-injars  artifacts/in.jar(org/skipped/**.class)
-injars  artifacts/in.jar(org/moreskipped/**.class)
-outjars out/merged.jar

-dontshrink
-dontoptimize
-dontobfuscate

结果是一个合并的 jar,它是已处理的包路径和跳过的路径的组合。如果有人可以提供一种完全跳过某些路径处理的方法(我还没有找到),那么这个练习是无效的。

Lots of people seem to recommend -keep class com.myapp.customcomponents.** { *; } as a way to exclude a path from being processed. See here:

  1. exclude packages from proguard
  2. Make Proguard completely ignore package
  3. Prevent a directory from proguard obfuscation

The problem with this solution is that there is still some level of obfuscation happening, which can break your code. You can see the mapping in the mapping print out:

java.lang.String toString() -> toString
int getMemoizedSerializedSize() -> getMemoizedSerializedSize
void setMemoizedSerializedSize(int) -> setMemoizedSerializedSize
int getSerializedSize() -> getSerializedSize
boolean equals(java.lang.Object) -> equals
int hashCode() -> hashCode

The solution I have opted for is a two step process. First, use injars with a filter to select the package path I would like to process. It is possible to add the other package pathes as libraries.

-injars       artifacts/in.jar(org/toprocess/**.class)
-outjars      out/processed.jar
-libraryjars  artifacts/in.jar(org/skipped/**.class)
-libraryjars  artifacts/in.jar(org/moreskipped/**.class)

Second, merge the processed jar with the original jar, but only those paths that were skipped.

-injars  out/processed.jar
-injars  artifacts/in.jar(org/skipped/**.class)
-injars  artifacts/in.jar(org/moreskipped/**.class)
-outjars out/merged.jar

-dontshrink
-dontoptimize
-dontobfuscate

The result is a merged jar that is the combination of the processed package path and the skipped paths. This exercise is invalid, if someone can provide a way to skip processing of certain paths completely (which I haven't found).

人疚 2024-10-21 22:42:11

将所有与模型相关的包括模型、请求和响应对象放入中央子包中。

例如:com.example.models

然后将此行添加到您的 proguard-rules.pro 文件中:

-keepclassmembers class com.example.models.** { <fields>; }

Put all your models-related include models, requests and responses object into a central sub package.

For example: com.example.models

Then add this line to your proguard-rules.pro file:

-keepclassmembers class com.example.models.** { <fields>; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文