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:
1 2 |
<span style="font-weight: 400;">compile 'com.android.support.constraint:constraint-layout:1.0.1' </span> |
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:
1 |
<span style="font-weight: 400;"> compile ‘com.subwax.toastlibrary:CustomToast:0.0.1`</span> |
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.
- Click on Add New Repository
2 . Enter the name and select type maven from drop down and enter description of library
your repository will show in list like below
3 . Click on CustomToast you will see the detail of repository where you will found the link Add New Package click on it
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
Now your package will show inside repository like bellow .
5 .Click on Package detail and Add new Version
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
1 2 3 |
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
1 |
apply plugin: 'maven' |
Step 2
Paste the following script at the end of library module build.gradle file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<span style="font-weight: 400;">ext {</span> <span style="font-weight: 400;"> PUBLISH_GROUP_ID = 'com.subwax.toastlibrary'</span> <span style="font-weight: 400;"> PUBLISH_ARTIFACT_ID = 'CustomToast'</span> <span style="font-weight: 400;"> PUBLISH_VERSION = '0.0.1'</span> <span style="font-weight: 400;">}</span> <span style="font-weight: 400;">def groupId = project.PUBLISH_GROUP_ID</span> <span style="font-weight: 400;">def artifactId = project.PUBLISH_ARTIFACT_ID</span> <span style="font-weight: 400;">def version = project.PUBLISH_VERSION</span> <span style="font-weight: 400;">def localReleaseDest = "${buildDir}/release/${version}"</span> <span style="font-weight: 400;">task androidJavadocs(type: Javadoc) {</span> <span style="font-weight: 400;"> failOnError = false</span> <span style="font-weight: 400;"> source = android.sourceSets.main.java.srcDirs</span> <span style="font-weight: 400;"> ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"</span> <span style="font-weight: 400;"> classpath += files(ext.androidJar)</span> <span style="font-weight: 400;">}</span> <span style="font-weight: 400;">task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {</span> <span style="font-weight: 400;"> classifier = 'javadoc'</span> <span style="font-weight: 400;"> from androidJavadocs.destinationDir</span> <span style="font-weight: 400;">}</span> <span style="font-weight: 400;">task androidSourcesJar(type: Jar) {</span> <span style="font-weight: 400;"> classifier = 'sources'</span> <span style="font-weight: 400;"> from android.sourceSets.main.java.srcDirs</span> <span style="font-weight: 400;">}</span> <span style="font-weight: 400;">uploadArchives {</span> <span style="font-weight: 400;"> repositories.mavenDeployer {</span> <span style="font-weight: 400;"> pom.groupId = groupId</span> <span style="font-weight: 400;"> pom.artifactId = artifactId</span> <span style="font-weight: 400;"> pom.version = version</span> <span style="font-weight: 400;"> // Add other pom properties here if you want (developer details / licenses)</span> <span style="font-weight: 400;"> repository(url: "file://${localReleaseDest}")</span> <span style="font-weight: 400;"> }</span> <span style="font-weight: 400;">}</span> <span style="font-weight: 400;">task zipRelease(type: Zip) { </span> <span style="font-weight: 400;"> from localReleaseDest</span> <span style="font-weight: 400;"> destinationDir buildDir</span> <span style="font-weight: 400;"> archiveName "release-${version}.zip"</span> <span style="font-weight: 400;">}</span> <span style="font-weight: 400;">task generateRelease << {</span> <span style="font-weight: 400;"> println "Release ${version} can be found at ${localReleaseDest}/"</span> <span style="font-weight: 400;"> println "Release ${version} zipped can be found ${buildDir}/release-${version}.zip"</span> <span style="font-weight: 400;">}</span> <span style="font-weight: 400;">generateRelease.dependsOn(uploadArchives)</span> <span style="font-weight: 400;">generateRelease.dependsOn(zipRelease)</span> <span style="font-weight: 400;">artifacts {</span> <span style="font-weight: 400;"> archives androidSourcesJar</span> <span style="font-weight: 400;"> archives androidJavadocsJar</span> <span style="font-weight: 400;">} </span> |
This script contain different tasks of gradle like generate javadoc of your source code by using androidJavadocs and androidJavadocsJar tasks.
1 2 3 4 5 6 7 8 9 10 |
<span style="font-weight: 400;">task androidJavadocs(type: Javadoc) {</span> <span style="font-weight: 400;"> failOnError = false</span> <span style="font-weight: 400;"> source = android.sourceSets.main.java.srcDirs</span> <span style="font-weight: 400;"> ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"</span> <span style="font-weight: 400;"> classpath += files(ext.androidJar)</span> <span style="font-weight: 400;">}</span> <span style="font-weight: 400;">task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {</span> <span style="font-weight: 400;"> classifier = 'javadoc'</span> <span style="font-weight: 400;"> from androidJavadocs.destinationDir</span> <span style="font-weight: 400;">}</span> |
Generate jar of your library source code by using this task androidSourcesJar.
1 2 3 4 |
<span style="font-weight: 400;">task androidSourcesJar(type: Jar) {</span> <span style="font-weight: 400;"> classifier = 'sources'</span> <span style="font-weight: 400;"> from android.sourceSets.main.java.srcDirs</span> <span style="font-weight: 400;">}</span> |
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
1 2 |
<span style="font-weight: 400;">generateRelease.dependsOn(uploadArchives)</span> <span style="font-weight: 400;">generateRelease.dependsOn(zipRelease)</span> |
Step 3
Click on terminal in Android studio and enter the following gradle command to run our task we have created in step 2
1 |
./gradlew clean build generateRelease |
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.
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.
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.
Now you can use CustomToast library inside any android Project by using the
1 |
compile 'com.subwax.toastlibrary:CustomToast:0.0.1'<strong> </strong> |
inside your build.gradle file.