# Android

The Thryve Android SDK is the fastest way to implement Thryve's product into your Android application and is required to allow users to connect Health Connect and Samsung Health as a data source. The Thryve iOS SDK is available via the Maven repository and frameworks.

## Integrate using Thryve Repository Dependencies

Thryve provides a private, protected repository supporting the Maven repository format to retrieve the Thryve SDK build artifacts. To configure a Gradle project to resolve dependencies declared in `build.gradle` file, the Thryve repository has to be declared as shown in the root and app `build.gradle` files.

### The Root `build.gradle` file

{% tabs %}
{% tab title="build.gradle.kts" %}

```kotlin
allprojects {
    repositories {
        ... 
        maven {         
            url = uri("https://git.thryve.de/api/v4/projects/12/packages/maven")
            credentials {
                    username = thryveUsername
                    password = thryvePassword
            }
        }
        ...
    }
}
```

{% endtab %}

{% tab title="build.gradle" %}

```groovy
allprojects {
    repositories {
        ...
        maven {
            url "https://git.thryve.de/api/v4/projects/12/packages/maven"
            credentials {
                username = thryveUsername
                password = thryvePassword
            }
        }
        ...
    }
}
```

{% endtab %}

{% tab title="settings.gradle.kts" %}

```
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        ...
        maven {
            url = uri("https://git.thryve.de/api/v4/projects/12/packages/maven")
            credentials {
                username = thryveUsername
                password = thryvePassword
            }
        }
        ...
    }
}
```

{% endtab %}
{% endtabs %}

Your username and password are provided in onboarding when signing up with Thryve. It is recommended to store credentials in your project’s `local.properties` or the user-global Gradle properties `gradle.properties`

```
thryveUsername=YOUR_SDK_CREDENTIAL_USER
thryvePassword=YOUR_SDK_CREDENTIAL_PASSWORD
```

### The App `build.gradle` file

Add dependencies to the app `build.gradle` after adding the repository

{% hint style="info" %}
You must use `api` instead of `implementation` to make the Thryve SDK and it's modules work correctly
{% endhint %}

{% hint style="info" %}
Note the change in name of the package. The versions preceding 5.0.0 used  `com.thryve.connector` . Use the appropriate package when upgrading to or downgrading from 5.x
{% endhint %}

## Integrate using Thryve .aar Libraries

The Thryve Android SDK module is available as a standard Android Archive (`aar`). You can download the framework files here:

<details>

<summary>The <code>.aar</code> libraries can be downloaded from here</summary>

| Module Name           | Download Version                                                                                       |
| --------------------- | ------------------------------------------------------------------------------------------------------ |
| Core SDK Module       | [5.2.1](https://git.thryve.de/thryve/sdk/-/blob/master/Android/thryve_core_sdk_5.2.1.aar)              |
| Commons Module        | [5.2.1](https://git.thryve.de/thryve/sdk/-/blob/master/Android/thryve_module_commons_5.2.1.aar)        |
| Health Connect Module | [5.2.1](https://git.thryve.de/thryve/sdk/-/blob/master/Android/thryve_module_hconnect_5.2.1.aar)       |
| Samsung Health Module | [5.2.1](https://git.thryve.de/thryve/sdk/-/blob/master/Android/thryve_module_samsung_health_5.2.1.aar) |
| Observability Module  | [5.2.1](https://git.thryve.de/thryve/sdk/-/blob/master/Android/thryve_module_observability_5.2.1.aar)  |
| ShenAI module         | [5.2.1](https://git.thryve.de/thryve/sdk/-/blob/master/Android/thryve_module_shen_ai_5.2.1.aar)        |
| Android Sample Code   | [5.2.1](https://git.thryve.de/thryve/sdk/-/tree/master/Android/ThryveAndroidSample)                    |

</details>

### Integrating AAR Libraries into Your Application

1. **Add AAR File:**
   1. Place the `.aar` file in a project directory, such as `libs`.
2. **Update `build.gradle`:**
   * Add `implementation files('libs/.aar')`.
3. **Enable Kotlin Support:**
   * Include [annotation processing](https://kotlinlang.org/docs/reference/kapt.html).
4. **Add Android Support Libraries:**
   * Integrate the `Android AppCompat-Library`.
5. **Set Java Compatibility:**
   * Ensure target and source compatibility for Java 1.8.
6. **Set Kotlin JVM Target:**
   * Use Kotlin JVM target version 1.8.

Ensure you include the necessary dependencies for the Thryve SDK and verify that they are all compatible versions.

{% tabs %}
{% tab title="build.gradle.kts" %}

```kotlin
...
android {
    ...
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    ...
}

dependencies {
    ...
    implementation("androidx.appcompat:appcompat:$appCompat")
    implementation("androidx.core:core-ktx:$androidXCore")

    // Referenced in project's build.gradle
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version")

    implementation(files("libs/thryve_core_sdk_${thryve_sdk_version}.aar")

    // CommonsModule import needed for other modules
    implementation(files("libs/thryve_module_commons_${thryve_sdk_version}.aar"))
    
    // Optional modules
    implementation(files("libs/thryve_module_samsung_health_${thryve_sdk_version}.aar"))
    implementation(files("libs/thryve_module_hconnect_${thryve_sdk_version}.aar"))
    ...
    // dependencies for network and others
    implementation("com.google.code.gson:gson:$gson")
    implementation("androidx.legacy:legacy-support-v4:$legacySupport")
    implementation("io.reactivex.rxjava2:rxjava:$rxJava")
    implementation("io.reactivex.rxjava2:rxandroid:$rxAndroid")
    implementation("io.reactivex.rxjava2:rxkotlin:$rxKotlin")
    implementation("io.github.reactivecircus.cache4k:cache4k:$cache4k")

    // dependencies for background sync (Google Fit, Health Connect, Samsung Health)
    implementation("androidx.work:work-runtime:$work")
    implementation("androidx.work:work-runtime-ktx:$work")
    implementation("androidx.work:work-gcm:$work")

    implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle")
    implementation "androidx.lifecycle:lifecycle-process:$lifecycle"

    implementation("androidx.concurrent:concurrent-futures:$concurrentFutures")
    implementation("androidx.concurrent:concurrent-futures-ktx:$concurrentFutures")

    // dependencies for encryption/auth mechanisms
    implementation("androidx.security:security-crypto:$crypto")
    
    // dependencies for health connect
    implementation("androidx.health.connect:connect-client:$healthConnectClient")
    
    // dependency for compression
    implementation("com.github.luben:zstd-jni:$zstdVersion") {
        artifact { type = "aar" }
    }
    
    // dependency for new Samsung Data SDK 
    // skip this dependency if kotlin-parcelize is already added as a plugin(recommended)
    implementation ("org.jetbrains.kotlin:kotlin-parcelize-runtime:$kotlin")
    
   // dependencies for Observability Module
    implementation("io.opentelemetry:opentelemetry-api:$openTelemetryVersion")
    implementation("io.opentelemetry:opentelemetry-sdk:$openTelemetryVersion")
    implementation("io.opentelemetry.semconv:opentelemetry-semconv:$openTelemetrySemanticConventionsVersion")
    implementation("io.opentelemetry:opentelemetry-exporter-otlp:$openTelemetryVersion")// OpenTelemetry Exporters
    
  }
```

{% endtab %}

{% tab title="build.gradle" %}

```gradle
...
  ...
  android {
    ...
    compileOptions {
      sourceCompatibility 1.8
      targetCompatibility 1.8
    }
    kotlinOptions {
        jvmTarget = 1.8
    }
  }
  ...
  dependencies {
    ...
    implementation "androidx.appcompat:appcompat:$appCompat"
    implementation "androidx.core:core-ktx:$androidXCore"

    // Referenced in project's build.gradle
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version'

    implementation files("libs/thryve_core_sdk_${thryve_sdk_version}.aar")

    // CommonsModule import needed for other modules
    implementation files("libs/thryve_module_commons_${thryve_sdk_version}.aar")
    
    // Optional modules
    implementation files("libs/thryve_module_samsung_health_${thryve_sdk_version}.aar")
    implementation files("libs/thryve_module_hconnect_${thryve_sdk_version}.aar")
    ...
    // dependencies for network and others
    implementation "com.google.code.gson:gson:$gson"
    implementation "io.github.reactivecircus.cache4k:cache4k:$cache4k"
    implementation "androidx.legacy:legacy-support-v4:$legacySupport"

    implementation "io.reactivex.rxjava2:rxjava:$rxJava"
    implementation "io.reactivex.rxjava2:rxandroid:$rxAndroid"
    implementation "io.reactivex.rxjava2:rxkotlin:$rxKotlin"
    implementation "io.github.reactivecircus.cache4k:cache4k:$cache4k"

    // dependencies for background sync (Google Fit, Health Connect, Samsung Health)
    implementation "androidx.work:work-runtime:$work"
    implementation "androidx.work:work-runtime-ktx:$work"
    implementation "androidx.work:work-gcm:$work"

    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle"
    implementation "androidx.lifecycle:lifecycle-process:$lifecycle"

    implementation "androidx.concurrent:concurrent-futures:$concurrentFutures"
    implementation "androidx.concurrent:concurrent-futures-ktx:$concurrentFutures"

    // dependencies for Google Fit Native
    implementation "com.google.android.gms:play-services-auth:$playServicesAuth"
    implementation "com.google.android.gms:play-services-fitness:$playServicesFitness"

    // dependencies for encryption/auth mechanisms
    implementation "androidx.security:security-crypto:$crypto"
    
    // dependency for compression
    implementation("com.github.luben:zstd-jni:$zstdVersion") {
        artifact { type = "aar" }
    }
    
    // dependency for new Samsung Data SDK 
    // skip this dependency if kotlin-parcelize is already added as a plugin(recommended)
    implementation "org.jetbrains.kotlin:kotlin-parcelize-runtime:$kotlin"
    
    // dependency for compression
    implementation("com.github.luben:zstd-jni:${zstdVersion}") {
     artifact {
       type = "aar"
     }
    }
    
    // dependencies for Observability Module
    implementation "io.opentelemetry:opentelemetry-api:${openTelemetryVersion}"
    implementation "io.opentelemetry:opentelemetry-sdk:${openTelemetryVersion}"
    implementation "io.opentelemetry.semconv:opentelemetry-semconv:${openTelemetrySemanticConventionsVersion}"
    implementation "io.opentelemetry:opentelemetry-exporter-otlp:${openTelemetryVersion}"// OpenTelemetry Exporters
   
  }
```

{% endtab %}
{% endtabs %}

### Configure dependency versions

Keeping your dependency versions aligned with the supported versions of the Thryve Android SDK is crucial for ensuring compatibility and maintaining the stability of your application.&#x20;

Outdated or mismatched versions can lead to integration issues, such as runtime errors or unexpected behavior, which may hinder app performance and user experience.&#x20;

```groovy
buildscript {
    ext.androidXCore = "1.12.0"
    ext.appCompat = "1.6.1"
    ext.annotation ="1.7.1"
    ext.cache4k = "0.13.0"
    ext.crypto = "1.1.0-alpha06"
    ext.concurrentFutures = "1.1.0"
    ext.gradlePlugin = "8.6.0"
    ext.gson = "2.10.1"
    ext.kotlin = "1.9.23"
    ext.legacySupport = "1.0.0"
    ext.lifecycle = "2.7.0"
    ext.playServicesLocation = "21.2.0"
    ext.playServicesWearable = "18.1.0"
    ext.playServicesFitness = "21.1.0"
    ext.playServicesAuth = "21.0.0"
    ext.rxAndroid2 = "2.1.1"
    ext.rxJava2 = "2.2.21"
    ext.rxKotlin = "2.4.0"
    ext.sdkVersion = "5.2.0"
    ext.work = "2.9.0"
    ext.healthConnectClient = "1.1.0"
    ext.zstdVersion = "1.5.7-6"
    ext.openTelemetryVersion = "1.61.0"
    ext.openTelemetrySemanticConventionsVersion = "1.40.0"
    ...
}
```

{% hint style="info" %}
When updating Thryve SDK versions, always refer to the release changelog for documented dependency version updates.
{% endhint %}

## Custom WorkManager configuration

Thryve Android SDK relies on [Android WorkManager](https://developer.android.com/topic/libraries/architecture/workmanager) for scheduled background task. To provide a custom implementation and rules for background tasks and overwrite Thryve SDK defaulty, implement `Configuration.Provider` and use `WorkManagerInitializer` in your Application class. See the Kotlin code example below for guidance. The code snippet changes the minimum logging level for your app, but the implementation can be further customized to your apps needs.

```kotlin
class App : Application, Configuration.Provider {
    override fun getWorkManagerConfiguration() =
            Configuration.Builder()
                    .setMinimumLoggingLevel(Log.VERBOSE)
                    .build()
```

{% hint style="info" %}
Thryve Android SDK uses WorkManager for background data synchronization for HealthConnect and Samsung Health.
{% endhint %}

## Health Connect project configuration

To integrate Health Connect with your Android app, utilize the `ThryveHealthConnect` module from the Thryve Android SDK. This module enables users to connect with Health Connect on their smartphones and allows your app to retrieve evaluated and aggregated data from various sources linked to Health Connect. You can integrate it by using [Thryve Maven Repository Dependency](/integrate-your-mobile-app/setup-thryve-sdk/android.md#using-thryve-repository-dependencies) or [AAR Libraries](https://docs.thryve.health/integrate-your-mobile-app/setup-thryve-sdk/pages/gd15Xn3eNpfQhy8TrHU5#id-2.-using-the-.aar-libraries) and updating your app's `build.gradle` file accordingly.

{% tabs %}
{% tab title="Thryve Maven Repo " %}

```kotlin
dependencies {
    ...
    api("com.thryve.sdk:hconnect:${thryve_sdk_version}")
}
```

{% endtab %}

{% tab title="AAR Libraries" %}

```kotlin
dependencies {
    ...
    implementation(files("libs/thryve_module_hconnect_5.2.1.aar"))
    implementation("androidx.health.connect:connect-client:1.1.0-rc02")
}
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
The Health Connect SDK supports Android 8 (API level 26) or higher, while the Health Connect app is only compatible with Android 9 (API level 28) or higher. This means that third-party apps can support users with Android 8, but only users with Android 9 or higher can use Health Connect.
{% endhint %}

### Declare Health Connect permissions

To utilize Health Connect, your application must declare all necessary permissions and provide a privacy policy activity for handling the `ACTION_SHOW_PERMISSIONS_RATIONALE` intent within the `AndroidManifest`. Pay attention to the differences in declarations for Android 14 versus older versions.&#x20;

```xml
<activity
    android:name=".PermissionsActivity"
    android:exported="true" >
    <intent-filter>
        <action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
    </intent-filter>
    <meta-data android:name="health_permissions" android:resource="@array/health_permissions"/>
</activity>

<activity-alias
    android:name="AndroidURationaleActivity"
    android:exported="true"
    android:targetActivity=".PermissionsActivity"
    android:permission="android.permission.START_VIEW_PERMISSION_USAGE">
    <intent-filter>
        <action android:name="android.intent.action.VIEW_PERMISSION_USAGE" />
        <category android:name="android.intent.category.HEALTH_PERMISSIONS" />
    </intent-filter>
</activity-alias>
```

Ensure that the declared policies align with the data types set in `ThryveHealthConnectConfig`. Also, remember to declare permissions for background sync and health data history when enabling these features.

```xml
<! -- Required for background sync -->
<uses-permission android:name="android.permission.health.READ_HEALTH_DATA_IN_BACKGROUND" />
<! -- Required for health data history -->
<uses-permission android:name="android.permission.health.READ_HEALTH_DATA_HISTORY" />

<! -- Required to request ACTIVECALORIESBURNED -->
<uses-permission android:name="android.permission.health.READ_ACTIVE_CALORIES_BURNED" />
<! -- Required to request BASALBODYTEMPERATURE -->
<uses-permission android:name="android.permission.health.READ_BASAL_BODY_TEMPERATURE" />
<uses-permission android:name="android.permission.health.READ_BASAL_METABOLIC_RATE" />
<uses-permission android:name="android.permission.health.READ_BLOOD_GLUCOSE" />
<uses-permission android:name="android.permission.health.READ_BLOOD_PRESSURE" />
<uses-permission android:name="android.permission.health.READ_BODY_FAT" />
<uses-permission android:name="android.permission.health.READ_BODY_TEMPERATURE" />
<uses-permission android:name="android.permission.health.READ_BODY_WATER_MASS" />
<uses-permission android:name="android.permission.health.READ_BONE_MASS" />
<uses-permission android:name="android.permission.health.READ_CERVICAL_MUCUS" />
<uses-permission android:name="android.permission.health.READ_EXERCISE" />
<uses-permission android:name="android.permission.health.READ_DISTANCE" />
<uses-permission android:name="android.permission.health.READ_ELEVATION_GAINED" />
<uses-permission android:name="android.permission.health.READ_EXERCISE" />
<uses-permission android:name="android.permission.health.READ_FLOORS_CLIMBED" />
<uses-permission android:name="android.permission.health.READ_HEART_RATE" />
<uses-permission android:name="android.permission.health.READ_HEART_RATE_VARIABILITY" />
<uses-permission android:name="android.permission.health.READ_HEIGHT" />
<uses-permission android:name="android.permission.health.READ_HYDRATION" />
<uses-permission android:name="android.permission.health.READ_INTERMENSTRUAL_BLEEDING" />
<uses-permission android:name="android.permission.health.READ_LEAN_BODY_MASS" />
<uses-permission android:name="android.permission.health.READ_MENSTRUATION" />
<uses-permission android:name="android.permission.health.READ_NUTRITION" />
<uses-permission android:name="android.permission.health.READ_OVULATION_TEST" />
<uses-permission android:name="android.permission.health.READ_OXYGEN_SATURATION" />
<uses-permission android:name="android.permission.health.READ_POWER" />
<uses-permission android:name="android.permission.health.READ_RESPIRATORY_RATE" />
<uses-permission android:name="android.permission.health.READ_RESTING_HEART_RATE" />
<uses-permission android:name="android.permission.health.READ_SEXUAL_ACTIVITY" />
<uses-permission android:name="android.permission.health.READ_SLEEP" />
<uses-permission android:name="android.permission.health.READ_SPEED" />
<uses-permission android:name="android.permission.health.READ_STEPS" />
<uses-permission android:name="android.permission.health.READ_TOTAL_CALORIES_BURNED" />
<uses-permission android:name="android.permission.health.READ_VO2_MAX" />
<uses-permission android:name="android.permission.health.READ_WEIGHT" />
<uses-permission android:name="android.permission.health.READ_MINDFULNESS" />
<uses-sdk tools:overrideLibrary="androidx.health.connect.client" />
```

## Samsung Health project configuration

You must suppress warnings after adding the `ThryveSamsungHealth` module, include `-dontwarn com.samsung.**` in your `proguard-rules` file.

{% hint style="success" %}
Read [the ThryveBLE Set-up guide](https://open-2v.gitbook.com/url/preview/site_WRtC1/~/revisions/8MPw4Z6n3dOec6crWrLf/integrate-your-mobile-app/connect-bluetooth-devices#set-up) for additional information on how to set-up the ThryveBLE module
{% endhint %}


---

# 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.thryve.health/integrate-your-mobile-app/setup-thryve-sdk/android.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.
