Reputation: 5444
So we have an extensive library of java code in a number projects that make jar files and external libraries. I am working on an app that uses a sparse set of classes from the large set of jar files in the class path.
Because we are loading the app on a device with limited storage I would like to cull all the unused classes from the jar file set and then create a jar of only what is needed for the application.
That is run a process very similar to a traditional "link" that only includes code that is actually referenced.
Ideally I could specify a list of "Root" classes like the one including the application's "main" and anything I know is dynamically loaded and items such as resources, and run some tool on a specified class path which will build a folder of just what is needed to run the application.
Then I can jar up that result for the as-small-as-possible jar needed to run the app/applet/servlet.
I could conceive of a custom "class loader" that would simply generate a listing of everything that is loaded when an app runs. (ie class + source in classpath) that could be used for this purpose if one was able to run all code paths in the application. Of course if there is dynamic loading of platform or other runtime dependencies this would not cover for that but it would do a large part of the job.
Thanks.
Upvotes: 2
Views: 99
Reputation: 5526
One such tool is ProGuard.
ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or higher, or for Java Micro Edition.
Tutorial for using it is here.
Upvotes: 1