Generating Clients

The Smithy Kotlin build plugin, kotlin-codegen, generates Kotlin clients from Smithy models, and can be executed with Gradle (recommended) or the Smithy CLI.

Important

The Smithy CLI is a prerequisite for this guide. See the Smithy CLI installation guide if you do not already have the CLI installed.

Configuring code generation

In order to execute code generation, the kotlin-codegen plugin must be added to your smithy-build config:

smithy-build.json
{
  "version": "1.0",
  "plugins": {
+     "kotlin-codegen": {
+       "service": "com.example#CoffeeShop", // Replace with your service's ID
+       "sdkId": "CoffeeShop", // Replace with your service's SDK ID
+       "package": {
+         "name": "io.smithy.kotlin.client.example", // Generated Kotlin code will use this as the root package namespace
+         "version": "0.0.1"
+       }
+     }
  }
}

Add Smithy build to the Kotlin build

To ensure your client code is generated on every Kotlin build, Gradle must be configured to run a Smithy build before Kotlin compilation in your Gradle build script:

build.gradle.kts
tasks.named("compileKotlin") {
    dependsOn("smithyBuild")
}

Add generated code to the Kotlin sourceSet

Your package is now configured to generate Kotlin client source code. However, the generated code must be added to a source set to be compiled. To add the generated code to the main source set, add the following to your Gradle build script:

build.gradle.kts
afterEvaluate {
    val clientPath = smithy.getPluginProjectionPath(smithy.sourceProjection.get(), "kotlin-codegen")
    sourceSets.main.get().kotlin.srcDir(clientPath)
}

Opt in to internal APIs

Some of the code generated client APIs are public but marked with an InternalApi annotation to discourage client end users from using them outside of generated code. To opt in to the InternalApi annotation, add the following to your Gradle build script:

build.gradle.kts
val optinAnnotations = listOf("kotlin.RequiresOptIn", "aws.smithy.kotlin.runtime.InternalApi")
kotlin.sourceSets.all {
    optinAnnotations.forEach { languageSettings.optIn(it) }
}

Generating code

To generate and compile your client code, run a build from the root of your Gradle project:

./gradlew clean build

Building the project will generate code in the build/smithy-projections/<project-name>/source/kotlin-codegen/ directory.