How to publish a Kotlin-Android library to jCenter

There seems to be a lack of information out there about how to publish an Android library written in Kotlin to jCenter. If you search on how to publish an Android library to jCenter, you may have come across this post that claims to have a solution. While that solution probably worked at some point, it currently does not work. However, it does provide an important hint we can use to fix my favourite way to publish a library.

My personal favourite post on how to publish a library in Android is an article on inthecheesefactory.com. If your library is written in Kotlin, this solution keeps coming up with an error. This post is about what changes you need to make, in order to get this solution to work. I’m sure this solution worked once, however, it seems to be old and no longer works.

The main error you get when you try to use the method outlined by inthecheesefactory, is that the JavaDoc is not generated and this causes the script to abort.

The solution for this is simple

In /lib/build.gradle at the top add the dokka-android plugin after the kotlin-android plugin.

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'org.jetbrains.dokka-android'

Then, configure dokka (still under lib/build.gradle).

android {
...
}

dokka {
outputFormat = 'html'
outputDirectory = "$buildDir/javadoc"
}

Finally, inthecheesefactory.com gives you the following 2 files to include.

apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'

You need to download and edit the bintrayv1.gradle file the first block of code should look like this:

if (project.hasProperty("android")) { // Android libraries
task sourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}

task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
}

You need to make the changes below.

if (project.hasProperty("kotlin")) { //Kotlin libraries
task sourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}

task javadoc(type: Javadoc, dependsOn: dokka) {

}
} else
if (project.hasProperty("android")) { // Android libraries
task sourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}

task javadoc(type: Javadoc, dependsOn: dokka) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
}

Basically, we are telling it use the javadoc task depends on dokka. The javadoc task doesn’t do anything other than trigger dokka which is configured under lib/build.gradle.

You need to place the edited file under your lib directory and then your apply should look something like this:

apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'bintrayv1.gradle'

That should be it! You should now be able to build and deploy your library to bintray.

Finally

I haven’t put my whole code up, I’m hoping to contact inthecheesefactory and have them update their blog and their code. If this works or does not, I’ll report back here. Like the article so that you are sure not to miss out on android updated.

And, finally, In order to build great Android apps, read more of my articles.


Yay! you made it to the end! We should hang out! feel free to follow me on Medium, LinkedIn, Google+ or Twitter.