创建并发布多平台库——教程

In this tutorial, you will learn how to create a multiplatform library for JVM, JS, and Native platforms, write common tests for all platforms, and publish the library to a local Maven repository.

This library converts raw data – strings and byte arrays – to the Base64 format. It can be used on Kotlin/JVM, Kotlin/JS, and any available Kotlin/Native platform.

You will use different ways to implement the conversion to the Base64 format on different platforms:

You will also test your code using common tests, and then publish the library to your local Maven repository.

You can find a similar project in this GitHub repository.

创建并发布多平台库——教程 - 图1

搭建环境

You can complete this tutorial on any operating system. Download and install the latest version of IntelliJ IDEA with the latest Kotlin plugin.

创建一个项目

  1. In IntelliJ IDEA, select File | New | Project.
  2. In the left-hand panel, select Kotlin Multiplatform.
  3. Enter a project name, then in the Multiplatform section select Library as the project template.

    Select a project template

    By default, your project will use Gradle with Kotlin DSL as the build system.

  4. Specify the JDK, which is required for developing Kotlin projects.

  5. Click Next and then Finish.

The wizard will create a sample multiplatform library with the following structure:

Multiplatform library structure

Write cross-platform code

Define the classes and interfaces you are going to implement in the common code.

  1. In commonMain/kotlin, create a new org.jetbrains.base64 directory.
  2. Create the Base64.kt file in the new directory.
  3. Define the Base64Encoder interface that converts bytes to the Base64 format:

    1. package org.jetbrains.base64
    2. interface Base64Encoder {
    3. fun encode(src: ByteArray): ByteArray
    4. }
  4. Define the Base64Factory object to provide an instance of the Base64Encoder interface to the common code:

    1. expect object Base64Factory {
    2. fun createEncoder(): Base64Encoder
    3. }

The factory object is marked with the expect keyword in the cross-platform code. For each platform, you should provide an actual implementation of the Base64Factory object with the platform-specific encoder. Learn more about platform-specific implementations.

提供平台相关实现

Now you will create the actual implementations of the Base64Factory object for each platform:

JVM

  1. In jvmMain/kotlin, create a new org.jetbrains.base64 package.
  2. Create the Base64.kt file in the new package.
  3. Provide a simple implementation of the Base64Factory object that delegates to the java.util.Base64 class:

    IDEA inspections help create actual implementations for an expect declaration.

    创建并发布多平台库——教程 - 图4

    1. package org.jetbrains.base64
    2. import java.util.*
    3. actual object Base64Factory {
    4. actual fun createEncoder(): Base64Encoder = JvmBase64Encoder
    5. }
    6. object JvmBase64Encoder : Base64Encoder {
    7. override fun encode(src: ByteArray): ByteArray = Base64.getEncoder().encode(src)
    8. }

Pretty simple, right? You’ve provided a platform-specific implementation by using a straightforward delegation to a third-party implementation.

JS

For the JS part of your project, you’ll make use of the base-64 npm package. It can be added as dependency to implement JS-specific functionality:

  1. Update your build.gradle.kts file by adding a dependency to the jsMain source set:

    1. kotlin {
    2. // ...
    3. sourceSets {
    4. val jsMain by getting {
    5. dependencies {
    6. implementation(npm("base-64", "1.0.0"))
    7. }
    8. }
    9. }
    10. }
  2. In jsMain/kotlin, create a new org.jetbrains.base64 directory.

  3. Create the Base64.kt file in the new directory.

  4. Provide a simple implementation of the Base64Factory object that delegates to the Base64 object:

    1. package org.jetbrains.base64
    2. actual object Base64Factory {
    3. actual fun createEncoder(): Base64Encoder = JsBase64Encoder
    4. }
    5. object JsBase64Encoder : Base64Encoder {
    6. override fun encode(src: ByteArray): ByteArray {
    7. val binString = src.decodeToString()
    8. return Base64.encode(binString).encodeToByteArray()
    9. }
    10. }
  5. Create a separate file Base64Npm.kt in the same directory.

  6. Implement the Base64 object using the base-64 npm package:

    1. package org.jetbrains.base64
    2. @JsModule("base-64")
    3. @JsNonModule
    4. external object Base64 {
    5. fun encode(s: String): String
    6. }

Native

Unfortunately, there is no third-party implementation available for all Kotlin/Native targets, so you need to write it yourself:

  1. In nativeMain/kotlin, create a new org.jetbrains.base64 directory.
  2. Create the Base64.kt file in the new directory.
  3. Provide your own implementation for the Base64Factory object:

    1. package org.jetbrains.base64
    2. private val BASE64_ALPHABET: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    3. private val BASE64_MASK: Byte = 0x3f
    4. private val BASE64_PAD: Char = '='
    5. private val BASE64_INVERSE_ALPHABET = IntArray(256) {
    6. BASE64_ALPHABET.indexOf(it.toChar())
    7. }
    8. private fun Int.toBase64(): Char = BASE64_ALPHABET[this]
    9. actual object Base64Factory {
    10. actual fun createEncoder(): Base64Encoder = NativeBase64Encoder
    11. }
    12. object NativeBase64Encoder : Base64Encoder {
    13. override fun encode(src: ByteArray): ByteArray {
    14. fun ByteArray.getOrZero(index: Int): Int = if (index >= size) 0 else get(index).toInt()
    15. // 4n / 3 是预期的 Base64 有效载荷
    16. val result = ArrayList<Byte>(4 * src.size / 3)
    17. var index = 0
    18. while (index < src.size) {
    19. val symbolsLeft = src.size - index
    20. val padSize = if (symbolsLeft >= 3) 0 else (3 - symbolsLeft) * 8 / 6
    21. val chunk = (src.getOrZero(index) shl 16) or (src.getOrZero(index + 1) shl 8) or src.getOrZero(index + 2)
    22. index += 3
    23. for (i in 3 downTo padSize) {
    24. val char = (chunk shr (6 * i)) and BASE64_MASK.toInt()
    25. result.add(char.toBase64().code.toByte())
    26. }
    27. // 使用 '=' 填充
    28. repeat(padSize) { result.add(BASE64_PAD.code.toByte()) }
    29. }
    30. return result.toByteArray()
    31. }
    32. }

Test your library

Now when you have actual implementations of the Base64Factory object for all platforms, it’s time to test your multiplatform library.

To save time on testing, you can write common tests that will be executed on all platforms instead of testing each platform separately.

Prerequisites

Before writing tests, add the encodeToString method with the default implementation to the Base64Encoder interface, which is defined in commonMain/kotlin/org/jetbrains/base64/Base64.kt. This implementation converts byte arrays to strings, which are much easier to test.

  1. interface Base64Encoder {
  2. fun encode(src: ByteArray): ByteArray
  3. fun encodeToString(src: ByteArray): String {
  4. val encoded = encode(src)
  5. return buildString(encoded.size) {
  6. encoded.forEach { append(it.toInt().toChar()) }
  7. }
  8. }
  9. }

You can also provide a more efficient implementation of this method for a specific platform, for example, for JVM in jvmMain/kotlin/org/jetbrains/base64/Base64.kt:

  1. object JvmBase64Encoder : Base64Encoder {
  2. override fun encode(src: ByteArray): ByteArray = Base64.getEncoder().encode(src)
  3. override fun encodeToString(src: ByteArray): String = Base64.getEncoder().encodeToString(src)
  4. }

One of the benefits of a multiplatform library is having a default implementation with optional platform-specific overrides.

Write common tests

Now you have a string-based API that you can cover with basic tests.

  1. In commonTest/kotlin, create a new org.jetbrains.base64 directory.
  2. Create the Base64Test.kt file in the new directory.
  3. Add tests to this file:

    1. package org.jetbrains.base64
    2. import kotlin.test.Test
    3. import kotlin.test.assertEquals
    4. class Base64Test {
    5. @Test
    6. fun testEncodeToString() {
    7. checkEncodeToString("Kotlin is awesome", "S290bGluIGlzIGF3ZXNvbWU=")
    8. }
    9. @Test
    10. fun testPaddedStrings() {
    11. checkEncodeToString("", "")
    12. checkEncodeToString("1", "MQ==")
    13. checkEncodeToString("22", "MjI=")
    14. checkEncodeToString("333", "MzMz")
    15. checkEncodeToString("4444", "NDQ0NA==")
    16. }
    17. private fun checkEncodeToString(input: String, expectedOutput: String) {
    18. assertEquals(expectedOutput, Base64Factory.createEncoder().encodeToString(input.asciiToByteArray()))
    19. }
    20. private fun String.asciiToByteArray() = ByteArray(length) {
    21. get(it).code.toByte()
    22. }
    23. }
  4. In the Terminal, execute the check Gradle task:

    1. ./gradlew check

    You can also run the check Gradle task by double-clicking it in the list of Gradle tasks.

    创建并发布多平台库——教程 - 图5

The tests will run on all platforms (JVM, JS, and Native).

Add platform-specific tests

You can also add tests that will be run only for a specific platform. For example, you can add UTF-16 tests on JVM:

  1. In jvmTest/kotlin, create the org.jetbrains.base64 package.
  2. Create the Base64Test.kt file in the new package.
  3. Add tests to this file:

    1. package org.jetbrains.base64
    2. import kotlin.test.Test
    3. import kotlin.test.assertEquals
    4. class Base64JvmTest {
    5. @Test
    6. fun testNonAsciiString() {
    7. val utf8String = "Gödel"
    8. val actual = Base64Factory.createEncoder().encodeToString(utf8String.toByteArray())
    9. assertEquals("R8O2ZGVs", actual)
    10. }
    11. }

This test will automatically run on the JVM platform in addition to the common tests.

Publish your library to the local Maven repository

Your multiplatform library is ready for publishing so that you can use it in other projects.

To publish your library, use the maven-publish Gradle plugin.

  1. In the build.gradle.kts file, apply the maven-publish plugin and specify the group and version of your library:

    1. plugins {
    2. kotlin("multiplatform") version "1.9.10"
    3. id("maven-publish")
    4. }
    5. group = "org.jetbrains.base64"
    6. version = "1.0.0"
  2. In the Terminal, run the publishToMavenLocal Gradle task to publish your library to your local Maven repository:

    1. ./gradlew publishToMavenLocal

    You can also run the publishToMavenLocal Gradle task by double-clicking it in the list of Gradle tasks.

    创建并发布多平台库——教程 - 图6

Your library will be published to the local Maven repository.

Publish your library to the external Maven Central repository

You can go public and release your multiplatform library to Maven Central, a remote repository where maven artifacts are stored and managed. This way, other developers will be able to find it and add as a dependency to their projects.

Register a Sonatype account and generate GPG keys

If this is your first library, or you used the sunset Bintray to do this before, you need first to register a Sonatype account.

You can use the GetStream article to create and set up your account. The Registering a Sonatype account section describes how to:

  1. Register a Sonatype Jira account.
  2. Create a new issue. You can use our issue as an example.
  3. Verify your domain ownership corresponding to the group ID you want to use to publish your artifacts.

Then, since artifacts published on Maven Central have to be signed, follow the Generating a GPG key pair section to:

  1. Generate a GPG key pair for signing your artifacts.
  2. Publish your public key.
  3. Export your private key.

When the Maven repository and signing keys for your library are ready, you can move on and set up your build to upload the library artifacts to a staging repository and then release them.

Set up publication

Now you need to instruct Gradle how to publish the library. Most of the work is already done by the maven-publish and Kotlin Gradle plugins, all the required publications are created automatically. You already know the result when the library is published to a local Maven repository. To publish it to Maven Central, you need to take additional steps:

  1. Configure the public Maven repository URL and credentials.
  2. Provide a description and javadocs for all library components.
  3. Sign publications.

You can handle all these tasks with Gradle scripts. Let’s extract all the publication-related logic from the library module build.script, so you can easily reuse it for other modules in the future.

The most idiomatic and flexible way to do that is to use Gradle’s precompiled script plugins. All the build logic will be provided as a precompiled script plugin and could be applied by plugin ID to every module of our library.

To implement this, move the publication logic to a separate Gradle project:

  1. Add a new Gradle project inside your library root project. For that, create a new folder named convention-plugins and create a new file build.gradle.kts inside of it.
  2. Place the following code into the new build.gradle.kts file:

    1. plugins {
    2. `kotlin-dsl` // Is needed to turn our build logic written in Kotlin into the Gradle Plugin
    3. }
    4. repositories {
    5. gradlePluginPortal() // To use 'maven-publish' and 'signing' plugins in our own plugin
    6. }
  3. In the convention-plugins directory, create a src/main/kotlin/convention.publication.gradle.kts file to store all the publication logic.

  4. Add all the required logic in the new file. Be sure to make changes to match your project configuration and where explicitly noted by angle brackets (i.e. <replace-me>):

    1. import org.gradle.api.publish.maven.MavenPublication
    2. import org.gradle.api.tasks.bundling.Jar
    3. import org.gradle.kotlin.dsl.`maven-publish`
    4. import org.gradle.kotlin.dsl.signing
    5. import java.util.*
    6. plugins {
    7. `maven-publish`
    8. signing
    9. }
    10. // Stub secrets to let the project sync and build without the publication values set up
    11. ext["signing.keyId"] = null
    12. ext["signing.password"] = null
    13. ext["signing.secretKeyRingFile"] = null
    14. ext["ossrhUsername"] = null
    15. ext["ossrhPassword"] = null
    16. // Grabbing secrets from local.properties file or from environment variables, which could be used on CI
    17. val secretPropsFile = project.rootProject.file("local.properties")
    18. if (secretPropsFile.exists()) {
    19. secretPropsFile.reader().use {
    20. Properties().apply {
    21. load(it)
    22. }
    23. }.onEach { (name, value) ->
    24. ext[name.toString()] = value
    25. }
    26. } else {
    27. ext["signing.keyId"] = System.getenv("SIGNING_KEY_ID")
    28. ext["signing.password"] = System.getenv("SIGNING_PASSWORD")
    29. ext["signing.secretKeyRingFile"] = System.getenv("SIGNING_SECRET_KEY_RING_FILE")
    30. ext["ossrhUsername"] = System.getenv("OSSRH_USERNAME")
    31. ext["ossrhPassword"] = System.getenv("OSSRH_PASSWORD")
    32. }
    33. val javadocJar by tasks.registering(Jar::class) {
    34. archiveClassifier.set("javadoc")
    35. }
    36. fun getExtraString(name: String) = ext[name]?.toString()
    37. publishing {
    38. // Configure maven central repository
    39. repositories {
    40. maven {
    41. name = "sonatype"
    42. setUrl("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
    43. credentials {
    44. username = getExtraString("ossrhUsername")
    45. password = getExtraString("ossrhPassword")
    46. }
    47. }
    48. }
    49. // Configure all publications
    50. publications.withType<MavenPublication> {
    51. // Stub javadoc.jar artifact
    52. artifact(javadocJar.get())
    53. // Provide artifacts information requited by Maven Central
    54. pom {
    55. name.set("MPP Sample library")
    56. description.set("Sample Kotlin Multiplatform library (jvm + ios + js) test")
    57. url.set("https://github.com/<your-github-repo>/mpp-sample-lib")
    58. licenses {
    59. license {
    60. name.set("MIT")
    61. url.set("https://opensource.org/licenses/MIT")
    62. }
    63. }
    64. developers {
    65. developer {
    66. id.set("<your-github-profile>")
    67. name.set("<your-name>")
    68. email.set("<your-email>")
    69. }
    70. }
    71. scm {
    72. url.set("https://github.com/<your-github-repo>/mpp-sample-lib")
    73. }
    74. }
    75. }
    76. }
    77. // Signing artifacts. Signing.* extra properties values will be used
    78. signing {
    79. sign(publishing.publications)
    80. }

Applying just maven-publish is enough for publishing to the local Maven repository, but not to Maven Central. In the provided script, you get the credentials from local.properties or environment variables, do all the required configuration in the publishing section, and sign your publications with the signing plugin.

  1. Go back to your library project. To ask Gradle to prebuild your plugins, update the root settings.gradle.kts with the following:

    1. rootProject.name = "multiplatform-lib" // your project name
    2. includeBuild("convention-plugins")
  2. Now, you can apply this logic in the library’s build.script. In the plugins section, replace maven-publish with conventional.publication:

    1. plugins {
    2. kotlin("multiplatform") version "1.9.10"
    3. id("convention.publication")
    4. }
  3. Create a local.properties file within your library’s root directory with all the necessary credentials and make sure to add it to your .gitignore:

    1. # The GPG key pair ID (last 8 digits of its fingerprint)
    2. signing.keyId=...
    3. # The passphrase of the key pair
    4. signing.password=...
    5. # Private key you exported earlier
    6. signing.secretKeyRingFile=...
    7. # Your credentials for the Jira account
    8. ossrhUsername=...
    9. ossrhPassword=...
  4. Run ./gradlew clean and sync the project.

New Gradle tasks related to the Sonatype repository should appear in the publishing group – that means that everything is ready for you to publish your library.

Publish your library to Maven Central

To upload your library to the Sonatype repository, run the following program:

  1. ./gradlew publishAllPublicationsToSonatypeRepository

The staging repository will be created, and all the artifacts for all publications will be uploaded to that repository. All it’s left to do is to check that all the artifacts you wanted to upload have made it there and to press the release button.

These steps are described in the Your first release section. In short, you need to:

  1. Go to https://s01.oss.sonatype.org and log in using your credentials in Sonatype Jira.
  2. Find your repository in the Staging repositories section.
  3. Close it.
  4. Release the library.
  5. To activate the sync to Maven Central, go back to the Jira issue you created and leave a comment saying that you’ve released your first component. This step is only needed if it’s your first release.

Soon your library will be available at https://repo1.maven.org/maven2, and other developers will be able to add it as a dependency. In a couple of hours, other developers will be able to find it using Maven Central Repository Search.

Add a dependency on the published library

You can add your library to other multiplatform projects as a dependency.

In the build.gradle.kts file, add mavenLocal() or MavenCentral() (if the library was published to the external repository) and add a dependency on your library:

  1. repositories {
  2. mavenCentral()
  3. mavenLocal()
  4. }
  5. kotlin {
  6. sourceSets {
  7. val commonMain by getting {
  8. dependencies {
  9. implementation("org.jetbrains.base64:multiplatform-lib:1.0.0")
  10. }
  11. }
  12. }
  13. }

The implementation dependency consists of:

  • The group ID and version — specified earlier in the build.gradle.kts file
  • The artifact ID — by default, it’s your project’s name specified in the settings.gradle.kts file

For more details, see the Gradle documentation on the maven-publish plugin.

下一步做什么?