1

My library refers some big size jar, e.g. guava, math3, but i only use one or two method of them. Could i shrink the third-part codes to my library? Because with current aar output, my library size seems small, but for application used my lib, it will download all the third-part jars and build the whole code into their apk, which results a big size.

The "aar" seems only records which library it will use, but not compile the used code into it. Is there a library output type supports shrinking the code?

I see suggestion to use proguard, or minifyEnalbed. They don't work for what i wanted. Is any one familiar with compiler or aar/jar? is it possible to package all the code in one file, not download the jars when apk compile?

Sarah Ma
  • 141
  • 1
  • 6

2 Answers2

0

In your build.gradle set minifyEnabled true to enable proguard. By default it will remove all unused methods. You may need to set proper rules in the proguard-rules.pro file. Usually the library will have those documented.

Here is example release build type.

release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

You can also set it for your debug build however, I suggest not to enable proguard in debug as it will make your build slow and debugging the app would be painful.

bond
  • 11,236
  • 7
  • 48
  • 62
  • Actually it has been in the build.gradle, but it not work for aar output. Please check my last sentence. – Sarah Ma Dec 04 '15 at 09:00
0

As you want to reduce the file size of your library to reduce the dependencies and you only use two methods. I recommend a simple and pragmatic approach:

Simply copy these Methods. (Maybe check the license before if you are allowed to)

You can argue that is it not optimal and hard to maintain, but for two methods it is acceptable.

The thing about a library is that the user may want to have the dependencies as well. So it is hard to remove dependencies from the packaged aar-file. This could lead to inconsistencies if you have half of the library packaged to you lib and the user may need a more recent version of the same library.

Note:

Actually you don't need care about the size off the final application. Because Proguard can shrink this final application in the end.

The reason why is sometimes copy single Methods to my code is the reduce the time spend in packing the Debugging-Application. Because the waiting time can get quite high if you have a lot of dependencies.

Denis Lukenich
  • 3,084
  • 1
  • 20
  • 38