Reduce duplicated dependencies from your project

Serhii Chaban
3 min readFeb 8, 2017

--

Developing android applications if you are not experienced developer you may eventually get problem with multidex in android and catch multidex exception:

Unable to execute dex: method ID not in [0, 0xffff]: 65536

Multidex is a problem of 4 android because it can work only with 65536 methods in your apk.

To solve this problem there are some ways:
— make up your minSdkVersion to lollipop and forget for pre lollipop devices (shit way cause android 4 devices are used for now)

- or you can set up multidex extension to your apk and continue to work as if nothing bad happened

- or you can use proguard and try to remove unused classes and resources to get squeezed into limit

- or you can manage your dependencies, remove dependencies and get new achievement of your knowledge.

You can thought “there only dependencies that i use so there are no dependencies that i can delete” aaaand… it’s not true and we will learn how to do this.

Some used dependencies and their methods count

Google Play Services play-services:10.0.1 79,958

Guava guava:18.0 14,842

AppCompat V7 appcompat-v7:25.0.1 16,653

Android Support V4 support-v4:25.0.1 10,417

Joda-Time joda-time:2.5 5,025

Apache Commons Lang3 commons-lang3:3.3.2 3,582

Apache Commons IO commons-io:2.4 1,571

Gson gson:2.3 1,243

As we see, some common known dependencies are very heavy in methods count, but what to do?

So let’s try to do something.

1) Step first replace play-services dependency with contained dependency you need (gcm, locations, analytics, etc)

2) Remove Guava, using it not the good price, really.

3) Instal this plugin it will help you watch about libraries and they hidden dependencies
UPD:this plugin is seems to not working now, so here an alternative
https://github.com/KeepSafe/dexcount-gradle-plugin

4) Try to fix using android support-v4 library unless you really need it.

5) Many libraries uses other libraries as dependency so in one moment there could be several copies of one library with different versions and it could be checked.

You can use ./gradlew [app_name]:dependences to see tree of dependencies or see to the picture to check already resolved packages

Set this to Project

Here you can see all copies of your dependencies

6) Remove included dependencies and replace them with one package with latest number

For example many dependencies uses support-v4 and appcompat-v7 as included packages and then could be different versions, so you need remove this packages from inside of dependencies and create one compile dependency.

This will remove all included modules of libraries

configurations {all*.exclude module: ‘appcompat-v7’all*.exclude module: ‘support-v4’}

Or you can manage throw each dependency to more clear removing packages like this:

compile (‘com.mapbox.mapboxsdk:mapbox-android-sdk:4.2.0@aar’) {    transitive = true    exclude group: ‘com.android.support’, module: ‘appcompat-v7’    exclude group: ‘com.android.support’, module: ‘recyclerview-v7’    exclude group: ‘com.android.support’, module: ‘design’    exclude group: ‘com.android.support’, module: ‘support-v4’    exclude group: ‘com.squareup.retrofit2’, module: ‘retrofit’    exclude group: ‘com.squareup.retrofit2’, module: ‘retrofit’    exclude group: ‘com.google.code.gson’, module: ‘gson’    exclude module: ‘guava’}

All of removed dependencies must be declared outside of mapbox in one copy for all libraries uses them.

That’s all! I hope this knowledge will be helpful and save you some neurons in brain!

After some time of researching and calculating methods you will get clear lightweight apk builds without unnecessary dependencies, and after using proguard will only get it better.

--

--