如何使用 ProGuard 将所有方法保留在类中
我使用 ProGuard 来优化我的 Android 应用程序。然而,对于 Android 仪器测试,我需要一些(但不是全部)类来保留所有成员。我尝试了各种方法,最后一种是:
-keepclassmembers public class com.mycompany.myclass {
*;
}
但令人惊讶的是我仍然得到
java.lang.NoSuchMethodError: com.mycompany.myclass.<init>
这里令人痛苦的部分是有两个构造函数,其中一个有相当多的参数。
有谁知道保持类完全不变且不受 ProGuard 影响的正确语法吗?
I use ProGuard to optimize my Android Application. However for Android instrumentation test I need some (but not all) classes to keep all there members. I tried various approaches, the last being:
-keepclassmembers public class com.mycompany.myclass {
*;
}
But surprisingly I still get
java.lang.NoSuchMethodError: com.mycompany.myclass.<init>
The painful part here is that there are two constructors and one has quite a few parameters.
Does anyone know the correct syntax to keep a class completely unchanged and untouched by ProGuard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,是时候告白了。这个问题是胡说八道。
-keepclassmembers
是正确的。问题的发生是因为一名队友破坏了代码,而构造函数确实不存在。请注意,如果整个类发生了优化,那么您应该按照 kharles 的建议使用
-keep
,但需要{*;}
来确保所有方法留在原地。请注意,
{*;}
仅用于测试。对于生产,应该使用更细粒度的方法。我为有同样问题的人保留这个问题。
Well, it is confession time. The question is bollocks. The
-keepclassmembers
is correct. The problem occurred because a team mate broke the code and the constructor was truly not there.Note that if there is a change that the whole class is optimized away then you should use
-keep
as kharles suggested but the{*;}
is needed to ensure that all methods stay in place.Note that the
{*;}
is for testing only. For production one should use a more fine grained approach.I keep the question for anybody with the same problem.
尝试
(使用 keep,而不使用 {*;} )
try
( use keep, and no {*;} )