# Java/Kotlin

{% hint style="info" %}
Java/Kotlin SDK supports JDK 8 and above.
{% endhint %}

## Add Dependency

[![](https://img.shields.io/maven-central/v/io.hackle/hackle-server-sdk)](https://central.sonatype.com/artifact/io.hackle/hackle-server-sdk)

Add the SDK dependency.

```groovy
repositories {
  mavenCentral()
}

dependencies {
  implementation 'io.hackle:hackle-server-sdk:2.32.0'
}
```

## SDK Initialization

{% hint style="danger" %}
HackleClient must be a singleton.

`HackleClient` manages state internally to assign Variations without blocking the calling thread.\
This requires additional resources.

It should be managed as a single instance throughout the application lifecycle rather than being instantiated for each request.
{% endhint %}

`HackleClient` is a class that provides methods for using the SDK features.

`HackleClient` initialization is required to use the SDK.

#### Instantiation

Pass the SDK Key to instantiate `HackleClient`.\
`HackleClient` periodically synchronizes with the Hackle server as a background task to obtain the required information.

* You can find the SDK Key in [SDK Integration Info](https://dashboard.hackle.io/config/sdk-setting) on the Hackle Service Dashboard.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
import io.hackle.sdk.HackleClient
import io.hackle.sdk.HackleClients

// Replace YOUR_SERVER_SDK_KEY with the SDK Key.
val hackleClient = Hackle.client(YOUR_SERVER_SDK_KEY)
```

{% endtab %}

{% tab title="Java" %}

```java
import io.hackle.sdk.HackleClient
import io.hackle.sdk.HackleClients

// Replace YOUR_SERVER_SDK_KEY with the SDK Key.
HackleClient hackleClient = HackleClients.create(YOUR_SERVER_SDK_KEY);
```

{% endtab %}
{% endtabs %}

## Shutdown

You must call the `close()` method when the application shuts down. This releases resources in use and sends any remaining events.

{% hint style="danger" %}
If the application exits without calling `close()`, **events may be lost.**
{% endhint %}

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
import io.hackle.sdk.HackleClient

hackleClient.close()
```

{% endtab %}

{% tab title="Java" %}

```java
import io.hackle.sdk.HackleClient

hackleClient.close();
```

{% endtab %}
{% endtabs %}

#### If you are using Spring Framework

The best approach is to register `HackleClient` as a bean.\
It will be managed as a single instance throughout the application lifecycle, and `close()` will be called automatically when the application shuts down.

#### If you are wrapping HackleClient

It is recommended to implement `AutoClosable` in your wrapping class and call `HackleClient`'s `close()` when the wrapper's `close()` is called.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
import io.hackle.sdk.HackleClient

class CustomHackleClient(private val hackleClient: HackleClient): AutoCloseable {

    override fun close() {
        hackleClient.close()
    }
}
```

{% endtab %}

{% tab title="Java" %}

```java
import io.hackle.sdk.HackleClient

public class CustomHackleClient implements AutoCloseable {

    private final HackleClient hackleClient;

    public CustomHackleClient(HackleClient hackleClient) {
        this.hackleClient = hackleClient;
    }

    @Override
    public void close() throws Exception {
        hackleClient.close();
    }
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hackle.io/en/development-guide/java-kotlin.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
