为什么Proguard不会混淆某些软件包专用的类? [英] Why some package-private classes are not obfuscated by Proguard?

查看:186
本文介绍了为什么Proguard不会混淆某些软件包专用的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在启用了Proguard和一些特定规则的情况下,使用Android Studio 3.2中的Android项目,我无法确定以下内容:

Working with an Android project in Android Studio 3.2, having enabled Proguard and some specific rules, I'm not able to figure out the following:

客户规则使用的库模块中的特定程序包(及其子程序包)通过以下规则保留:

a specific package (and its subpackages) in a library module, used by client code, is preserved through the rule:

-keep public class com.mylib.mypackage.** {
    public protected *;
}

现在,在此软件包中,还有许多软件包专用类,该类不应选择这些类.这些类中有一些被有效地混淆了,无论是它们自己的名称还是其成员名称,这都很好.

Now, within this package there are also a number of package-private classes, which should not be picked by that rule. Some of those classes are effectively obfuscated, both in their own names and their member names, which is fine.

相反,有一些类实现了公共接口,它们的类名没有被混淆,而我希望它们应该如此.为了完整起见,它们的成员名称(如果不是接口的一部分)将被有效地混淆.

Instead there are some classes, implementing public interfaces, whose class names are not obfuscated, while I'd expect they should. For completeness, their member names, when not part of interface, are effectively obfuscated.

示例:

/* package */ class InternalComponent implements ExternalInterface {

  // ExternalInterface is kept: Ok
  // InternalComponent is kept: don't like, I'd like it renamed

  @Override
  public void ExternalMethod() {
    // this is kept: Ok
  }

  public void InternalMethod() {
    // this is renamed: Ok
  }
}

我想强调一点,InternalComponent是在其他(保留的)类中创建的,并且仅通过ExternalInterface返回到客户端代码.

I'd like to highlight that InternalComponent is created within some other (kept) class and only returned to client code through the ExternalInterface.

如果可能的话,我还如何混淆它们的类名?

How can I also obfuscate their class names as well, if possible?

编辑#1
在对Proguard输出文件进行@emandt答复后,我再次检查了com.mylib.mypackage.InternalComponent并列在seeds.txt中,根据

Edit #1
After @emandt answer on Proguard output files, I double checked and com.mylib.mypackage.InternalComponent is listed in seeds.txt, which according to this blog post lists all items matched by keep rules. So, for some reason, the rule above also picks package-private classes, which still seems wrong to me.

编辑#2 同时,我最终使用了@shizhen提出的完全相同的方法.为了完整起见,为了将排除范围扩展到名为internal any 程序包,我将proguard规则修改为:

Edit #2 In the meantime, I ended up doing exactly the same approach proposed by @shizhen. For completeness, in order to extend the exclusion to any package named internal, I modified my proguard rule as:

-keep public class !com.mylib.mypackage.**.internal.*, com.mylib.mypackage.** { 
    public protected *;
}

(请注意逗号前面的第一部分,以!为前缀)

(note the first part before the comma, prefixed by !)

我会标出@shizhen的答案,尽管我想知道为什么原来的规则还选择了包私有的组件.

I'll mark @shizhen answer, though I'd like to be curious as to why the original rule is also picking package-private components.

推荐答案

您是否正在从事 Android库项目?可能是.

Are you working on an Android Library project? Probably YES.

为了实现您的目的,恐怕您需要将软件包重新组织为如下所示:

In order to achieve your purpose, I am afraid that you need to re-organise your packages into something like below:

公共界面

com.my.package.apiforusers

私有/内部实现

com.my.package.apiforusers.internal

然后根据混淆规则,如下所示:

Then for your obfuscation rules, you can have it like below:

-keep public class com.my.package.apiforusers.* { public *; }

因此,仅 公共类/接口会被保留,而com.my.package.apiforusers.internal内部的所有类/接口都将被混淆.

So that only the public classes/interfaces are kept and all those ones inside com.my.package.apiforusers.internal will be obfuscated.

这篇关于为什么Proguard不会混淆某些软件包专用的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆