4

I have Intellij Idea 2016.1 gradle-application(Not Android) written in Kotlin. I need to place it in jCenter and add to the Android-application as a dependency. I can not find any information about it. All references refer to the Android project. I have no concept of how this can be done. Looking for detailed instructions

Michael
  • 53,859
  • 22
  • 133
  • 139

2 Answers2

3

Publishing Kotlin libraries to jCenter is absolutely the same as for JAva libraries.

buildscript {
  ext.kotlinVersion = '1.0.2'

  repositories {
    mavenCentral()
  }

  dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
  }
}

plugins {
  id "com.jfrog.bintray" version "1.6"
}

apply plugin: 'kotlin'
apply plugin: 'maven-publish'

group '<groupId>'
version '<version>'
sourceCompatibility = 1.6

repositories {
  mavenCentral()
}

dependencies {
  compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
}

bintray {
  user = '<bintray user name>'
  key = '<bintray API key>'
  publications = ['mavenJava']

  pkg {
    repo = '<repository name on bintray>'
    name = '<artifactId>'

    version {
      name = project.version
      released = new Date()
      vcsTag = "v${project.version}"
    }
  }
}

task sourcesJar(type: Jar, dependsOn: project.classes) {
  from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: project.javadoc) {
  from javadoc.destinationDir
}

artifacts {
  archives sourcesJar, javadocJar
}

publishing {
  publications {
    mavenJava(MavenPublication) {
      artifactId project.bintray.pkg.name
      from components.java

      artifact sourcesJar {
        classifier = 'sources'
      }
      artifact javadocJar {
        classifier = 'javadoc'
      }
    }
  }
}

Create a repository on bintray and then just call ./gradlew bintrayUpload. If you don't ask the bintray plugin to publish an uploaded dependency then you have to publish it manually from your bintray account.

When the dependency is published you can link it to jCenter from your account. After it's published on jCenter you can use it as a regular dependency from an Android project.

Michael
  • 53,859
  • 22
  • 133
  • 139
1

Michael's answer is correct. But if you execute bintrayUpload, the kotlin dependency will be included to the .pom file. This lead to adding kotlin libs twice when you include your lib in gradle-project. This lead to DuplicateFileException. I don't know what's wrong. Maybe I did some stupid mistake. But if this is your case to, you can execute gradlew build with Michael's build.gradle for generating .jar and source.jar. Then you can create your own .pom file with no dependencies and deploy 3 files(jar, source.jar and .pom) to bintray manually. Then link library to jCenter.

For example:

Generated .pom with ./gradlew bintrayUpload:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ru.livemaster</groupId>
  <artifactId>LongToWords</artifactId>
  <version>0.1</version>
  <dependencies>
    <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-stdlib</artifactId>
      <version>1.0.2</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>

Your own .pom for deploying without kotlin-dependencies:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ru.livemaster</groupId>
  <artifactId>LongToWords</artifactId>
  <version>0.1</version>
</project>