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.
- 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
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
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
compile 'com.subwax.toastlibrary:CustomToast:0.0.1'
inside your build.gradle file.