java jar包冗余,JarShrink:通过删除冗余类文件来缩小JAR

JarShrink

Shrinks JARs by removing redundant class-files.

Motivation

Looking for a way to statically link libraries in Java as you would do in C/C++, I was surprised to find no proper way.

So the next best thing that came to mind was to look for tools that would at least remove unused classes from a jar. Amazed by how slowly ProGuard does it's job, I wrote this clean simple tool.

How to use

You can either run JarShrink as a commandline tool or integrate it into your software by importing it and calling it's API.

Instructions on how to use these interfaces are found in the sections further below.

If your software or software you're including makes use of Reflection, you will most likely need to tell JarShrink to keep certain packages or whole libaries in order to preserve their functionality. I've started compiling a table of known libaries with information regarding their compatibility with JarShrink further below this readme.

There are pre-built JARs in the release-section, so you won't necessarily have to build JarShrink yourself.

However there is also a python build-script.

Prequisites

Java 8 or higher

JDK to run from commandline or any implementation of jdeps for API use

Commandline Interface

Grammar

jarShrink []

Arguments

Argument

Value

Effect

-o or -out

directory

Specifies the output-file for the newly created jar.

-k or -keep

package or class

Specifies a package or class that will be retained together with it's dependencies.

Can be called multiple times.

-s or -status

Print status information while processing.

-n or -nolist

Don't print a list of the remaining dependencies.

-t or -tmp

directory

Specifies JarShrinks tmp directory

Example

jarShrink "my.jar" -out "my_shrunken.jar" -status -keep "some.package.with.reflection"

API

JarShrink's functionality is encapsulated in the class JarShrinker for API use.

Examples

A minimal example would be:

JarShrinker shrinker = new JarShrinker();

shrinker.shrink(jarFile, outFile);

A more complete example:

JarShrinker shrinker = new JarShrinker(tmpDir);

shrinker.setPrintStatus(true);

shrinker.setPrintDependencyList(true);

shrinker.shrink(jarFile, outFile, keeps);

How it works

A basic summary of JarShrink's procedure:

Extract the jar's contents into a temporary directory.

Use jdeps to generate a dependency-map of all classes inside the jar.

Search for a Main-Class specified in the MANIFEST.MF file.

Construct a Dependency-Tree with the Main-Class and/or the specified classes/packages to keep as it's root.

Remove all class-files from the temporary directory that aren't in Dependency-Tree and scrap folders that are now empty.

Build a new jar from the remaining contents of the temporary directory.

Compatibility with known libraries

Below is a table of known libraries and how well JarShrink does with them.

Note that any library will still work if imported as a jar file.

Library

Compatibility

Extra arguments (if needed)

Dom4j

Guava

HTMLUnit

X

jbzip2

JInput

(✓)

-keep net.java.games.input

jorbis

libjpeg-turbo

LWJGL

Slick2D

A few notes

JarShrink won't touch included .jar files. This is not due to lazyness but to retain the ability to make sure that libraries, that make use of reflection will still keep their full functionality.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值