Host your own Maven Repository for Android

Android Maven

Gradle custom maven server

Android Studio is my favorite development enviroment because it is easy and smart to use. Your project almost needs 3rd party libraries like Retrofit. In most cases you insert the dependencies from jcenter or maven central:

dependencies {
  compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
}

Android Studio scans now jcenter or maven central for the dependency and downloaded it.

In this article i explain how do you host your own maven repository in your local network infrastructure. This can be helpful, because you have a library which are used by many developers in your company but you dont want to publish it to the wide world.

The requirements for the following tutorial are:

  • Android Studio
  • Gradle
  • Microsoft IIS Server

First of all we configure our IIS Server to deploy the Android library. Open your IIS Manager and create a virtual folder and call it maven. I think your location on the server file system is “D:\iis\maven”. After you created the virtual folder you have to add special mime types for this folder, to allow that a client can get this files from your server. You have to add the following mime types

  • .aar application/java-archive
  • .pom text/xml
  • .md5 text/plain
  • .sha1 text/plain

Now you have to share this folder with read and write permissions to access it in the network with an UNC path e.g. \\server\maven. Thats completed the configuration for the IIS-Server.

Next you have to open the Android Studio with the library project which you want to deploy. Open the build.gradle file from the module. You have to insert the following lines under the apply plugin: ‘com.android.library’

apply plugin: 'com.android.library'

group = "com.example"
version = "1.0"

At the end of the file you have to insert the following line

apply from: 'publish_maven.gradle'

The complete build.grade file looks like this

apply plugin: 'com.android.library'

group = "com.example"
version = "1.0"

android {
    compileSdkVersion 23
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'com.android.support:support-annotations:23.0.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'

}

apply from: 'publish_maven.gradle'

If you want to build your project you get an error, because we haven’t create the publish_maven.gradle. Thats the next step to your self hosted maven repository.

Create a new file in the module folder and name it “publish_maven.gradle”. In this file you have declare your information for deploying the library. The complete file looks like this

apply plugin: 'maven'

afterEvaluate{ project->
    uploadArchives{
        repositories{
            mavenDeployer{
                repository(url: 'file:\\\\server\\maven\\')

                pom.project {
                    name 'Example Library'
                    packaging 'aar'
                    description 'Example Library to test deploying'
                    url 'https://github.com/exampleRepository'

                    scm {
                        url 'scm:git@github.com/exampleRepository.git'
                        connection 'scm:git@github.com/exampleRepository.git'
                        developerConnection 'scm:git@github.com/exampleRepository'
                    }

                    licenses {
                        license {
                            name 'Apache Licence 2.0'
                            url 'http://www.apache.org/licences/LICENCE-2.0'
                            distribution 'repo'
                        }
                    }

                    developers {
                        developer {
                            id 'steffenwellner'
                            name 'Steffen Wellner'
                            email 'steffen.wellner@XXXXXXX.com'
                        }
                    }
                }
            }
        }
    }

    task androidSourcesJar(type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.sourceFiles
    }
    artifacts {
        archives androidSourcesJar
    }
}

buildscript{
    repositories{
        jcenter()
    }
    dependencies{
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.github.dcendents:android-maven-plugin:1.2'
    }
}

The text which is marked bold must be replaced with your server UNC path. Now we can deploy the library to the server. Open the terminal window in Android Studio and write

gradle uploadArchives

After the build which was hopefull successfully you can now see that a new folder is created on your server. If you step into this folder you can see that the gradle build script created a module-1.0.pom and a module-1.0.aar files. Now you have published your first maven repository on your server.

To use the library in your project you have to tell maven which server is used. In your build.gradle file from your app you have to declare this.

repositories {
    maven{
        url 'http://yourserver.com/mavendirectory'
        artifactUrls 'http://yourserver.com/mavendirectory'
    }
    jcenter()
    flatDir {
        dirs 'libs'
    }
}

dependencies{
    compile 'com.example:modulename:1.0'
}

Build your app and have some fun.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s