|
| 1 | +--- |
| 2 | +type: tutorial |
| 3 | +layout: tutorial |
| 4 | +title: "Working with Kotlin/Native Libraries" |
| 5 | +description: "A look at how to work with Kotlin/Native libraries" |
| 6 | +authors: Hadi Hariri |
| 7 | +date: 2018-01-22 |
| 8 | +showAuthorInfo: false |
| 9 | +--- |
| 10 | + |
| 11 | + |
| 12 | +In this tutorial we'll see |
| 13 | + |
| 14 | +* [What is a Kotlin/Native Libary](#what-is-kotlin/native-library) |
| 15 | +* [How to create a Kotlin/Native Library](#creating-a-kotlin/native-libraries) |
| 16 | +* [How to consume Kotlin/Native libraries](#consuming-a-kotlin/native-library) |
| 17 | +* [How to use and create library repositories](#using-and-creating-library-repositories) |
| 18 | + |
| 19 | + |
| 20 | +## What is Kotlin/Native Library |
| 21 | + |
| 22 | +The concept of a library, a collection of functions and other resources such as images, to be used by applications is available in Kotlin/Native and it is represented |
| 23 | +as a file with the extension `klib`. Kotlin/Native libraries are artifacts that are only available at compile time, i.e. we do not ship these as dependencies to our application |
| 24 | +(as we would for instance with DLL files on the Windows platform). |
| 25 | + |
| 26 | +A `klib` file is a compressed archive in zip format with the following directory structure: |
| 27 | + |
| 28 | +File `utils.klib` |
| 29 | + |
| 30 | +``` |
| 31 | + targets/ /* One or more target platforms */ |
| 32 | + |
| 33 | + macbook/ /* When targeting macbook, this entry would appear */ |
| 34 | + |
| 35 | + kotlin/ /* Kotlin code compiled to LLVM bitcode */ |
| 36 | + |
| 37 | + native/ /* Bitcode for additonal native objects */ |
| 38 | + |
| 39 | + linux/ /* When targeting Linux, this entry would appear */ |
| 40 | + |
| 41 | + kotlin/ |
| 42 | + |
| 43 | + native/ |
| 44 | + |
| 45 | + linkdata/ /* A set of [ProtoBuf](https://github.com/google/protobuf) files with serialised linkage metadata */ |
| 46 | + |
| 47 | + resources/ /* Resource files such as images, etc. */ |
| 48 | +
|
| 49 | + manifest /* A file in Java Property Format describing the library */ |
| 50 | +``` |
| 51 | + |
| 52 | +## Creating a Kotlin/Native Libraries |
| 53 | + |
| 54 | +There are two ways we can create a Kotlin/Native library. The first and most common way is to use the Kotlin compiler. Let's assume |
| 55 | +we create a small library called `utils.kt` which contains the following two functions |
| 56 | + |
| 57 | +```kotlin |
| 58 | +package utils |
| 59 | + |
| 60 | +fun printMessage(message: String) { |
| 61 | + println("Message: $message") |
| 62 | +} |
| 63 | + |
| 64 | +fun printWarning(warning: String) { |
| 65 | + println("Warning: $warning") |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +To create a library from this file, we can use the compiler with the parameter `-produce library` or `-p library` for short: |
| 70 | + |
| 71 | + konanc utils.kt -p library |
| 72 | + |
| 73 | +By default, the output of the filename is `library.klib`. We can override it using the `-output` or `-o` parameter: |
| 74 | + |
| 75 | + konanc utils.kt -p library -o utils |
| 76 | + |
| 77 | + |
| 78 | +The second way to create a library is using the `cinterop` tool which allows us to create a Kotlin/Native library from an existing |
| 79 | +C library. See the [Interop with C tutorial](interop-with-c.html) on how to accomplish this. |
| 80 | + |
| 81 | +## Consuming a Kotlin/Native Library |
| 82 | + |
| 83 | +Now that we have our library, we can use it in our application. In our case this is a simple file named `sample.kt` with the following contents: |
| 84 | + |
| 85 | +```kotlin |
| 86 | +import utils.* |
| 87 | + |
| 88 | +fun main(args: Array<String>) { |
| 89 | + printWarning("App is about to shut down!") |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +Notice how we need to import the necessary package from the library on the first line using the `import` statement. |
| 94 | + |
| 95 | +In order for the compiler to correctly link in the library, we need to pass the library name using the `-library` or `-l` parameter |
| 96 | + |
| 97 | + konanc sample.kt -l utils |
| 98 | + |
| 99 | +This would then produce a single executable file with no runtime dependencies. |
| 100 | + |
| 101 | +## Using and creating library repositories |
| 102 | + |
| 103 | +Often it is useful to use the same library across multiple applications. To avoid having various copies of the same library, the Kotlin |
| 104 | +compiler can search for libraries in what's called a library repository. The default repository is usually installed under the folder `~/konanc/klib` and we can |
| 105 | +add and remove our own libraries with a utility named `klib` that ships as part of the compiler tools. |
| 106 | + |
| 107 | +### Installing libraries to the default repository |
| 108 | + |
| 109 | +The easiest way to install a library so that it can be later referenced by any application is to use `klib` with the following command: |
| 110 | + |
| 111 | + klib install utils |
| 112 | + |
| 113 | +This will copy and extract the necessary files to the proper locations in the default library folder, allowing us to then compile our application and link |
| 114 | +a specific library without needing to have the `klib` file in the project folder: |
| 115 | + |
| 116 | + konanc sample.kt -l utils |
| 117 | + |
| 118 | +We can of course also uninstall libraries at any point by issuing the command: |
| 119 | + |
| 120 | + klib remove utils |
| 121 | + |
| 122 | + |
| 123 | +### Installing libraries to custom repositories |
| 124 | + |
| 125 | +In addition to using the default repository, we can also have libraries stored in custom repositories, which can be useful for instance |
| 126 | +if we want to share libraries amongst a group of projects. |
| 127 | + |
| 128 | +In order to do this, we can once again use the command `klib`: |
| 129 | + |
| 130 | + klib install utils -repository jetbrains |
| 131 | + |
| 132 | +but this time adding the parameter `-repository` with the value `jetbrains`. This installs the `utils` library into a custom repository located in a subdirecty named `jetbrains` relative to where we execute the command. For instance if our project is located under the directory `/home/kotlin/projects/`, the above command would install the library to the directory `/home/kotlin/projects/jetbrains/utils`. |
| 133 | + |
| 134 | + |
| 135 | +For more information about the commands available for `klib` as well as the sequence when searching for libraries, please see the [README](https://github.com/JetBrains/kotlin-native/blob/master/LIBRARIES.md#advanced-topics) |
| 136 | + |
| 137 | + |
0 commit comments