# Android/Fire TV Integration

## Architecture

This SDK is built for Android-based platforms including Android TV and Fire TV. We should not include any Google/Amazon/TV specific features in the SDK.

We use Kotlin and Coroutine to build up this SDK and demo apps. Java developers can also use this SDK easily

The SDK uses the following dependencies:

* [Glide](https://github.com/bumptech/glide) for image loading
* [Gson](https://github.com/google/gson) for json serialization/deserialization
* [Okhttp](https://github.com/square/okhttp/) for network connection
* [Retrofit](https://github.com/square/retrofit) for http rest api connection
* [ExoPlayer](https://github.com/google/ExoPlayer) for video playback

## Modules

* video\_preview\_sdk: the sdk library
* demo\_app: show how to integrate the SDK for Android TV apps
* sample\_app: show how to integrate the SDK for generic android apps

## Branches

* `master`: The developing branch with the latest changes

## Build and deploy

### Fastlane

We will use [fastlane](https://fastlane.tools/) to help the build and deploy process. You can setup fastlane by [fastlane\_README](https://app.gitbook.com/s/-Mf6ZckSosxtgmbTz71L/preview-sdk/tv/fastlane/README.md) or [Online Doc](https://docs.fastlane.tools/) The Fastlane config files: [Fastfile](https://app.gitbook.com/s/-Mf6ZckSosxtgmbTz71L/preview-sdk/tv/fastlane/Fastfile) and [Appfile](https://app.gitbook.com/s/-Mf6ZckSosxtgmbTz71L/preview-sdk/tv/fastlane/Appfile)

You can also use gradle build as normal Android build process.

List some fastlane commands we defined in the following:

```bash
# Clean project
bundle exec fastlane clean

# Run lint static source code check
bundle exec fastlane lint_check

# Run all unit tests
bundle exec fastlane unit_tests

# Run lint and all unit tests
bundle exec fastlane lint_and_unit_tests

# Build debug sdk and apk
bundle exec fastlane build_debug

# Build release sdk and apk
bundle exec fastlane build_release
```

### Continuous Integration (CI)

We are using [Circle CI](https://circleci.com/bb/lucidvr) as our SAAS CI server.

The circleci needs a config file [config.yml](https://app.gitbook.com/s/-Mf6ZckSosxtgmbTz71L/preview-sdk/tv/.circleci/config.yml) and all the automation commands are inside the config file.

## Integrating with Android or Fire TV

Use these instructions to integrate the SDK with your Android/Android TV/Fire TV app. If you have any questions, please contact the Lucid account team.

### Step 1: Download the Lucid Android SDK <a href="#download-lucid-android-sdk" id="download-lucid-android-sdk"></a>

The Lucid Preview Video Android SDK is available as an AAR. Download the latest aar file from [here](https://app.gitbook.com/s/-Mf6ZckSosxtgmbTz71L/preview-sdk/tv/android-sdk/TODO)

### Step 2: Add SDK to your project <a href="#add-sdk-to-your-project" id="add-sdk-to-your-project"></a>

To add the sdk dependency, put the aar file into your libs folder and make sure the app module's *build.gradle* to have the following lines:

```
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
}
```

The Lucid Preview Video Android SDK uses ExoPlayer for displaying the previews, so the first step will be to add ExoPlayer to your project. Add these lines to the app module's *build.gradle*:

```
dependencies {
    // ExoPlayer's leanback extension
    def exoplayer_version = '2.14.0'
    implementation "com.google.android.exoplayer:exoplayer-core:$exoplayer_version"
    implementation "com.google.android.exoplayer:exoplayer-ui:$exoplayer_version"
    implementation "com.google.android.exoplayer:extension-mediasession:$exoplayer_version"
    implementation "com.google.android.exoplayer:extension-leanback:$exoplayer_version"
    implementation "com.google.android.exoplayer:exoplayer-dash:$exoplayer_version"
    implementation "com.google.android.exoplayer:exoplayer-hls:$exoplayer_version"
}
```

To support Java 8, add the language feature support:

```
android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
```

### Step 3. Update Your Android Manifest <a href="#update-android-manifest" id="update-android-manifest"></a>

Update your AndroidManifest.xml in order to complete the SDK integration.

```
<!-- Required permissions -->
<uses-permission android:name="android.permission.INTERNET" />
```

### Step 4: Initialize the SDK in your app <a href="#init-sdk-in-your-app" id="init-sdk-in-your-app"></a>

After you have integrated the Lucid Preview Video SDK and created an access token, you must call **LucidPreviewVideoSdk.init** before showing any videos. Initialization is required for only once per app's lifecycle, typically on app launch.

You need to pass an access token string to the **LucidPreviewVideoSdk.init** API for initialization.

```
LucidPreviewVideoSdk.init("token")
```

You can also adjust the log level for debugging, the default log level is *LogLevel.ERROR*, which only print the error messages. You can set your own configuration when using **LucidPreviewVideoSdk.init** API.

```
// Do not print any logs
LucidPreviewVideoSdk.init("token", LogLevel.NONE)
// Print all logs
LucidPreviewVideoSdk.init("token", LogLevel.VERBOSE)
```

You can also adjust the cache size for the preview videos, the default size is *50* entries, you can set your own configuration when using **LucidPreviewVideoSdk.init** API.

```
LucidPreviewVideoSdk.init("token", LogLevel.Error, 100)
```

### Step 5: Prepare Preview Videos <a href="#prepare-preview-videos" id="prepare-preview-videos"></a>

Once the sdk is initialized, you can use **LucidRequest** to prepare the preview video information with the **prepareVideo()** API.

```
val request = LucidRequest(videoId, videoIndex)
LucidPreviewVideoSdk.prepareVideo(request, object: LucidPreviewVideoSdk.LucidPreviewListener{
    override fun onError(e: Exception) {
    }

    override fun onSuccess(video: LucidVideo) {
    }
})
```

### Step 6: Use PreviewLayout <a href="#use-preview-layout" id="use-preview-layout"></a>

The PreviewLayout class is an UI component to display the preview video. Use **setLucidVideo()** API to prepare the preview content and use **setPreviewPlayback()** to start/stop playing preview.

```
private lateinit var titlePreview: PreviewLayout

titlePreview = view.findViewById(R.id.title_preview)
titlePreview.setLucidVideo(video)
titlePreview.setPreviewPlayback(true)
```

There are multiple attributes for PreviewLayout. You can configurate them either using xml or Java/Kotlin APIs.

```
loop: default true
mute: default false
gradient_border: default true
```

`{"mode":"full","isActive":false}`


---

# 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://lucid-ai.gitbook.io/devdocs/products/video-personalization/playback-integration/android-fire-tv-integration.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.
