Generate minimized jar with only used classes

前端 未结 4 1264
失恋的感觉
失恋的感觉 2021-01-17 11:44

I\'m in need of creating the minimal jar of utils library for use in Android. I\'m using some methods from apache commons libraries (such as IOUtils, Stri

相关标签:
4条回答
  • 2021-01-17 12:13

    In general it is not possible to automatically select all classes that are used by an application. Just think about what we can do with Class.forName(String name) or if we use a dependency injection container and declare types in external configuration files.

    0 讨论(0)
  • 2021-01-17 12:15

    I guess if you use Eclipse to JAR the project it gives some options to do that while JARing :) Maybe it will be useful.

    Also you can collect your used library classes under a custom library and include this user created library in the project.

    0 讨论(0)
  • 2021-01-17 12:39

    The maven plugin you can't remember is probably Apache Maven Shade Plugin, there is minimizeJar option. As Andreas_D noticed, this won't include classes, loaded with Class.forName, so you will need to implicity say in configuration, that you need them. Here is how i made maven to include jdbc driver in my single jar:

    <filter>
      <artifact>net.sourceforge.jtds:jtds</artifact>
        <includes>
          <include>**</include>
        </includes>
    </filter>
    
    0 讨论(0)
  • 2021-01-17 12:40

    Excuse me, maybe i not clearly understood question. Obfuscator tool (i.e. ProGuard) could do that, isn't it? It packs several JARs into one and strips unused classes. If you don't need obfuscation/optimization (to prevent unwanted side-effects) then you could disable them, leaving "shrink" phase enabled.

    0 讨论(0)
提交回复
热议问题