最小化 jar 依赖大小

2022-01-15 00:00:00 dependencies java jar utility

我编写的一个应用程序使用了几个第三方 jar.有时只使用了整个 50kB 到 1.7mB jar 中的一小部分——一个或两个函数调用或类.

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.

减小罐子尺寸的最佳方法是什么.我应该下载源代码并用我需要的类构建一个 jar 吗?哪些现有工具可以帮助实现这一点(例如,我简要地查看了 http://code.google.com/p/jarjar/)?

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/)?

谢谢

编辑 1:我想减小我的第三方官方"罐子的大小,如 swingx-1.6.jar (1.4 MB)、set-3.6 (1.7 MB) glazedlists-1.8.jar (820kB) 等,以便它们只包含我需要的最低限度的课程

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

编辑 2:如果库使用反射,则手动或使用 proguard 之类的程序最小化 jar 会更加复杂.使用 google guice 注入不再起作用用proguard混淆后

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

cletus 在另一篇文章中的回答非常好 如何判断Java程序使用了哪些类?

The answer by cletus on another post is very good How to determine which classes are used by a Java program?

推荐答案

Proguard 将是一个选项.它可以消除未使用的类和方法.您还可以使用它来混淆,这可以进一步减小最终 jar 的大小.请注意,除非注意保持受影响的类不被混淆,否则按名称加载类可能会中断.

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.

我发现 Proguard 非常有效 - 一开始可能有点难以理解.但是我没有任何类似的经验来提供比较.

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.

相关文章