Public Repositories
When it comes to our work, the project’s code is mainly stored in repositories. It’s a very convenient solution for both continuous development and cooperation with other users. But when we want to share the final version of our project’, it’s better to use distribution repositories such as: jcenter or mavenCentral. These two are very well known to Android Developers. They allow easy downloading and managing dependencies in their applications, for example:
allprojects { repositories { jcenter() mavenCentral() } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.some:library:1.2.3' compile 'com.another:library:2.3.4' }
Distributions servers store only generated archives of our projects without their source code.
Every distribution has to have a special name, for example: com.some:library:1.2.3 according to the following scheme: GROUP_ID:ARTIFACT_ID:VERSION we will name our library following this pattern.
Before setting up our project, we need to create an account on bintray.com. This service has a lot of plans at different prices, yet the open source software is completely free.
The next step is to generate and send the GPG keys to bintray.com. With this piece of information, the service will identify us as the authors of published libraries. We generate keys the following way:
gpg --gen-key
All the questions related to keys creation can be answered with default values. The password should be difficult to guess. If we don’t have gpg in our system, we can install it with: brew install gpg (OSX) or even use the online generator. Once they keys are generated in the terminal we have to publish and export them.
gpg --list-keys ------------------------------- pub 2048R/11A203AB 2016-07-23 uid Your Name (yourNick) <your.name@mail.com> sub 2048R/1A2BB12F 2016-07-23
To publish our public key we have to run the following command:
gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 11A203AB
Here we obviously use our key’s ID. If we had generated keys by another tool, we could also send public key using form on the site. The last step is to export public and private key:
gpg -a --export your.name@mail.com > pub_key.asc gpg -a --export-secret-key your.name@mail.com > priv_key.asc
If everything has worked out well, we can copy the keys to bintray.com (Your profile -> edit -> GPG Signing).
Library Project in Android Studio
When we’re done with the process of creating and generating keys, we can go straight to Android Studio and start working on our Library. It’s a good idea to divide our project into two modules: library and example how to use it. This layout lets us keep order. Here is the example of this structure.
You can create library module (timertextview) by choosing from the main menu: File -> New -> New module… -> Android Library. Our example of use (timertextview-sample) is a standard Android application. If you want to create a structure like this, you must modify the file: settings.gradle :
include ':timertextview', ':timertextview-sample' rootProject.name = 'timertextview-root'
In this project we can distinguish several key configuration files:
rootProject/gradle.properties GROUP=com.your.name VERSION_NAME=0.0.1 POM_DESCRIPTION=Your library description POM_URL=https://github.com/yourname/lib POM_SCM_URL=https://github.com/yourname/lib POM_SCM_CONNECTION=scm:git:git://github.com/yourname/lib.git POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com:yourname/lib.git POM_ISSUE_URL=https://github.com/yourname/lib/issues POM_LICENCE_NAME=The Apache Software License, Version 2.0 POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt POM_LICENCE_DIST=repo POM_DEVELOPER_ID=yourName POM_DEVELOPER_NAME=Your Name
This is the place to provide our group’s ID (it’s the first part of our unique name), current version, description and link to the code repository.
yourLibrary/gradle.properties POM_ARTIFACT_ID=yourlibrary POM_NAME=YourLibrary POM_PACKAGING=aar
A separate module configuration allows independent distribution (we can also do the distribution for “example of usage”). POM_ARTIFACT_ID is the second part of our distribution’s name, the third one is the earlier given version.
rootProject/local.properties bintray.user=YourBinTrayUserName bintray.apikey=YourBinTrayApiKey bintray.gpg.password=YourKeyPassword
This configuration file should be ignored in our repository (look .gitignore), because it contains confidential data. user is our account’s name in bintray.com, apikey comes from bintray profile (Your profile -> Edit -> API Key), gpg.password is the password given when creating GPG keys.
rootProject/gradle/gradle-mvn-push.gradle
This file uses the official Bintray Gradle Plugin, which allows uploading the library we built to bintray.com. I recommend you copy and, if possible, modify it after reading the plugin documentation. As the last configuration step, add this file in the library’s build.gradle as the last line:
yourLibrary/build.gradle // Last line in file apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
Distribution publishing
If you’ve gotten this far, it means that you’re ready to publish your distribution. Sending the library to the server looks like this:
./gradlew build bintrayUpload
From this moment, our compiled code will be available to other users. The following dependencies configuration is necessary to use it :
repositories { maven { url 'https://dl.bintray.com/yourName/maven/' } } dependencies { compile 'com.your.name:yourlibrary:0.0.1' }
Additionally, we can add our repository to jcenter . This eliminates the need to define the direct URL to our distribution. We can do this by visiting our published distribution: https://bintray.com/yourName/maven/yourLibrary and clicking on “Add to JCenter”. After having our application accepted our (on average it takes about an hour) we will be able to replace the entire URL by jcenter ().
Initially, the whole setup process of distribution may seem complicated. Don’t be misled! It’s totally worth your work, especially if you want to share the code in a professional manner.