java 如何使用 ProGuard?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12114096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:38:12  来源:igfitidea点击:

How do I use ProGuard?

javacommand-lineproguard

提问by lvella

I was trying to learn how to use ProGuard, and it is no way as easy as I thought. At first I found a simple Java code to try it, a simple two class Swing calculator.

我试图学习如何使用ProGuard,它并不像我想象的那么容易。一开始找了个简单的Java代码试了一下,一个简单的二类Swing计算器

The code can be found by following that link, but I found it too verbose to post the it here. Anyway, it is a plain application with entry point on Calc.main(), there are no packages.

可以通过该链接找到代码,但我发现它太冗长了,无法在此处发布。无论如何,它是一个入口点在 上的普通应用程序,Calc.main()没有包。

Then I compiled both sources with:

然后我编译了两个来源:

$ javac *.java

and created the .jarfile (because it seems ProGuard only work with jars):

并创建了.jar文件(因为 ProGuard 似乎只适用于 jars):

$ jar cvef Calc calc.jar *.class
added manifest
adding: Calc.class(in = 3869) (out= 2126)(deflated 45%)
adding: Calc$ClearListener.class(in = 468) (out= 327)(deflated 30%)
adding: CalcLogic.class(in = 1004) (out= 515)(deflated 48%)
adding: Calc$NumListener.class(in = 1005) (out= 598)(deflated 40%)
adding: Calc$OpListener.class(in = 1788) (out= 1005)(deflated 43%)

Wrote the ProGuard file named obfuscate.pro:

编写名为 的 ProGuard 文件obfuscate.pro

-injars       calc.jar
-outjars      calc_obf.jar
-libraryjars  <java.home>/lib/rt.jar

-keep public class Calc extends javax.swing.JFrame {
public static void main(java.lang.String[]);
}

And finally run ProGuard:

最后运行 ProGuard:

$ ~/progs/proguard/proguard4.8/bin/proguard.sh @obfuscate.pro
ProGuard, version 4.8
Reading program jar [/home/lucas/tmp/calc.jar]
Reading library jar [/usr/lib/jvm/java-7-openjdk-i386/jre/lib/rt.jar]
Error: The output jar is empty. Did you specify the proper '-keep' options?

Well, obviously didn't work. I got tired of messing with ProGruard parameters, specially with that -keepoptions, with no success. All I found in the docs related to my problem could not help me. Then I am resorting to you... What is wrong? How to do it right?

嗯,显然没有用。我厌倦了搞乱 ProGruard 参数,特别是那些-keep选项,但没有成功。我在与我的问题相关的文档中找到的所有内容都无济于事。那我就求助于你了……怎么了?怎么做才对?

采纳答案by Louis Wasserman

I got it to work using the following configuration file:

我使用以下配置文件让它工作:

-injars       calc.jar
-outjars      calc_obf.jar
-libraryjars  <java.home>/lib/rt.jar
-keep class Calc {
  public static void main(java.lang.String[]);
}

Most notably, I ditched the publicin front of class Calc.

最值得注意的是,我放弃了public前面的class Calc.

回答by user3272245

I've had similar problems, solved by taking out Java modifiers.

我遇到过类似的问题,通过去掉 Java 修饰符解决了。

Java modifiers such as visibility modifiers are optional in the ProGuard configuration file -keep option (and in related options -keepclassmembers etc.)

Java 修饰符如可见性修饰符在 ProGuard 配置文件 -keep 选项(以及相关选项 -keepclassmembers 等)中是可选的

From manual: -keep [,modifier,...] class_specification

来自手册:-keep [,modifier,...] class_specification

So unless there is a specific reason otherwise, you can leave them out.

因此,除非有特殊原因,否则您可以将它们排除在外。