Search code examples
androidgradlejarlibs

Android: .jar file in libs vs dependency url in gradle


I have come across two ways when I am adding jar dependency in an android project in Android Studio.

  1. Adding *.jar file in libs folder.
  2. Adding dependency url in gradle.build.

Which one is better and why?


Solution

  • I particularly prefer the dependency URL. A static library file in the project directory is seldom a better choice.

    If a dependency is updated, you just have to update the build.gradle. No need of downloading and replacing files, which adds size to the project (kilo or megabytes in the file system). Also, manually updating these files is an error-prone action.

    If the project is in a version control system (e.g. Git), commiting the jars to the repository is arguably a bad practice, whereas a modification in build.gradle is perfectly acceptable.

    Another advantage of dependency declaration is smart dependency management. If an artifact in use depends on another one, the dependency management is done automatically by e.g. Gradle or Maven. If a statically added jar has a dependency, it has to be manually added to the project, which is a laborious, tedious, and error-prone task.