DevCrew I/O
  • Home
  • Portfolio
  • Careers
  • About Us
  • Contact Us
  • Blogs
DevCrew I/O - 47806 ba82 4 1
March 24, 2017

How to write and distribute Android Library Project (part – 2)

By Site Admin

In our last post we created a custom toast library. Now it’s time to distribute it.

Time to Distribute your Library

After testing your library, you can distribute it at the public repository as discussed in the part 1 of this post. Before upload the library, we should know how android studio fetch these libraries from server. It is nothing complicated, we just understand the string  that we used to add inside our app build.gradle file. For example:

compile 'com.android.support.constraint:constraint-layout:1.0.1'

This string consists of three parts: [GROUP_ID] : [ARTIFACT_ID] : [VERSION]

GROUD_ID explain the group of libraries like the ones you have. You can write your project package name as anything that makes sense. ARTIFACT_ID is basically the real name of your library like we have CustomToast and VERSION is version name that is usually write in a.b.c pattern follow by any string like ‘0.0.1-beta’ etc.

For our library we construct it like this:

 compile ‘com.subwax.toastlibrary:CustomToast:0.0.1`

When we  write this line compile ‘com.subwax.toastlibrary:CustomToast:0.0.1’ in build.gradle file android studio search this library at  jcenter  to fetch and compile the source code inside your android project. That’s All!
It’s time to create account at Bintary. After create the account you have to create repository (ARTIFACT_ID) that name will be the name of your library in our case we have CustomToast and after that we will add package name (GROUP_ID) that is com.subwax.toastlibrary in our case.

  1. Click on Add New Repository

DevCrew I/O - Screen Shot 2017 03 14 at 4.08.21 PM
2 . Enter the name and select type maven from drop down and enter description of library
DevCrew I/O - Screen Shot 2017 03 14 at 4.05.32 PM
your repository will show in list like below
DevCrew I/O - Screen Shot 2017 03 14 at 4.08.21 PM 1
3 . Click on CustomToast you will see the detail of repository where you will found the link Add New Package click on it
DevCrew I/O - Screen Shot 2017 03 14 at 4.10.16 PM
4. Enter the description of your package and give the link of your version control server like github etc and create the package by click on Create Package
DevCrew I/O - Screen Shot 2017 03 14 at 1.10.02 PM
        Now your package will show inside repository like bellow .
DevCrew I/O - Screen Shot 2017 03 14 at 4.19.16 PM
   5 .Click on Package detail and Add new Version
DevCrew I/O - Screen Shot 2017 03 14 at 1.11.42 PM
give the version detail after create this every thing have set. Now we need files that maven repository needed like some  jar files of our source and java docs and some xml files like pom.
 

Generate source jar files

Step 1
Add Maven plugin to generate some file required by maven repository like POM etc. Add following code after buildscript block inside your Project main build.gradle file

plugins {
   id "com.github.dcendents.android-maven" version "1.5"
}

After that apply the maven plugin inside your library module build.gradle file. Write the below code at the top of your file

apply plugin: 'maven'

Step 2
Paste the following script at the end of library module build.gradle file

ext {
   PUBLISH_GROUP_ID = 'com.subwax.toastlibrary'
   PUBLISH_ARTIFACT_ID = 'CustomToast'
   PUBLISH_VERSION = '0.0.1'
}
def groupId = project.PUBLISH_GROUP_ID
def artifactId = project.PUBLISH_ARTIFACT_ID
def version = project.PUBLISH_VERSION
def localReleaseDest = "${buildDir}/release/${version}"
task androidJavadocs(type: Javadoc) {
   failOnError = false
   source = android.sourceSets.main.java.srcDirs
   ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
   classpath += files(ext.androidJar)
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
   classifier = 'javadoc'
   from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
   classifier = 'sources'
   from android.sourceSets.main.java.srcDirs
}
uploadArchives {
   repositories.mavenDeployer {
       pom.groupId = groupId
       pom.artifactId = artifactId
       pom.version = version
       // Add other pom properties here if you want (developer details / licenses)
       repository(url: "file://${localReleaseDest}")
   }
}
task zipRelease(type: Zip) {

   from localReleaseDest
   destinationDir buildDir
   archiveName "release-${version}.zip"
}
task generateRelease << {
   println "Release ${version} can be found at ${localReleaseDest}/"
   println "Release ${version} zipped can be found ${buildDir}/release-${version}.zip"
}
generateRelease.dependsOn(uploadArchives)
generateRelease.dependsOn(zipRelease)
artifacts {
   archives androidSourcesJar
   archives androidJavadocsJar
}

This script contain different tasks of gradle like generate javadoc of your source code by using  androidJavadocs and  androidJavadocsJar tasks.

task androidJavadocs(type: Javadoc) {
   failOnError = false
   source = android.sourceSets.main.java.srcDirs
   ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
   classpath += files(ext.androidJar)
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
   classifier = 'javadoc'
   from androidJavadocs.destinationDir
}

Generate jar of your library source code by using this task  androidSourcesJar.

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

We also generate the zip of all file by using  zipRelease task. We also need POM file that are also needed by maven by using uploadArchives block. Next we create generateRelease task that is depend on uploadArchives and zipRelease tasks

generateRelease.dependsOn(uploadArchives)
generateRelease.dependsOn(zipRelease)

 
Step 3
Click on terminal in Android studio and enter the following gradle command to run our task we have created in step 2

./gradlew clean build generateRelease

DevCrew I/O - Screen Shot 2017 03 14 at 1.25.40 PM
 
after build successfully jar files of source and  javadoc, release-0.0.1.zip will save in library module`s build folder.
Step 4
We can upload the these file by using bintary plugin inside gradle script also we will learn about it later in other post. Upload this zip file on bintary repository that we have created. Click the version number it will take you detail of version.
DevCrew I/O - Screen Shot 2017 03 14 at 1.29.39 PM
Then find Upload files at the top of page and drag the zip file and upload . After upload it will ask from you to publish these file confirm it then your library has published to use.
 
DevCrew I/O - Screen Shot 2017 03 14 at 1.28.16 PM 1
 
DevCrew I/O - Screen Shot 2017 03 14 at 1.30.05 PM 1
 
Step 5 
Add your library to jcenter but wait for 2-3 hours to let bintray team approves our request. Once sync request is approved, you will receive an email informing you the change. Now let’s check the web interface, you will see some change in Linked To section.
DevCrew I/O - Screen Shot 2017 03 15 at 5.53.30 PM
 
Now you can use CustomToast library inside any android Project by using the

compile 'com.subwax.toastlibrary:CustomToast:0.0.1' 

inside your build.gradle file.
 
 
 
 
 

subscribe desktop background

Straight from our desk, to your Inbox.

Subscribe to our newsletter

    footer logo
    • Home
    • Portfolio
    • Careers
    • About Us
    • Contact Us
    • Blogs
    • DevCrew I/O - fb icon
    • DevCrew I/O - insta icon
    • DevCrew I/O - linkedin icon
    • DevCrew I/O - youtube icon
    • DevCrew I/O - snapchat icon

    Copyright ©2023 DevCrew I/O. All Rights Reserved.