# Android / Amazon

The HyprMX SDK is designed to present rewarded, interstitial, and banner/MREC ads in your application. To integrate the SDK, follow the steps below.

## SDK Integration <a href="#quickstart-integratingthesdk" id="quickstart-integratingthesdk"></a>

You can integrate the HyprMX SDK through [Gradle](https://gradle.org/) dependencies. Follow the steps below to set up the SDK.&#x20;

{% hint style="info" %}
This guide assumes you already have your Gradle-based project up and running in Android Studio.
{% endhint %}

{% hint style="info" %}
If your app is integrated with HyprMX SDK 6.x and you need help with migrating to 6.4+, please follow the steps in the [migration guide](https://documentation.hyprmx.com/sdk-integration-guides/android-amazon/migrate-to-version-6.4+).
{% endhint %}

{% stepper %}
{% step %}
Open your existing application in Android Studio.
{% endstep %}

{% step %}
Add the HyprMX SDK to your app's `build.gradle` file dependencies block:

```gradle
dependencies {
    implementation 'com.hyprmx.android:HyprMX-SDK:6.4.6'
}
```

{% endstep %}

{% step %}
[Adding Google Play Services Ads Identifier](https://developers.google.com/android/guides/setup#list-dependencies) is optional. If the dependency below is included, it is the app developer's responsibility to ensure compliance with all Google Play policies, including, but not limited to, the [Families Program](https://play.google.com/console/about/families/).

```gradle
dependencies {
    implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1'
}
```

{% endstep %}

{% step %}
If you are using Kotlin in your application, add the following to your `build.gradle` inside the `android` block:

```kts
kotlinOptions {
  jvmTarget = "1.8"
  freeCompilerArgs = [
      "-Xjvm-default=compatibility",
  ]
}
```

{% endstep %}
{% endstepper %}

## Initializing HyprMX <a href="#quickstart-usage" id="quickstart-usage"></a>

After you have integrated the SDK, proceed to initialize the HyprMX SDK. To initialize, follow the steps below.

{% stepper %}
{% step %}
Add the following imports to your activity, or auto-import them as you move forward with your integration:

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

```java
import com.hyprmx.android.sdk.core.HyprMX;
import com.hyprmx.android.sdk.core.HyprMXErrors;
import com.hyprmx.android.sdk.core.HyprMXIf;
import com.hyprmx.android.sdk.core.InitResult;
import com.hyprmx.android.sdk.placement.HyprMXPlacementExpiryListener;
import com.hyprmx.android.sdk.placement.HyprMXRewardedShowListener;
import com.hyprmx.android.sdk.placement.HyprMXShowListener;
import com.hyprmx.android.sdk.placement.Placement;
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
import com.hyprmx.android.sdk.core.HyprMX
import com.hyprmx.android.sdk.core.HyprMXErrors
import com.hyprmx.android.sdk.placement.HyprMXPlacementExpiryListener
import com.hyprmx.android.sdk.placement.HyprMXRewardedShowListener
import com.hyprmx.android.sdk.placement.HyprMXShowListener
import com.hyprmx.android.sdk.placement.Placement
```

{% endtab %}
{% endtabs %}
{% endstep %}

{% step %}
Initialize the HyprMX SDK in your main `Activity` inside the `onCreate` method as shown below.&#x20;

As a best practice, initialize HyprMX as soon as possible (i.e. when your application is loading) so we can begin preloading ads.

Review the details of initialization parameters, [context](#context-required) and [distributorID](#distributorid-required).

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

```java
private HyprMXIf.HyprMXInitializationListener initializationListener = this;
HyprMX.INSTANCE.initialize(this, DISTRIBUTOR_ID, initializationListener);

// or
HyprMX.INSTANCE.initialize(this, DISTRIBUTOR_ID, initResult -> {
   // do something
});
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val initializationListener: HyprMXIf.HyprMXInitializationListener = this
HyprMX.initialize(this, DISTRIBUTOR_ID, initializationListener)

// or
HyprMX.initialize(this@MainActivity, DISTRIBUTOR_ID) { (isSuccess, message) ->
  // do something
}

// or
HyprMX.initialize(this@MainActivity, DISTRIBUTOR_ID) { result ->
  // do something
}
```

{% endtab %}
{% endtabs %}
{% endstep %}
{% endstepper %}

#### **context (required)**

Your current `context` instance.

#### **distributorID (required)**

The value for `distributorID` is assigned to your app by HyprMX. If you have not received this ID and your placements information, please reach out to your HyprMX account manager.

If you need to call HyprMx initializer inside a [suspending function](https://kotlinlang.org/docs/composing-suspending-functions.html) you can do as below:

```kotlin
val (initSuccess, _) = HyprMX.initialize(this@MainActivity, DISTRIBUTOR_ID)
```

### Initialization Callbacks

`initializationListener` is the listener for the Initialization Status of the SDK. It will callback to `onInitialized` as below:

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

```java
private HyprMXIf.HyprMXInitializationListener initializationListener = new HyprMXIf.HyprMXInitializationListener() {
  @Override
  public void onInitialized(@NonNull InitResult initResult) {
    if(initResult.isSuccess()) {
      // do something
    }

    if(initResult.getMessage() != null) {
       // an error was thrown, i.e., isSuccess() == false
    }
  }
};
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val initializationListener: HyprMXIf.HyprMXInitializationListener = object : HyprMXIf.HyprMXInitializationListener {
    override fun onInitialized(result: InitResult) {
      if(result.success) {
        // do something
      }
  
      if(!result.message.isNullOrEmpty()) {
         // an error was thrown, i.e., success == false
      }
    }
}
```

{% endtab %}
{% endtabs %}

After receiving the `onInitialized` callback, you are now ready to load and display ads in your application. See our [Rewarded Ads](https://documentation.hyprmx.com/sdk-integration-guides/android-amazon/ad-formats/rewarded-ads), [Interstitial Ads](https://documentation.hyprmx.com/sdk-integration-guides/android-amazon/ad-formats/interstitial-ads), and [Banner/MREC Ads](https://documentation.hyprmx.com/sdk-integration-guides/android-amazon/ad-formats/banner-mrec-ads) guides to add these ad types to your app.

{% hint style="info" %}
You can convert these callbacks to lambda (Java) or use a [high-order function](https://kotlinlang.org/docs/lambdas.html#higher-order-functions) on Kotlin, as shown above in the [initialization](#sample-initializer) section.
{% endhint %}

## \[Optional] Passing Alternative Identifiers

{% hint style="danger" %}
Alternative/Extended IDs are supported in HyprMX SDK 6.4+.
{% endhint %}

HyprMX SDK supports the following alternative IDs or extended IDs (EID) to help improve monetization. If applicable, you are responsible for tokenizing IDs before passing them to HyprMX SDK.

* [Unified ID 2.0 (UID2)](https://unifiedid.com/)
* [ID5](https://id5.io/)
* [LiveIntent ID](https://www.liveintent.com/identity-solutions/)

The following code snippet shows how to pass the IDs to HyprMX. Any optional fields supplied should be of the correct type.

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

```java
public class EIDJson {
  public static final String EXTRA_USER_EIDS_KEY = "eids";

  public static final String UID2 =
    """
    {
        "uid2": [{
            "id": "testUID2Id"
        }]
    }
    """;

  public static final String ID5 =
    """
    {
        "id5": [{
            "id": "testID5id",
            "linkType": 1,
            "abTestingControlGroup": true
        }]
    }
    """;

  public static final String LIVE_INTENT =
    """
    {
        "liveintent": [{
            "id": "testLiveintentId",
            "atype": 1
        }]
    }
    """;
}

private void setExtentedID(@NonNull String value) {
  String eidData = null;
  try {
    // Make sure that UID JSON content is a valid one
    eidData = new JSONObject(value).toString();
  } catch (Exception ex)  {
    Log.e(TAG, "Error validating EID JSON structure.");
  }

  HyprMX.INSTANCE.setUserExtras(EIDJson.EXTRA_USER_EIDS_KEY, eidData);
}

// Setting extends ID for UID2
setExtendedID(EIDJson.UID2);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
object EIDJson {
    val EXTRA_USER_EIDS_KEY = "eids"

    val UID2 =
    """
    {
        "uid2": [{
            "id": "testUID2Id"
        }]
    }
    """

    val ID5 =
    """
    {
        "id5": [{
            "id": "testID5id",
            "linkType": 1,
            "abTestingControlGroup": true
        }]
    }
    """

    val LIVE_INTENT =
    """
    {
        "liveintent": [{
            "id": "testLiveintentId",
            "atype": 1
        }]
    }
    """
}

private fun setExtendedID(value: String) {
  // Make sure that UID JSON content is a valid one
  val eidData = try {
    JSONObject(value).toString()
  } catch (ex: Exception)  {
    Log.e(TAG, "Error validating EID JSON structure.")
  null
  }

  HyprMX.setUserExtras(EIDJson.EXTRA_USER_EIDS_KEY, eidData)
}

// Setting extends ID for UID2
setExtendedID(EIDJson.UID2)
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
For best results, set alternative/extended IDs before initializing HyprMX.
{% endhint %}

{% hint style="info" %}
Passing an empty string instead of json clears all alternative/extended IDs.
{% endhint %}

## Privacy Compliance

Please refer to our [Privacy](https://www.hyprmx.com/privacy-policy.html) page to learn more about your privacy compliance responsibilities and to implement the relevant privacy methods.

## License <a href="#quickstart-license" id="quickstart-license"></a>

By integrating the HyprMX SDK, you are agreeing to the [End User License Agreement](https://hyprmx.com/eula.html).
