Search code examples
androidandroid-studioandroid-gradle-plugindependenciesbuild.gradle

Android Studio How to build an aar file with gradle that has remote dependencies


I created a Android Library Module with Android Studio and I was able to use it in the apps and right now I need to use it for my other apps. So I was thinking about using the remote dependecies like Picasso https://github.com/square/picasso

 compile 'com.squareup.picasso:picasso:2.5.2'

And I would like to know what are the steps I need to take? I read few article and website. It is very confusing.


Solution

  • To make your library available as a remote dependency there are only three steps you have to take:

    1. Build your library and provide the necessary repository meta data
    2. Put your library with meta data in a repository
    3. Tell your project to look in that repository for dependencies

    After that is in place you can have remote dependencies like this in your app's build.gradle file:

    compile 'com.example.developer:lib-util:1.0.0'
    

    This will look in all the repositories that you have registered for a group or organization called 'com.example.developer', and then an artifact named 'lib-util' with version '1.0.0'.

    Build your library and provide the necessary repository meta data

    That may sound complex but it's really not that hard. It's just a directory structure that looks a little like this:

    com
     |-- example
          |-- developer
               |-- lib-util
                    |-- 1.0.0
                    |    |-- lib-util-1.0.0.aar
                    |    |-- lib-util-1.0.0.aar.md5
                    |    |-- lib-util-1.0.0.pom
                    |    |-- lib-util-1.0.0.pom.md5
                    |-- maven-metadata.xml
                    |-- maven-metadata.xml.md5
    

    The file lib-util-1.0.0.aar is the compiled library (in Android Archive format). The lib-util-1.0.0.pom file contains information about the library itself, such as the authors and its dependencies. The maven-metadata.xml file contains the necessary information to know which versions are available (in this case just one). And lastly the *.md5 files contain a checksum to verify file integrities. (There are also *.sha1 checksum files that I've left out for brevity.)

    To build this structure you can make use of the Maven Gradle plugin. Put it in your library's build.gradle and configure the mavenDeployer properties:

    library build.gradle:

    apply plugin: 'com.android.library'
    apply plugin: 'maven'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.1"
    
        defaultConfig {
            minSdkVersion 16
            targetSdkVersion 23
            versionCode 1
            versionName "1.0.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    uploadArchives {
        repositories.mavenDeployer {
            pom.groupId = 'com.example.developer'
            pom.artifactId = 'lib-util'
            pom.version = android.defaultConfig.versionName
            // Add other pom properties here if you want (developer details / licenses)
            repository(url: "file:./releases/")
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.0.1'
    }
    

    The above example is a stock library build.gradle as Android Studio 1.4 produces it for a new library but with the Maven Gradle plugin added and configured.

    The second line: apply plugin: 'maven', adds the plugin. The uploadArchives closure configures it.

    • pom.groupId is the name of the organization/group. This will be written in the .pom file but is primarily used to actually find your library as a remote dependency.
    • pom.artifactId is the name of your library. Again, this will be put in the .pom file but is used to actually find your lib.
    • pom.version is the version of your library. You will have to increase this when you make a new release. Also put in the .pom and used to locate your lib.

    The line repository(url: "file:./releases/") configures the plugin to write the structure to a filesystem directory relative to the project root.

    If you run the following Gradle command in the root directory of your project you should see the Maven directory structure being built.

    ./gradlew clean uploadArchives
    

    This will first clean your build and then build and perform the uploadArchives task which will create the releases directory.

    Put your library with meta data in a repository

    You already have a local Maven repository right now, namely on your local filesystem in your lib-util's project directory.

    To make it an online remote Maven repository it only has to be reachable by http (or https) get-requests. If you have a server with a webserver like Apache or Nginx or IIS you can configure that to host the files. Or you can check them into your Github account and let that host it. You could even copy those files to Dropbox and use it to host them.

    If you want to have it publicly available over Maven Central or JCenter you will have to go to their websites and register for an account where you claim your 'groupId' and you can then use their systems to upload the files so they will be hosted by them.

    Tell your project to look in that repository for dependencies

    By default Android Studio registers your project to look for dependencies in JCenter. Previously it used to default to Maven Central. So if you have actually uploaded your library to those repositories you wont have to do anything as it will already work.

    Suppose you want to configure your project to look for remote dependencies on your local filesystem in /Users/rob/projects/lib-util/releases, and in a self-hosted Maven repository at http://developer.example.com/repo/. Add those repository urls to your project's build.gradle (so not in your library's):

    project build.gradle:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    
    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.3.0'
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            maven { url '/Users/rob/projects/lib-util/releases/' }
            maven { url 'http://developer.example.com/repo/' }
            jcenter()
        }
    }
    

    The example above is the stock project build.gradle as Android Studio provides it with just two extra repositories. Looking dependencies up goes in the order listed. So in this case it first checks the local filesystem, then the self-hosted repo and finally it looks in JCenter.