Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I strip the fluff out of a third party library?

It may not be best practice but are there ways of removing unsused classes from a third party's jar files. Something that looks at the way in which my classes are using the library and does some kind of coverage analysis, then spits out another jar with all of the untouched classes removed.

Obviously there are issues with this. Specifically, the usage scenario I put it though may not use all classes all the time.

But neglecting these problems, can it be done in principle?

like image 333
Allain Lalonde Avatar asked Dec 13 '22 06:12

Allain Lalonde


2 Answers

There is a way.

The JarJar project does this AFAIR. The first goal of the JarJar project is to allow one to embed third party libraries in your own jar, changing the package structure if necessary. Doing so it can strip out the classes that are not needed.

Check it out at http://code.google.com/p/jarjar/.

Here is a link about shrinking jars: http://sixlegs.com/blog/java/jarjar-keep.html

like image 60
SaM Avatar answered Dec 15 '22 18:12

SaM


There is a tool in Ant called a classfileset. You specify the list of root classes that you know you need, and then the classfileset recursively analyzes their code to find all dependencies.

Alternatively, you could develop a good test suite that exercises all of the functions that you need, then run your tests under a test coverage tool. The tool will tell you which classes (and statement in them) were actually utilized. This could give you an even smaller set of code than what you'd find with static analysis.

like image 27
erickson Avatar answered Dec 15 '22 20:12

erickson