Kotlin and OSGi

To enable Kotlin OSGi support you need to include kotlin-osgi-bundle instead of regular Kotlin libraries. It is recommended to remove kotlin-runtime, kotlin-stdlib and kotlin-reflect dependencies as kotlin-osgi-bundle already contains all of them. You also should pay attention in case when external Kotlin libraries are included. Most regular Kotlin dependencies are not OSGi-ready, so you shouldn’t use them and should remove them from your project.

Maven

To include the Kotlin OSGi bundle to a Maven project:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.jetbrains.kotlin</groupId>
  4. <artifactId>kotlin-osgi-bundle</artifactId>
  5. <version>${kotlin.version}</version>
  6. </dependency>
  7. </dependencies>

To exclude the standard library from external libraries (notice that “star exclusion” works in Maven 3 only):

  1. <dependency>
  2. <groupId>some.group.id</groupId>
  3. <artifactId>some.library</artifactId>
  4. <version>some.library.version</version>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>org.jetbrains.kotlin</groupId>
  8. <artifactId>*</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>

Gradle

To include kotlin-osgi-bundle to a gradle project:

  1. compile "org.jetbrains.kotlin:kotlin-osgi-bundle:$kotlinVersion"

To exclude default Kotlin libraries that comes as transitive dependencies you can use the following approach:

  1. dependencies {
  2. compile (
  3. [group: 'some.group.id', name: 'some.library', version: 'someversion'],
  4. .....) {
  5. exclude group: 'org.jetbrains.kotlin'
  6. }

FAQ

Why not just add required manifest options to all Kotlin libraries

Even though it is the most preferred way to provide OSGi support, unfortunately it couldn’t be done for now due to so called “package split” issue that couldn’t be easily eliminated and such a big change is not planned for now. There is Require-Bundle feature but it is not the best option too and not recommended to use. So it was decided to make a separate artifact for OSGi.