13

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, StringUtils). However, each such usage makes me import the whole library (commons-lang, commons-io etc.) which is absolutely acceptable under Tomcat (war's are mamoot-sized anyway), but absolutely unacceptable for Android project.

So, my aim is, to pack all used classes from dependencies into one jar - but only that classes that are needed. I remember once being in touch with maven plugin that done that task, unfortunatelly I can't remember its name nor find it via Google.

So please, do you know maven plugin that will do such minimization of dependencies, or any stand-alone tool that will do the same?

Danubian Sailor
  • 1
  • 38
  • 145
  • 223

4 Answers4

6

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>
Kirill
  • 6,762
  • 4
  • 51
  • 81
1

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.

lxbndr
  • 2,159
  • 1
  • 15
  • 17
  • I've found that ProGuard is already integrated into Android SKD, so I'll definetly look if it meets my requirements. – Danubian Sailor Mar 01 '12 at 15:33
  • As i know such packaging brokes signed library JARs. It may be serious disadvantage. – lxbndr Mar 01 '12 at 15:38
  • If you re-package libraries (strip unneeded stuff) then you logically need to sign the resulting version by yourself if you want signed code. The library is not the same anymore and the same signature should not apply in any case. – Mikko Rantalainen Jun 11 '18 at 07:33
0

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.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • I'm aware of it, but this is only when you use reflection. AFAIR this tool was working when no reflection was present, but can't tell for sure I've really seen it or it was only a dream ;) – Danubian Sailor Mar 01 '12 at 15:17
  • Not only when you use reflection - you could load a class from an url and that class could depend on classes on your classpath. Would crash too. – Andreas Dolk Mar 01 '12 at 15:32
  • it is still very useful even with some code loading in a way compiler does not see. Those who want to create a minimal jar can whitelist classes that would not be found automatically. – Davor Hrg Sep 22 '17 at 09:33
-3

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.

erogol
  • 13,156
  • 33
  • 101
  • 155