Publishing an Android Library to Maven Central with Gradle
I recently migrated the We Heart It Android app project to Android Studio and I thought I’d migrate the Android-ImageManager library into the new format as well. That was the easy part. Another thing I wanted to do was to upload it to Maven Central, so it would make people’s lives easier when using it.
Ok this was painful. Here are the steps I took in order to achieve it, so hopefully people won’t struggle that much doing it in the future:
- Follow Chris Banes’ steps;
- Follow the steps here to configure the upload username and password;
- Make sure you edit
gradle.properties
andbuild.gradle
with your library’s information (version, description, links, etc); - Follow this guide. To summarize, you’ll need to create a Sonatype JIRA account and then you’ll have to create an issue there in order to create your project. That is basically apply for open source project hosting (that’s basically the steps 2 and 3 on the user guide mentioned above);
- Wait for your issue to be “Resolved”, that may take up to 2 business days (!!!). Someone from Sonatype will do it and you’ll get an email;
- Run
./gradlew clean build uploadArchives
to build and upload your project into Sonatype. IMPORTANT: You have to upload a Debug build first, otherwise it is not gonna work. You do that by adding-SNAPSHOT
at the end of yourVERSION_NAME
ongradle.properties
, like this, eg.:VERSION_NAME=1.0.0-SNAPSHOT
- After that, if all went well, you should see your project here. You can navigate through the folders. My project’s group id was
com.felipecsl.android
, so I could find it at http://oss.sonatype.org/content/repositories/snapshots/com/felipecsl/android/
I still havent figured out how to promote a Staging Repository. Right now it is failing to close the repository since the package is not signed. I will update this post once I have a solution.
In the meantime, you can already use your snapshot with Maven/Gradle.
Here is how your build.gradle
would look like:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
mavenCentral()
}
dependencies {
compile 'com.felipecsl.android:library:1.0.0-SNAPSHOT'
}
Well that is pretty much it for now. It took me almost an entire afternoon to figure this all out, hope it is useful for other people. If you’re interested in a simpler and a lot more elegant solution for Open Source library project hosting, check rubygems.org which is used to, guess what, host Ruby gems. That is really piece of cake compared to all this Java mess :sigh: