If you developed some android library that is useful in multiple projects then there are two ways to utilize it.

First Way:

You develop library and add it in your project or share with other team members. It is easy to use and share. But if you update library then you need to add again it in all your projects and share again with your team members it is painful task when you have use it in multiple projects.

Second Way:

You develop library publish it in repository and add it to your project as a compile dependency or share just compile dependency with your team members.

Easy to update library in all your projects in simple way just add compile dependency and if you update library then release and publish library again with new version in your repository.

Then change only library version no and sync project to get all updates made in library project.

Here are some Repository managers,

  1. JitPack
  2. JFrog Artifactory
  3. JFrog Bintray
  4. Apache Archiva
  5. Sonatype Nexus

In this blog, we will discuss JitPack and JFrog Artifactory further.

Get started

For Public Git Repository

Prerequisites

  • Git installed on your PC.
  • Basic knowledge of operation performed on Git.
  • For public Git repository you can use JitPack.

 

What is JitPack?

According to official documentation, JitPack is a novel package repository for JVM and Android projects. It builds Git projects on demand and provides you with ready-to-use artifacts (jar, aar).

You can build your public or private Git repository artifacts using JitPack. For public Git repositories JitPack is free. And for private repositories you need JitPack subscription.

 

Step 1: Open this link https://jitpack.io/

JitPack Site Page
JitPack Site Page

 

 Step 2: Enter Git repo URL and press Look up button. You can show below options.

JitPack Get Artifacts Section
JitPack Get Artifacts Section

 

 Step 3: Click on Get it button.

JitPack How to use Dependency Section
JitPack How to use Dependency Section

 

Step 4: Your can share library with anyone. By adding following line module level gradle to use this library.

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies {
   compile 'com.github.piyush-malaviya:ClockView:v1.0.0'
}

 

For Private Git Repository

For private repository you can use JFrog Artifactory. You can setup it on your dedicated server or on local server. We are going to learn about hosting JFrog Artifactory on local pc.

 

What is JFrog Artifactory?

According to official documentation, JFrog’s Artifactory open source project was created to speed up development cycles using binary repositories. It’s the world’s most advanced repository manager, creating a single place for teams to manage all their binary artifacts efficiently.

Step 1: Open site: https://www.jfrog.com/open-source/ and download JFrog Artifactory Zip file.

JFrog Artifactory Site Url
JFrog Artifactory Site Url

Step 2: Extract zip file and open terminal and start Artifactory service by running artifactory.sh file in ubuntu or artifactory-service.exe in window . After successful running Artifactory you service you will get below message in ubuntu terminal.

JFrog Artifactory Successful Message in terminal
JFrog Artifactory Successful Message

 

Step 3: Now open the following link in your browser http://localhost:8081/artifactory/webapp/#/home.

JFrog Artifactory Set Admin Password
JFrog Artifactory Set Admin Password

 

By default your username is admin. Then set Admin password and press next button.

JFrog Artifactory Configure Proxy Server
JFrog Artifactory Configure Proxy Server

 

Configure a proxy server if you want to access repository remotely or skip.

JFrog Artifactory Create Repository
JFrog Artifactory Create Repository

 

Create repository by selecting package type maven.

JFrog Artifactory Home
JFrog Artifactory Home

 

Step 4: Now you have to add following lines in your library module gradle file, project level gradle file and gradle.properties file.

Add following lines in your gradle.properties file,

artifactory_repocitory_key=libs-release-local
artifactory_contextUrl=http://localhost:8081/artifactory
artifactory_user=your user name of artifactory
artifactory_password=your password given by you when you set up artifactory

 

Add following lines in your library project level gradle file,

buildscript {
   repositories {
       jcenter()
   }

   dependencies {
       classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.4.17"
   }
}

 

Add following lines in your library module gradle file,

apply plugin: 'com.android.library'
apply plugin: "com.jfrog.artifactory"
apply plugin: 'maven-publish'

def packageName = 'com.ln.mylibrary'
def libraryVersion = '1.0.0'

publishing {
    publications {
        aar(MavenPublication) {
            groupId packageName
            version = libraryVersion
            artifactId project.getName()

            artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
        }
    }
}

artifactory {
    //The base Artifactory URL if not overridden by the publisher/resolver
    contextUrl = artifactory_contextUrl
   // defining publishing information  
   publish {
        repository {
            //The Artifactory repository key to publish to
            repoKey = artifactory_repocitory_key
            //The publisher user name
            username = artifactory_user
            //The publisher user name
            password = artifactory_password
            maven = true
        }
        defaults {
            // which artifacts should be published to Artifactory.
            publications('aar')
            publishArtifacts = true
            
            // Properties to be attached to the published artifacts.
            properties = ['qa.level': 'basic', qa.os': 'ubuntu']
            //Publish generated POM files to Artifactory (true by default).
            publishPom = true
        }
    }
}

Step 5: Build your project and upload generated artifacts to Artifactory.

From Android Studio open View > Tool Windows > Terminal. and initiate invoke the assembleRelease task.

For ubuntu,
./gradlew assembleRelease

For window,
gradlew.bat assembleRelease
Assemble release command in terminal
Assemble Release

 

After successfully build. run following command to upload artifacts to Artifactory.

For ubuntu, 
./gradlew artifactoryPublish

For window,
gradlew.bat artifactoryPublish
Publish Artifact command in terminal
Publish Artifact

 

Now, you can see your published library is available in local JFrog Artifactory repository.

JFrog Artifactory Repository
JFrog Artifactory Repository

 

How to use the library in projects?

Add following lines in your projects to use library in your project and share this compile dependency with your team members to use this library.

repositories {
    maven { url "http://localhost:8081/artifactory/libs-release-local" }
}

dependencies {
    compile 'com.ln.mylibrary:mylibrary:1.0.0'
}

 

LetsNurture is a leading IT service provider that promises to deliver the best solution for your technical requirements. With a vast variety of industry experience, we have served more than 100 clients across the globe. We have an in-house expertise to cater the client’s requirements for Mobile App development, Android app development, and iOS app development.

To know more please visit our portfolio. Do write us at info@letsnurture.com.

Want to work with us? We're hiring!