从第三方库和 JAR 中删除不必要的类
我需要从第三方 JAR 中删除未使用的类。为什么我应该使用工具?
我已经尝试使用 ProGuard。但是,它仅从项目本身中删除未使用的类,但第三方库 jar 始终保持不变。
I need to remove unused classes from third party JARs. Why tools should I use?
I already tried to use ProGuard. However, it removes only unused classes from the project itself but the library jars - third party - always remain unchanged.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以创建一个 uber jar,然后使用 ProGuard。将库类重新打包到 jar 中将是一个挑战,但从您的问题的精神来看,您会更喜欢 uber jar 本身。
正如其他发帖者评论的那样,您仍然需要小心通过如此多的滥用和误解的反射机制加载的类。
You can create an uber jar and then use ProGuard. Repackaging library classes into jars would be a challenge, but from the spirit of your question you will prefer the uber jar as such.
As other posters have commented, you still need to be careful about classes loaded through the so much abused and misunderstood reflection mechanism.
@Joonas Pulakka 是对的。但是,如果您仍然确实想这样做,并确保您的应用程序不会因
ClassNotFoundException
失败,请使用选项 -verbose:class 运行您的应用程序,执行所有存在的用例,获取包含所有加载的日志类。然后获取第三方库的所有类的列表,并将库中从未加载的所有类归档。然后创建仅包含“需要”类的替代 jar 文件并祈祷:)祝你好运。
@Joonas Pulakka is right. But if you still really want to do this and be sure that your application will not fail for
ClassNotFoundException
run your application with option -verbose:class, perform all usecases that exist, take the log that contains all loaded classes. Then take list of all classes of your third party library and file all classes from your library that have been never loaded. Then create alternative jar file that contains only "needed" classes and pray :)Good luck.
知道您正在使用哪些类和库是一件好事,即使删除未使用的东西存在风险(正如彼得所指出的),携带任何类型的超额行李都是有成本的,您不应该仅仅这样做不断积累。如果您使用反射,那么请了解您使用它的目的,并系统地删除您不需要的内容。您可以更好地理解更精简的代码库的好处。
It is a good thing to know what classes and libraries you are using, and even if there is risk (as pointed out by Peter) to removing unused stuff, there is cost in carrying any kind of excess baggage, and you shouldn't just keep accumulating. If you use reflection, then get a handle on what you are using it for, and systematically get rid of what you don't need. There are benefits to a leaner code-base that you understand better.
Java 仅在使用时加载类。删除类只会给您带来问题,并且不会在运行时为您提供帮助。 36 MB 的代码并不算多,因为只加载了其中的一部分。你有多少内存?现在大多数 PC 至少有 2000 MB 如果您通过慢速链接下载 applet 或 Java WebStart 应用程序,我想您正在使用 pack200(使 jar 更小)并且已经包含了最少的库。
Java only loads class as they are used. Removing classes can only cause you problems and won't help you at runtime. 36 MB of code isn't that much given only a portion of it will be loaded. How much memory do you have? Most PC have at least 2000 MB these days If you are downloading your applet or Java WebStart application over a slow link I would imagine you are using pack200 (to make the jars smaller) and have included the minimum of libraries already.