# Android

The SDK is available as an AAR (recommended) and JAR library. The library is available in the MavenCentral and [GitHub repository](https://github.com/devtodev-analytics/android-sdk-2.0).

## Google implementation

{% hint style="warning" %}
**Attention!**&#x20;

From the SDK version `com.devtodev:android-analytics:'2.2.3'` and above you need to add `com.devtodev:android-google:'1.0.1'`.&#x20;

This framework encapsulates work with Google ads ID. When developing and publishing apps for kids [COPPA](https://www.ftc.gov/tips-advice/business-center/privacy-and-security/children's-privacy), you don’t need `com.devtodev:android-google`. You can find more information about working with [COPPA](https://www.ftc.gov/tips-advice/business-center/privacy-and-security/children's-privacy) at the end of this guide.
{% endhint %}

### Step 1. **Declare repositories**

In the Project **`build.gradle`**  file, declare the `mavenCentral` repository:

```groovy
repositories {
   //.. other repositories 
   mavenCentral()
}
```

### Step 2. Add Gradle Build Dependencies

If you use Gradle for building apps specify the following dependencies in the application **`build.gradle`** file.&#x20;

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

```kotlin
dependencies {
    // Requirement
    implementation ("com.google.code.gson:gson:*.*.*")
    implementation ("com.google.android.gms:play-services-ads-identifier:*.*.*")
    // Starting from version 2.2.3 and above, it is required
    implementation ("com.devtodev:android-google:*.*.*")

    // if you use AAR (recommended) or JAR downloaded from GitHub, please add:
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.aar"))))

    // or just add the dependency, get the latest version from
    // https://mvnrepository.com/artifact/com.devtodev/android-analytics
    implementation ("com.devtodev:android-analytics:*.*.*")

    // Optional (recommended)
    implementation ("com.android.installreferrer:installreferrer:*.*.*")
}
```

{% endtab %}

{% tab title="Groovy" %}

```groovy
dependencies {
    // Requirement
    implementation 'com.google.code.gson:gson:*.*.*'
    implementation 'com.google.android.gms:play-services-ads-identifier:*.*.*'
    // Starting from version 2.2.3 and above, it is required
    implementation 'com.devtodev:android-google:*.*.*'
    
    // if you use AAR (recommended) or JAR downloaded from GitHub, please add:
    implementation fileTree(dir: "libs", include: ["*.aar"]) 
    
    // or just add the dependency, get the latest version from
    // https://mvnrepository.com/artifact/com.devtodev/android-analytics
    implementation 'com.devtodev:android-analytics:*.*.*'
    
    // Optional (recommended)
    implementation 'com.android.installreferrer:installreferrer:*.*.*'
}
```

{% endtab %}
{% endtabs %}

#### Peculiarities of working with dependencies <a href="#peculiarities-of-working-with-dependencies" id="peculiarities-of-working-with-dependencies"></a>

**Working with Advertising ID with Android API level less than 26**

If you plan to use `com.google.android.gms:play-services-ads-identifier:18.2.0` and above, you need to add `com.android.tools:desugar_jdk_libs` to maintain backward compatibility with devices with API level less than 26, see [![](https://www.gstatic.com/devrel-devsite/prod/vec94db9b1329e6c4d1d9b6b24ba16ad6c02043dcd66ba9c6a8f3d8fa0af3eec7/android/images/favicon.svg)Use Java 8 language features and APIs  |  Android Studio  |  Android Developers](https://developer.android.com/studio/write/java8-support)

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

```kotlin
android {
    defaultConfig {
        // Required when setting minSdkVersion to 20 or lower
        multiDexEnabled = true
    }
    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled = true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
    // For AGP 7.4+
    coreLibraryDesugaring ("com.android.tools:desugar_jdk_libs:2.0.3")
    // For AGP 7.3
    // coreLibraryDesugaring ("com.android.tools:desugar_jdk_libs:1.2.3")
    // For AGP 4.0 to 7.2
    // coreLibraryDesugaring ("com.android.tools:desugar_jdk_libs:1.1.9")
}
```

{% endtab %}

{% tab title="Groovy" %}

```groovy
android {
    defaultConfig {
        // Required when setting minSdkVersion to 20 or lower
        multiDexEnabled true
    }
    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
    // For AGP 7.4+
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'
    // For AGP 7.3
    // coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.3'
    // For AGP 4.0 to 7.2
    // coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.9'
}
```

{% endtab %}
{% endtabs %}

We also recommend AGP 8.0+ as it makes it easier to configure `com.android.tools:desugar_jdk_libs`

## Huawei implementation

### Step 1. **Declare repositories**

If you use Gradle for compiling apps, declare the following dependencies in the build.gradle file in the dependency block:

```groovy
repositories {
   //.. other repositories 
   mavenCentral()
   maven { url 'https://developer.huawei.com/repo/' }
}

allprojects {
    repositories {
        //.. other repositories
        mavenCentral()
        // Configure the Maven repository address for the HMS Core SDK.
        maven {url 'https://developer.huawei.com/repo/'}
    }
}
```

### Step 2. Add Gradle Build Dependencies

In the Project `build.gradle` file declare the agconnect plugin

```groovy
dependencies {
    classpath 'com.huawei.agconnect:agcp:*.*.*'
}
```

In the application build.gradle file declare the following dependencies:

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

```kotlin
dependencies {
    // Requirement
    implementation ("com.google.code.gson:gson:*.*.*")
    implementation ("com.huawei.agconnect:agconnect-core:*.*.*")
    implementation ("com.huawei.hms:opendevice:*.*.*")
    // Starting from version 2.2.3 and above, it is required
    implementation ("com.devtodev.android-huawei:*.*.*")
    implementation ("com.huawei.hms:ads-identifier:*.*.*")
    
    // if you use AAR (recommended) or JAR downloaded from GitHub, please add:
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.aar"))))
    
    // or just add the dependency, get the latest version from
    // https://mvnrepository.com/artifact/com.devtodev/android-analytics
    implementation ("com.devtodev:android-analytics:*.*.*")
}
```

{% endtab %}

{% tab title="Groovy" %}

```groovy
dependencies {
    // Requirement
    implementation 'com.google.code.gson:gson:*.*.*'
    implementation 'com.huawei.agconnect:agconnect-core:*.*.*'
    implementation 'com.huawei.hms:ads-identifier:*.*.*'
    implementation 'com.huawei.hms:opendevice:*.*.*'
    // Starting from version 2.2.3 and above, it is required
    implementation 'com.devtodev.android-huawei:*.*.*'
    
    // if you use AAR (recommended) or JAR downloaded from GitHub, please add:
    implementation fileTree(dir: "libs", include: ["*.aar"]) 
    
    // or just add the dependency, get the latest version from
    // https://mvnrepository.com/artifact/com.devtodev/android-analytics
    implementation 'com.devtodev:android-analytics:*.*.*'
}
```

{% endtab %}
{% endtabs %}

And add a plugin:

```groovy
plugins {
    //.. other plugins
    id 'com.huawei.agconnect'
}
```

For more information see [huawei official documents](https://developer.huawei.com/consumer/en/doc/development/hiai-Guides/config-maven-0000001050040031).

### Step 3. AppGallery

The `com.devtodev.android-huawei` framework works with[ OAID](https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/oaid-0000001050316244) and [ODID](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/odid-0000001051063255) IDs. In case the [OAID](https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/oaid-0000001050316244) is undefined, we use the [ODID](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/odid-0000001051063255). For both IDs to work correctly, take the following steps:&#x20;

1. Create a project and an app in AppGallery. Open AppGalleryConnect → Project Settings.
2. Sign your app using a certificate (see [here](https://developer.android.com/studio/publish/app-signing)).
3. Enter SHA-256 certificate in the App information section. Read more about certificate creation [here](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/config-agc-0000001050166285#EN-US_TOPIC_0000001054452903__section10260203515546).

After taking all the steps described above, open the ‘App information’ section and download agconnect-services.json. You need to place this file in the app folder ([read more](https://developer.huawei.com/consumer/en/doc/development/hiai-Guides/add-appgallery-0000001050038080)).

{% hint style="warning" %}
If during testing the app you see that [OAID](https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/oaid-0000001050316244) is unavailable and [OAID](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/odid-0000001051063255) throws [errors](https://developer.huawei.com/consumer/en/doc/development/HMS-2-References/hmssdk_huaweiiap_api_reference_errorcode), you need to first of all check that the tested build is signed with a certificate.
{% endhint %}

## SDK Initialization

Use the following way to initialize the library in the first Activity **`onCreate()`** method:

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

```kotlin
class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
      
        val analyticsConfiguration = DTDAnalyticsConfiguration()
        analyticsConfiguration.logLevel = DTDLogLevel.Error
        DTDAnalytics.initialize("App ID", analyticsConfiguration, context = this)
    }
}
```

{% endtab %}

{% tab title="Java" %}

```java
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        DTDAnalyticsConfiguration configuration = new DTDAnalyticsConfiguration();
        configuration.setLogLevel(DTDLogLevel.Error);
        DTDAnalytics.INSTANCE.initialize("App ID", configuration, context);
    }
}
```

{% endtab %}
{% endtabs %}

You can find the `App ID` in the settings of the respective app in devtodev (Settings → SDK → Integration → [Credentials](https://docs.devtodev.com/reports-and-functionality/project-related-reports-and-fuctionality/settings#integration)).&#x20;

For [Cross-platform type projects](https://docs.devtodev.com/getting-started/cross-platform-application) use `App ID + Platform ID` .

* **`config`** - is a **`DTDAnalyticsConfiguration`** object instance that is used for specifying additional properties during initialization.

**`DTDAnalyticsConfiguration`**

<table data-header-hidden><thead><tr><th width="257.43845371312307">Parameter</th><th width="181">Type</th><th>Description</th></tr></thead><tbody><tr><td>Parameter</td><td>Type</td><td>Description</td></tr><tr><td><strong><code>currentLevel</code></strong></td><td>Integer</td><td>The player level at the moment of devtodev SDK initialization. It’s optional but we recommend using it for improving data accuracy.</td></tr><tr><td><strong><code>userId</code></strong></td><td>String</td><td>A custom user ID assigned by the developer. In the case of default calculation by device IDs, the identifier can be used for searching users in devtodev. In case the project uses calculation by user IDs, the parameter is mandatory because it becomes the principal calculation ID in devtodev.</td></tr><tr><td><strong><code>trackingAvailability</code></strong></td><td>DTDTrackingStatus (enum)</td><td>The property allows or disallows devtodev tracking of the user. By default, it is set to <em><strong><code>DTDTrackingStatus.enable</code></strong></em>. SDK stores the previously assigned value. Pass <em><strong><code>DTDTrackingStatus.disable</code></strong></em> if the user opted out of tracking in line with GDPR.</td></tr><tr><td><strong><code>logLevel</code></strong></td><td>DTDLogLevel (enum)</td><td>The level of logging the SDK activity. The <em><strong><code>DTDLogLevel.no</code></strong></em> value is used by default. For troubleshooting during integration it is recommended to set it to <em><strong><code>DTDLogLevel.debug</code></strong></em>, and either switch it off <em><strong><code>DTDLogLevel.no</code></strong></em>. Use <em><strong><code>DTDLogLevel.no</code></strong></em> in the release version.</td></tr></tbody></table>

Example:

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

```kotlin
val config = DTDAnalyticsConfiguration()
config.currentLevel = 1
config.userId = "CustomUserID"
config.trackingAvailability = DTDTrackingStatus.Enable
config.logLevel = DTDLogLevel.No
DTDAnalytics.initialize("App ID", config, context = this)
```

{% endtab %}

{% tab title="Java" %}

```java
DTDAnalyticsConfiguration config = new DTDAnalyticsConfiguration();
config.setCurrentLevel(1);
config.setUserId("CustomUserID");
config.setTrackingAvailability(DTDTrackingStatus.Enable);
config.setLogLevel(DTDLogLevel.No);
DTDAnalytics.INSTANCE.initialize("App ID", config, context);
```

{% endtab %}
{% endtabs %}

## **SDK obfuscation rules**

Add the following strings to the **`proguard-rules.pro`** file of your app

```kotlin
-keep class com.devtodev.** { *; }
-dontwarn com.devtodev.**
// For Google Mobile Services 
-keep class com.google.android.gms.** { *; }
// For Huawei Mobile Services 
-keep class com.huawei.hms.**{*;}
```

## **Apps targeted at children**

When developing and publishing apps targeted at children under 13 years old, you need to ensure special conditions for data processing. Any mobile app aimed at children or intended for users in a region with strict regulations on child online protection, must comply with current laws.

{% hint style="info" %}
Please study the following requirements:

* USA: [Children’s Online Privacy Protection Act (COPPA)](https://www.ftc.gov/tips-advice/business-center/privacy-and-security/children's-privacy)&#x20;
* EU: [General Data Protection Regulation (GDPR) Article 8](https://gdpr-info.eu/art-8-gdpr/)
  {% endhint %}

If your app has to comply with the legal requirements (COPPA), use the following recommendations:

1. Implement the `coppaControlEnable` method. The method disables collection of ad IDs and vendor IDs.
2. If your app is using Google services, remove the following dependencies from gradle:

   ```
   'com.google.android.gms:play-services-ads-identifier'  
   'com.devtodev:android-google'
   ```
3. If your app is using Huawei services, remove the following dependencies from gradle:

   ```
   'com.huawei.hms:ads-identifier' 
   'com.huawei.hms:opendevice' 
   'com.devtodev.android-huawei'
   ```

{% hint style="warning" %}
Call the `coppaControlEnable` method before SDK initialization. If the method was not called, the SDK will work as before.
{% endhint %}

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

```kotlin
DTDAnalytics.coppaControlEnable()
DTDAnalytics.initialize("App ID", config, context = this)
```

{% endtab %}

{% tab title="Java" %}

```java
DTDAnalytics.INSTANCE.coppaControlEnable();
DTDAnalytics.INSTANCE.initialize("App ID", config, context);
```

{% endtab %}
{% endtabs %}
