为什么 ProGuard 保留 onCreate() 方法?
我试图解决这个问题,但我根本不明白为什么会发生这种情况:根据默认的 proguard.cfg 文件,我定义了以下规则:
-keep public class * extends android.app.Activity
据我所知,这意味着:保留任何 < code>Activity 类作为入口点,但可以随意缩小/混淆/优化其中的任何内容(否则我必须使用
通配符来保留方法,也是吧?)。
现在我的测试 Activity 如下所示:
public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
}
public void unusedMethod() {
}
}
如果我现在导出签名的 APK,并且调用 ProGuard,它将按预期删除 unusedMethod
,但它将保留 onCreate
方法并不要混淆它的名字。这是为什么?
I'm trying to wrap my head around this, but I simply don't understand why this is happening: As per the default proguard.cfg file, I define the following rule:
-keep public class * extends android.app.Activity
as far as I understand, this means: keep any Activity
class as an entry point, but feel free to shrink/obfuscate/optimize anything inside it (otherwise I'd have to use e.g. the <methods>
wildcard to preserve methods, too, right?).
Now my test Activity looks like this:
public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
...
}
public void unusedMethod() {
}
}
If I now export a signed APK, and ProGuard is invoked, it will remove unusedMethod
as expected, but it will keep the onCreate
method and not obfuscate its name. Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对配置选项的理解是正确的。但是,ProGuard 无法删除或重命名您的
onCreate
方法,因为它会覆盖 android.app.Activity 中的 onCreate 方法。重命名它会破坏应用程序。不重写库方法的方法(例如unusedMethod
)可以安全地删除、内联或至少重命名。方法
M
应重命名,除非您为其指定某些 -keep 选项。您可以使用选项-whyareyoukeeping
进行检查。Your understanding of the configuuration options is correct. However, ProGuard can't remove or rename your
onCreate
method, because it overrides the onCreate method in android.app.Activity. Renaming it would break the application. Methods that don't override library methods, likeunusedMethod
, can safely be removed, inlined, or at least renamed.The method
M
should be renamed, unless you are specifying some -keep option for it. You can check that with the option-whyareyoukeeping
.