最小化 jar 依赖项大小
我编写的应用程序使用了多个第三方 jar。有时,仅使用整个 50kB 到 1.7mB jar 的一小部分 - 一两个函数调用或类。
减少罐子尺寸的最佳方法是什么?我应该下载源代码并构建一个仅包含我需要的类的 jar 吗?现有的工具可以帮助实现自动化(例如,我简要查看了 http://code.google.com/ p/jarjar/)?
谢谢
编辑1: 我想减小第三方“官方”jar 的大小,例如 swingx-1.6.jar (1.4 MB)、set-3.6 (1.7 MB) Glaledlists-1.8.jar (820kB) 等,以便它们仅包含我需要的最低限度的课程
编辑2: 如果库使用反射,则手动或使用 proguard 等程序最小化 jar 会更加复杂。 使用 google guice 注入不再起作用使用 proguard 进行混淆后
cletus 在另一篇文章中的答案非常好 如何确定Java程序使用了哪些类?
an application I have written uses several third party jars. Sometimes only a small portion of the entire 50kB to 1.7mB jar is used - one or two function calls or classes.
What is the best way to reduce the jar sizes. Should I download the sources and build a jar with just the classes I need? What existing tools can help automate this (ex I briefly looked at http://code.google.com/p/jarjar/)?
Thank you
Edit 1:
I would like to lower the size of my third party 'official' jars like swingx-1.6.jar (1.4 MB), set-3.6 (1.7 MB) glazedlists-1.8.jar (820kB) , etc. so that they only contain the bare minimum classes I need
Edit 2:
Minimizing a jar by hand or by using a program like proguard is further complicated if the library uses reflection.
Injection with google guice does not work anymore after obfuscation with proguard
The answer by cletus on another post is very good How to determine which classes are used by a Java program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Proguard 是一个选项。它可以消除未使用的类和方法。您还可以使用它进行混淆,这可以进一步减小最终 jar 的大小。请注意,除非注意保持受影响的类不模糊,否则按名称加载类可能会中断。
我发现 Proguard 非常有效 - 一开始可能有点难以理解。但我没有任何类似的经验来提供比较。
Proguard would be an option. It can eliminate unused classes and methods. You can also use it to obfuscate, which can further reduce the size of your final jar. Be aware that class loading by name is liable to break unless care is taken to keep the affected classes unobfuscated.
I've found Proguard quite effective - can be a bit cryptic to understand at the outset. But I don't have any experience with similar to offer a comparison.
首先,如果您仅使用 JAR 文件中的一个类,这并不意味着该类不使用该 JAR 中的其他类。
如果您使用开源 JAR,您的选择是获取该 JAR 的源代码,将它们附加到您的项目,删除不必要的内容并自行构建更改。
First of all, if you use only one class from JAR file this does not mean that this class does not use other classed from that JAR.
The option for you, if you use open source JARs, is to get sources of that JAR, attach them to your project, remove unnecessary stuff and build the changes by yourself.
您可以将 GenJar 添加为 Ant 任务并使用它来构建 JAR。正如图书馆主页上所说,
您可以在 SourceForge 上找到它。
You could add GenJar as an Ant task and use it to build the JAR. As it says on the library's home page,
You can find it on SourceForge.