简体   繁体   English

如何将绒毛从第三方库中剥离出来?

[英]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. 它可能不是最佳实践,但是有方法可以从第三方的jar文件中删除未使用的类。 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. 查看我的类使用库的方式并进行某种覆盖分析的东西,然后吐出另一个jar,删除所有未触及的类。

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? 但忽视这些问题,原则上可以做到吗?

There is a way. 有一种方法。

The JarJar project does this AFAIR. JarJar项目完成了这个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. JarJar项目的第一个目标是允许一个人在你自己的jar中嵌入第三方库,必要时更改包结构。 Doing so it can strip out the classes that are not needed. 这样做可以删除不需要的类。

Check it out at http://code.google.com/p/jarjar/ . 请访问http://code.google.com/p/jarjar/查看。

Here is a link about shrinking jars: http://sixlegs.com/blog/java/jarjar-keep.html 这是一个关于缩小罐子的链接: http//sixlegs.com/blog/java/jarjar-keep.html

I use ProGuard for this. 我使用ProGuard As well as being an excellent obfuscator, it has a code shrinking phase which can combine multiple JARs and then strip out any unused classes or class members. 它是一个优秀的混淆器,它有一个代码缩小阶段,可以组合多个JAR,然后去掉任何未使用的类或类成员。 It does an excellent job at shrinking. 它在缩小方面表现出色

There is a tool in Ant called a classfileset. Ant中有一个名为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. 与静态分析相比,这可以为您提供更小的代码集。

At a previous job, I used a Java obfuscator that as well as obfuscating the code, also removed classes and methods that weren't being used. 在以前的工作中,我使用了一个Java混淆器,它也混淆了代码,还删除了未使用的类和方法。 If you were doing "Class.byName" or any other type of reflection stuff, you needed to tell the obfuscator because it couldn't tell by inspecting the code what classes or methods called by reflection. 如果您正在使用“Class.byName”或任何其他类型的反射内容,您需要告诉混淆器,因为它无法通过检查代码通过反射调用哪些类或方法来判断。

The problem, of course, is that you don't know if other parts of the third party library are doing any reflection, and so removing an "unused" class might cause things to break in an obscure case that you haven't tested. 当然,问题在于您不知道第三方库的其他部分是否正在进行任何反射,因此删除“未使用的”类可能会导致在未经测试的模糊情况下中断。

jar is just a zip file, so I guess you can. jar只是一个zip文件,所以我想你可以。 If you could get to the source, it's cleaner. 如果你能到达源头,那就更清洁了。 Maybe try disassembling the class? 也许尝试拆解课程?

Adding to this question, can that improve performance? 添加到这个问题,可以提高性能吗? Since the classes not used would not be JIT compiled improving startup time or does the java automatically detect that while compiling to bytecode and do not even deal with the code that is not used? 由于未使用的类不会被JIT编译改善启动时间,或者java是否在编译为字节码时自动检测到并且甚至不处理未使用的代码?

This would be an interesting project (has anyone done it already?) 这将是一个有趣的项目(有人已经完成了吗?)

I presume you'd give the tool your jar(s) as a starting point, and the library jar to clean up. 我认为你给你的工具作为起点,并清理库罐。 It could use reflection to determine which classes your jar(s) reference directly, and which are used indirectly down the call tree (this is not trivial at all, but doable). 它可以使用反射来确定你的jar直接引用哪些类,以及在调用树中间接使用哪些类(这根本不是微不足道的,但可行)。 If it encounters any reflection code in any of the two places, it should give a very loud warning. 如果它遇到任何两个地方的任何反射代码,它应该发出非常大声的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM