Written by 7:23 am Cloud

Publishing Kotlin Multiplatform Swift Packages Using Google Cloud Storage and Cloud Run

Kotlin Multiplatform (KMM) is a framework that allows developers to write code that can be shared across multiple platforms, including iOS, Android, and the web. This can be a great way to save time and effort, as developers only need to write their code once and can then deploy it to multiple platforms with minimal changes.

One of the challenges of publishing KMM Swift packages is that Xcode does not support directly installing Swift packages from Google Cloud Storage. To workaround this, we can use a combination of Google Cloud Storage and Cloud Run to publish our KMM Swift packages.

Overview of the Solution

Our solution for publishing KMM Swift packages using Google Cloud Storage and Cloud Run consists of the following components:

  • A custom Gradle plugin that publishes XCFrameworks to Google Cloud Storage.
  • A Google Cloud Run service that downloads Swift Package binaries requested by Xcode.

The custom Gradle plugin will build and package our KMM Swift library into an XCFramework. It will then upload the XCFramework to a Google Cloud Storage bucket.

The Google Cloud Run service will expose an endpoint that Xcode can use to download the Swift Package binary. When Xcode requests the Swift Package binary, the Cloud Run service will download the XCFramework from Google Cloud Storage and return it to Xcode.

Setting Up Google Cloud Storage and Cloud Run

To set up Google Cloud Storage and Cloud Run, we will need to create a Google Cloud Platform (GCP) project. Once we have created a GCP project, we can create a Google Cloud Storage bucket and a Cloud Run service.

To create a Google Cloud Storage bucket, we can use the following steps:

  1. Go to the Google Cloud Platform Console: https://console.cloud.google.com/.
  2. Click the hamburger menu (three horizontal lines) in the top left corner of the page.
  3. Click Storage.
  4. Click the Create bucket button.
  5. Enter a name for your bucket and select a region.
  6. Click the Create button.

To create a Cloud Run service, we can use the following steps:

  1. Go to the Google Cloud Platform Console: https://console.cloud.google.com/.
  2. Click the hamburger menu (three horizontal lines) in the top left corner of the page.
  3. Click Cloud Run.
  4. Click the Create service button.
  5. Enter a name for your service and select a region.
  6. Click the Next button.
  7. Under Runtime, select Custom container.
  8. Under Image, enter the following image name:
gcr.io/<YOUR_GCP_PROJECT_ID>/swift-package-downloader
  1. Under Service URL, enter a URL for your service. This is the URL that Xcode will use to download the Swift Package binary.
  2. Click the Create button.

Building and Publishing the KMM Swift Package

To build and publish the KMM Swift package, we will need to use the custom Gradle plugin. To install the custom Gradle plugin, we can use the following steps:

  1. Open the build.gradle file for your KMM project.
  2. Add the following dependency to the plugin section:
dependencies {
    classpath 'com.premise.swift:swift-package-publisher:1.0.0'
}
  1. Sync your Gradle project.

Once we have installed the custom Gradle plugin, we can build and publish the KMM Swift package using the following steps:

  1. Open a terminal window and navigate to the directory containing your KMM project.
  2. Run the following command:
gradle publishSwiftPackage

This will build the KMM Swift library into an XCFramework and upload the XCFramework to the Google Cloud Storage bucket that we created earlier.

Consuming the KMM Swift Package in Xcode

To consume the KMM Swift package in Xcode, we will need to add the Cloud Run service as a source. To do this, we can use the following steps:

  1. Open your Xcode project.
  2. Go to File > Add Packages
  3. Click the + button in the top left corner of the dialog box.
  4. Enter the URL of your Cloud Run service in the Enter package repository URL field.
  5. Click the Add button.

Xcode will download the Swift Package binary from your Cloud Run service and add it to your project. You can then use the KMM Swift library in your Xcode project like any other Swift library.

Conclusion

This article has shown how to use Google Cloud Storage and Cloud Run to publish.

Visited 170,740 times, 1 visit(s) today
Close