Minimizing jar dependency sizes

怎甘沉沦 提交于 2019-11-27 03:20:09

问题


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?


回答1:


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.




回答2:


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.




回答3:


You could add GenJar as an Ant task and use it to build the JAR. As it says on the library's home page,

GenJar is a specialized Ant task that builds jar files based on class dependencies rather than simply the contents of a directory.

You can find it on SourceForge.



来源:https://stackoverflow.com/questions/3817666/minimizing-jar-dependency-sizes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!