Friday, May 11, 2018

Understanding Android Studio buildscript repository

The android project gradle build script always starts with the below block

repositories {
    mavenLocal()
    google()
    jcenter()
}

mavenLocal(), by default, mavenLocal will load the dependent library from ~/.m2/respository folder. So assume the gradle file has the below item
   implementation group: 'com.sap.cloud.mobile.fiori', name: 'fiori', version: 1.0.0-SNAPSHOT
then the gradle will try to load the library from the below folder
    ~/.m2/repository/com/sap/cloud/mobile/fiori/fiori/1.0.0-SNAPSHOT
   
Actually, you can set any local folder as a repository by using the below code
    maven {
            url "file://local/repo/"
    }


google() is a repository managed by google for google and android libraries. The actual url is
     https://maven.google.com
which redirects to https://dl.google.com/dl/android/maven2/. You can find all group in the repository from
https://dl.google.com/dl/android/maven2/index.html

   
For example, if the gradle file include the below line
    implementation "com.google.firebase:firebase-core:15.0.0"
you can find the item from above url, which list all versions available for the firebase-core library

The library will be loaded from
https://dl.google.com/dl/android/maven2/com/google/firebase/firebase-core/15.0.2/firebase-core-15.0.2.pom
Based on the pom file, it indicate the library type, so you can download other files of the library


jcenter() is a general repository, if a dependent library cannot be find in local .m2 and google() repository, then it is in jcenter repository.
The url of the repository is
https://jcenter.bintray.com

For example, if gradle file includes the below code
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
then the gradle will download the library from
https://jcenter.bintray.com/com/squareup/okhttp3/okhttp/
which lists all supported versions. This is much easy to use than the google() repository. You can also get detail about a particular library from
https://jcenter.bintray.com/com/squareup/okhttp3/okhttp/3.10.0/

So next time, a particular version of a dependent library cannot be find, then you can check the repository to find out the reason.

1 comment:

  1. Have you thought about switching to Windows though? Recently Microsoft has released a fantastic tool in cooperation with https://ax-dynamics.com/. It could help you with your job as well!

    ReplyDelete