Kotlin SDK (Beta)

Important
The Kotlin SDK is currently in BETA. There may be minor changes to parameter names or other implementation details before the general access release. We invite you to try it out. We would appreciate any feedback via our support team.

Overview

Use Cloudinary's Kotlin SDK to seamlessly add advanced media optimization, manipulation and delivery to your existing Kotlin or Java projects. The SDK can be used for Android applications as well as any backend Kotlin projects. Take a look at how to integrate cloudinary to learn more about getting started with Cloudinary or read on to quickly get set up with the SDK.

Key features

  • Uses Cloudinary's new SDK 2 principles with enhanced code autocomplete and action based syntax.
  • Actions and transformations are immutable, for easier and safer code reuse.
  • Makes use of Type-Safe Builders to create a Cloudinary DSL layer. The transformation syntax is therefore simpler and more human-readable when compared with the existing Java or Android SDKs.

Getting started

Quickly install and configure the SDK in your project to get started.

Add Kotlin SDK dependency

Add the SDK to your project as a dependency, we recommend using a build management tool such as Maven or Gradle to do this.

Using Gradle

Add the Cloudinary Kotlin SDK to the dependencies section of your build.gradle file.

Copy to clipboard
implementation 'com.cloudinary:kotlin-url-gen:0.0.1-beta.1''

Using Maven

Add the Cloudinary Kotlin SDK to the list of dependencies in your pom.xml file.

Copy to clipboard
<dependencies>
  ...
  <dependency>
    <groupId>com.cloudinary</groupId>
    <artifactId>kotlin-url-gen</artifactId>
    <version>0.0.1-beta.1</version>
    <type>pom</type>
  </dependency>
</dependencies>

Add your Cloudinary configuration

The Cloudinary class is the main entry point for using the library. Your cloud_name is required to create an instance of this class. Your api_key and api_secret are used to perform secure API calls to Cloudinary (e.g., image and video uploads). Setting the configuration parameters can be done either programmatically using an appropriate constructor of the Cloudinary class or globally using an environment variable. You can find your account-specific configuration parameters on the Dashboard page of your account console.

In addition to the required configuration parameters, you can define a number of optional configuration parameters if relevant.

Here's an example of setting configuration parameters in your Kotlin application:

Copy to clipboard
import com.cloudinary.*; 
...
private val cloudinary = Cloudinary("cloudinary://<your-api-key>:<your-api-secret>@<your-cloud-name>")

Example

Here is a simple example for generating a Cloudinary image URL using the Kotlin SDK:

Copy to clipboard
cloudinary.media {
    publicId("sample")
    resize(fill { 
        width(200)
        height(300)
    })
}

simple example

Migration from Java/Android

To use the Kotlin SDK with your existing Java and Android projects, we recommend adding your own Kotlin classes as a bridge between our SDK and your Java code. You can therefore take advantage of the simpler transformation building syntax.

For example, here's a simple Kotlin transformation:

Copy to clipboard
val t = transformation {
          gradientFade { strength(3) }
          adjust(opacity(80))
          border {
            width(4)
            color (Color.RED)
        }
    }

And the corresponding code in Java:

Copy to clipboard
Transformation t = new Transformation()
       .gradientFade(new GradientFade.Builder().strength(3).build())
       .adjust(new Opacity.Builder(80).build())
       .border(new Border.Builder().width(4).color(new ColorValue.Builder().named("red").build()).build());

You can add a new Kotlin file to act as a bridge e.g.:

Copy to clipboard
fun constructSpecificUseCaseTransformation() =
   transformation {
       gradientFade { strength(3) }
       adjust(opacity(80))
       border {
           width(4)
           color (Color.RED)
       }
   }

You can then call this specific function from your Java code:

Copy to clipboard
Transformation t = constructSpecificUseCaseTransformation()

✔️ Feedback sent!