[9] Android Adding Libraries - mariamaged/Java-Android-Kotlin GitHub Wiki

Android - Adding Libraries

1. Depending on A Local JAR

enter image description here

JARs, as you probably know, are libraries containing Java code, as created by standard Java Build Tools (javac, jar, etc.).

  • In Android, the contents of these JARs are packaged into your APK.
  • Whatever public classes happen to be in these JARs are available to you at compile time.

However, in general, using plain JARs nowadays is considered a bad idea.

  • There is no information in a JAR about:
    1. What version of the library the JAR represents - while this could be part of the filename, files can be renamed far too easily.
    2. What other libraries this JAR requires -at best, you can find that out from documentation, then need to hunt down those JARs and see what they require, and so on.

2. Artifacts and Repositories

  • An artifact is usually represented in the form of two files:
    • The actual content, such as JAR.
    • A metadata file, paired with the JAR, that has information about "transitive dependencies" (i.e., the other artifacts that this artifact depends upon).

Artifacts are housed in artifact repositories.

  • These repositories not only contain the artifacts, but they organize the artifacts for easy access:
    • This includes organizing them by version, so you can request a specific version of an artifact and get it, instead of an older (or possibly newer) version of the same artifact.
       
  • Some artifact repositories are public. A typical Android project will use two such repositories:
    • JCenter, a popular place for open source artifacts.
    • Google's repository, for things like the Android Gradle Plugin.
       
  • There are two general-purpose artifact repositories, such as:
    • Maven Central.
    • jitpack.io.

2.1 Configure the Repositories

  • You may find some libraries that advice you to configure support for jitpack.io.
     

enter image description here

2.2 Identify the Version That You Want

  • Artifact: has an identifier made of three pieces:
    1. A group ID.
    2. An artifact ID within the group.
    3. A version number.

2.3 Add the Dependencies

Note that many libraries showing sample code for adding them to your build.gradle file will show a slightly different syntax, with a compile keyword instead of implementation.

enter image description here

  • That is because Android Studio 3.0 and Gradle 4.1 switched to a new syntax for specifying dependencies.