Managing Android dependencies

  • Use Gradle to manage dependencies (the standard Android build tool).

  • Add dependencies to app module’s build.gradle file.

  • Adding a dependency is usually as simple as adding one line to build.gradle file but sometimes there is a need to also add a separate repository or apply a plugin (the library wiki should cover the details) and pressing ‘Sync Now’ link.

  • Try assessing the value of using a dependency before adding it to the project. Android discourages careless addition of depedencies to the app due to several reasons:
    • The 64K limit. The more dependencies the are, the easier is to reach this limit. So it is generally referenced as an anti-pattern to include Guava into the project since it has a lot of methods and you are usually going to use only a few. See here.
    • Dependencies can prohibit from updating other more important dependencies like support libraries. So using only a couple of features from a not so maintained library may result in an inability to update other libraries that this dependency depends on. Then, you will have to either try to update this dependency to the newest version (if one is present) and then try to update other libraries or this library will have to be removed in order to update other dependencies.
  • In other cases libraries will have to be added directly to the project (these will usually be libraries with closed source) and commited to VCS.

  • It is also important to note that added dependencies might need you to update/install tools in Android SDK Manager.

Updating dependencies:

  • Android Studio and support library updates will be shown automatically as soon as present.

  • Always update android support libraries as soon as possible (and the app does not break after update).

  • Updating android support libraries also lets you update target and compile SDK versions.

  • When support libraries cannot be upgraded it is usually because other dependencies might be depending on concrete version of those support libraries. So then dependencies should be upgraded first if possible (or removed).

  • Upgrading dependencies at its simplest form might be just changing the dependency version at the end of the dependency name (the newest version can be found in the dependency homepage or inside the repository that it uses). In other cases the code might have to be updated also.

  • Always write explanatory comments when some libraries can’t be upgraded:

dependencies {

    ...

    // newest version crashes, probably because we need to update support libs first
    compile 'com.github.rey5137:material:1.2.1.6-SNAPSHOT'

    compile 'com.github.lzyzsd:circleprogress:1.2.0'

    // newest version crashes, probably because we need to update support libs first
    compile 'com.github.AndroidDeveloperLB:MaterialPreferenceLibrary:5'

    ...

}