> ## Documentation Index
> Fetch the complete documentation index at: https://developer.onetrust.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Adding SDK to App

# Overview

> Supports Android Mobile, Android TV, and Fire TV applications.

The Android SDK can be implemented 2 different ways:

1. Add SDK using Maven
2. Import `.aar` package

### Dependencies

The OneTrust SDK uses the following dependencies.  Make sure to include them or exclude them based on your current project.  Not having these will cause runtime exceptions.

```java
// mandatory
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.work:work-runtime:2.9.0'
implementation 'androidx.browser:browser:1.7.0'
implementation 'com.github.bumptech.glide:glide:4.10.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0'
implementation 'androidx.fragment:fragment-ktx:1.6.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.7.10"
implementation 'com.squareup.retrofit2:converter-scalars:2.8.1'

// optional: Only when the SDK needs to measure Google Ad Id Opt In/Outs
implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1'
// optional for QR code display on TV UI
implementation 'com.google.zxing:core:3.3.0'
  
// for cmp api implementations 
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"   
```

Add the following code snippet to the 'android()' section of the app's gradle file

```java
buildFeatures {
  viewBinding true
  }
```

### Known Issues with Gradle Version 8+

There is a known issue with gradle version 8+ where the application is getting a build error with R8 with the OneTrust SDK.

**Solution 1:** Add the optional dependencies below in the app's build.gradle file

```java
 implementation ("com.google.android.gms:play-services-ads-identifier:18.0.1")
 implementation ("com.google.zxing:core:3.3.0")
```

**Solution 2:** In app's proguard-rules.pro file, add the rules below:

```java
	# Ignoring implementation ("com.google.zxing:core:3.3.0") dependencies.
	    -dontwarn com.google.zxing.BarcodeFormat
	    -dontwarn com.google.zxing.EncodeHintType
	    -dontwarn com.google.zxing.MultiFormatWriter
	    -dontwarn com.google.zxing.common.BitMatrix

	# Ignoring implementation ("com.google.android.gms:play-services-ads-identifier:18.0.1") dependencies
	   -dontwarn com.google.android.gms.ads.identifier.AdvertisingIdClient$Info
	   -dontwarn com.google.android.gms.ads.identifier.AdvertisingIdClient
```

### Known Runtime Issue with Gradle Version 8+ if `isMinifyEnabled = true`

With Gradle version 8+, if the app sets `isMinifyEnabled = true`, the app may crash on runtime when `startSDK()` is called.

The OneTrust SDK uses `Retrofit` for network calls, which requires some proguard rule updates if the app is using R8 shrinking and obfuscation rules. More info in the links below:

<https://github.com/square/retrofit?tab=readme-ov-file#r8--proguard>\
<https://github.com/square/retrofit/blob/trunk/retrofit/src/main/resources/META-INF/proguard/retrofit2.pro>

To resolve the crash, add the following to the `proguard-rules.pro`

**Option 1:**\
R8 full mode strips generic signatures from return types if not kept.

```
 -if interface * { @retrofit2.http.* public *** *(...); }
	 -keep,allowoptimization,allowshrinking,allowobfuscation class <3>
```

**Option 2:**\
Add the rule below similar to <https://developer.onetrust.com/onetrust/docs/android#frequently-asked-questions-faq>

```
-keep class retrofit2.** { *; }
```

***

## Adding SDK using Maven

OneTrust SDK is published and distributed through [Maven repository](https://mvnrepository.com/artifact/com.onetrust.cmp/native-sdk/6.29.0.0).  Follow the below given steps to add SDK to your application as a build dependency.

1. Open Gradle Scripts for your Project: `build.gradle (Project: <your_project>)`
2. Add the following line to the buildscript:

```java
repositories {
	mavenCentral()
} 
```

3. Add the following to the `dependencies` section of your `build.gradle (module: app)` file

```java
// Implement the SDK version that corresponds to the published version
implementation 'com.onetrust.cmp:native-sdk:X.X.0.0'

// Example: 
implementation 'com.onetrust.cmp:native-sdk:202209.1.0.0'
```

4. Save and Close gradle files.
5. Build your project. Once it is built successfully, you can import SDK.

### Dependency Conflicts

When using Maven, the OneTrust SDK includes several dependencies that you may already include in your project.  On compile, if you experience a dependency conflict, try excluding the OneTrust SDK's runtime dependency and re-build:

```java
// Make sure the com.onetrust.cmp:native-sdk:[version] matches what you're implementing.

implementation ('com.onetrust.cmp:native-sdk:202209.1.0.0') {
	exclude group: 'com.google.android.gms'
}
```

***

<br />

## Import `.aar` Framework

1. Download zip from OneTrust console which contains .aar file.
2. Place the OTPublishersHeadlessSDK-release.arr inside **libs** folder

[block:image]
{
  "images": [
    {
      "image": [
        "https://files.readme.io/8d48c74-Screenshot_2022-11-18_at_12.30.38.png",
        null,
        ""
      ],
      "align": "center",
      "sizing": "250px"
    }
  ]
}
[/block]

3. Navigate to **File > Project Structure > Dependencies**
4. In the **Declared Dependencies** tab, click **+** and select **Jar/AAR Dependency** in the dropdown

[block:image]
{
  "images": [
    {
      "image": [
        "https://files.readme.io/6ea425d-Screenshot_2022-11-18_at_12.00.11.png",
        null,
        ""
      ],
      "align": "center",
      "sizing": "600px"
    }
  ]
}
[/block]

5. In the **Add Jar/AAR Dependency** dialog, first enter the path to the OneTrust .aar file, then select the configuration **implementation**

[block:image]
{
  "images": [
    {
      "image": [
        "https://files.readme.io/6e3e2ba-Screenshot_2022-11-18_at_12.01.08.png",
        null,
        ""
      ],
      "align": "center",
      "sizing": "600px"
    }
  ]
}
[/block]

6. Check your app's build.gradle file to confirm the declaration.

```java
implementation files('libs/OTPublishersHeadlessSDK-release.arr')
```

7. Include the dependencies [(listed above)](https://developer.onetrust.com/onetrust/docs/adding-sdk-to-app-android#dependencies) if your app has not implemented these already. Not having the dependencies will cause exceptions at runtime. We recommend you implement the latest versions.

### Optional Dependencies

#### Google Ad Id

The Android SDK has the capability to measure the opt-in/out rates of Google's Advertising Id.  [According to Google](https://support.google.com/googleplay/android-developer/answer/6048248?hl=en), starting in late 2021 when a user opts out of **interest-based advertising** or **ads personalization** the advertising identifier will not be available and will be a string of 0's.

If you'd like to measure the opt in/out rate, it's mandatory to include the following dependency:

```java
implementation 'com.google.android.gms:play-services-ads:7.8.0'
```

#### QR Code for Android TV

In order to show a privacy policy as a QR code on Android TV, App will have to add the following dependency manually in app's gradle file if QR code has to be supported to avoid any runtime exceptions:

```java
implementation 'com.google.zxing:core:3.3.0' 
```

<br />

### D8 Exceptions

On compile, if you experience a any D8 exceptions with the above mentioned gradle versions, add the following code to app’s gradle file:

```java
compileOptions {    
	sourceCompatibility = '1.8'    
	targetCompatibility = '1.8'
}
```

## Import Header

Import header to begin using OneTrust SDK methods.

```java
import com.onetrust.otpublishers.headless.Public.OTCallback;
import com.onetrust.otpublishers.headless.Public.OTPublishersHeadlessSDK;
```