Updated on 2025-09-07 GMT+08:00

LTS Android SDK

If you need to collect and analyze user information on Android, such as browser, browsing behavior, device, device exception, and network usage records, you can use this SDK to report logs to LTS in Kotlin and Java.

This function is available for whitelisted users in regions CN North-Beijing4, CN East-Shanghai1, and CN South-Guangzhou. To use it, create a service ticket.

Transport Protocol

HTTPS

Prerequisites

  • You have obtained the name of the region where LTS is deployed.
  • You have obtained a project ID of your Huawei Cloud account. For details, see API Credentials.
  • You have obtained the IDs of the target log group and log stream in LTS.

Version Updates

Table 1 Version updates

Version

Download Address

Verification File Download Address

Description

System

1.0.28

Link

Link

Resolved the conflict caused by project configuration since version 1.0.26.

Android 7 or later

0.0.5

Maven Central

-

Retained the functions in version 1.0.28 and added support for Kotlin 1.3.x.

Android 7 or later

1.0.27

Link

Link

Optimized the cache efficiency.

Android 7 or later

0.0.4

Maven Central

-

Retained the functions in version 1.0.27 and added support for Kotlin 1.3.x.

Android 7 or later

1.0.26

Link

Link

Fixed the data exception issue caused by asynchronous modification of input parameters.

Android 7 or later

1.0.25

Link

Link

Enhanced code robustness and added parameter protection.

Android 7 or later

1.0.24

Link

Link

  • Added support for regions CN East-Shanghai1 and CN South-Guangzhou.
  • Modified the program directory to com.cloud.lts.*.

Android 7 or later

1.0.21

Link

Link

Discarded the setconfig method and replaced it with LTS SDK's construction method.

Android 7 or later

1.0.19

Link

Link

  • Modified the reporting interval threshold.
  • Fixed the issue that the cache cannot be obtained after the configuration information is modified.

Android 7 or later

1.0.18

Link

Link

  • Enhanced code robustness and fixed the crashing issue caused by multiple instances.
  • Fixed the issue that the minimum Android API version is incorrectly defined.
  • Added the API for setting the debug log level: setLogLevel.
  • Modified the type of the input parameter label for sending logs to support multi-layer nesting.
  • Added support for the array type of the input parameter content for sending logs.
  • Added support for the maximum number of cached storage records. If this is exceeded, cached records are discarded first before new records are stored.
  • Modified the control policy for request failures to support progressive waiting.
  • Optimized the output of local error logs.

Android 7 or later

1.0.17

Link

Link

This is the first release. Four interfaces are provided: init, config, report, and reportImmediately.

Android 7 or later

Installing the Android SDK

  1. Integrate the access SDK.

    1. Integration using the Maven repository
      1. Add dependencies to build.gradle.
        plugins {
            ...
            id 'org.jetbrains.kotlin.android' version 'KOTLIN_VERSION' apply false  // Add kotlin 1.6.20 or later for 1.x.x and 1.3.61 or later for 0.0.x.
            ...
        }
      2. Add dependencies to app/build.gradle.
        dependencies {
            ...
            implementation 'io.github.lts-sdk:lts-sdk-android:1.0.28'
            ...
        }
      3. Add the Maven repository to settings.gradle.
        pluginManagement{
            ...
            repositories {
                ...
                // Add the following content:
                mavenCentral()
           }
        }
        dependencyResolutionManagement {
            ...
            repositories {
                ...
                // Add the following content:
                mavenCentral()
           }
        }
    2. Manual integration
      1. Download the LTS SDK package.
      2. Decompress the package to a specific directory.
      3. Add the decompressed AAR static library file to your project.
      4. Add dependencies to build.gradle.
        plugins {
            ...
            id 'org.jetbrains.kotlin.android' version 'KOTLIN_VERSION' apply false  // Add kotlin 1.6.20 or later for 1.x.x and 1.3.61 or later for 0.0.x.
            ...
        }
      5. Add dependencies to app/build.gradle.
        dependencies {
            ...
            implementation fileTree(dir: $dir, include: ['*.aar']) // Enter the folder where the .aar package is located, for example, libs.
            implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0' // 0.0.x depends on 1.3.3.
            implementation 'androidx.core:core-ktx:1.7.0'// 0.0.x depends on 1.2.0.
            implementation 'com.google.code.gson:gson:2.8.9'
            kapt 'androidx.room:room-compiler:2.3.0'    // 0.0.x depends on 2.2.6.
            api 'androidx.room:room-runtime:2.3.0'      // 0.0.x depends on 2.2.6.
            api 'androidx.room:room-common:2.3.0'       // 0.0.x depends on 2.2.6.
            ...
        }
      6. Add the Maven repository to settings.gradle.

        If your Gradle version is earlier than 7.0, add the Maven repository to the build.gradle file.

        pluginManagement{
            ...
            repositories {
                ...
                // Add the following content:
                google()
                mavenCentral()
           }
        }
        dependencyResolutionManagement {
            ...
            repositories {
                ...
                // Add the following content:
                google()
                mavenCentral()
           }
        }

  2. Perform initialization. For details about the parameters, see Table 4.

    1. Add dependencies.
      import com.cloud.lts.UserConfig
      import com.cloud.lts.LTSSDK
    2. Kotlin: example initialization code
      // Note: Hard-coded or plaintext AK and SK of the account for authentication are risky. For security, encrypt them. Decrypt them only when necessary.
      val ak: String = getData("HUAWEICLOUD_SDK_AK")
      val sk: String = getData("HUAWEICLOUD_SDK_SK")
      // LTS parameter configuration
      val userConfig = UserConfig.Builder()
      // Mandatory parameters
      .setRegion(USER_REGION) // Region of LTS
      .setProjectId(PROJECT_ID) // Project ID of the account
      .setGroupId(LOG_GROUP_ID) // LTS log group ID
      .setStreamId(LOG_STREAM_ID) // LTS log stream ID
      .setAccessKey(ak) // Access key
      .setSecretKey(sk) // Secret access key
      // Optional parameters
      .setUrlHost(url) // LTS public network domain name
      .setCacheThreshold(200)
      .setTimeInterval(60)
      .setIsReportBackground(true)
      .setIsReportLaunch(false)
      .build()
      // LTS initialization method
      ltssdk = LTSSDK( </* Application Instance */> , userConfig)
    3. Java: example initialization code. The LTS SDK supports multi-instance log reporting. Instances are created based on different configurations for reporting.
      // Note: Hard-coded or plaintext AK and SK of the account for authentication are risky. For security, encrypt them. Decrypt them only when necessary.
      String ak = getData("HUAWEICLOUD_SDK_AK")
      String sk = getData("HUAWEICLOUD_SDK_SK")
      // LTS parameter configuration
      UserConfig userConfig = new UserConfig.Builder()
       // Mandatory parameters
      .setRegion(USER_REGION) // Region of LTS
      .setProjectId(PROJECT_ID) // Project ID of the account
      .setGroupId(LOG_GROUP_ID) // LTS log group ID
      .setStreamId(LOG_STREAM_ID) // LTS log stream ID
      .setAccessKey(ak) // Access key
      .setSecretKey(sk) // Secret access key
      // Optional parameters
      .setUrlHost(url) // LTS public network domain name
      .setCacheThreshold(200)
      .setTimeInterval(60)
      .setIsReportBackground(true)
      .setIsReportLaunch(false)
      .build();
      // LTS initialization method
      ltssdk = new LTSSDK( </* Application Instance */> , userConfig);

  3. Report logs. LTS provides two log reporting methods.

    Table 2 Log reporting methods

    Method

    Description

    report(content, labels)

    LTS stores logs in the local database and reports them in real time based on the configured policy.

    reportImmediately(content, labels)

    Report logs immediately.

    Table 3 Parameters

    Name

    Description

    content

    Log content. The value can be a dictionary or dictionary array. A maximum of 300 key-value pairs are supported. The maximum length of the JSON string converted from content is 30 × 1024. If the length exceeds the limit, the excess part will be truncated.

    labels

    Log labels.

    • The value can be a dictionary or null.
    • Maximum number of outermost key-value pairs: 50.
    • Maximum length of the outermost key: 64.
    • The value can contain letters, digits, and underscores (_), and must start with a letter.
    • The maximum length of a dictionary converted to a JSON string is 30,720 characters. If the length exceeds 30,720 characters, the log cannot be reported.
    • Kotlin sample code:
      var ltssdk: LTSSDK
      val food = hashMapOf(Pair("food_1", "rice"), Pair("food_2", "wheat"), Pair("food_3", "egg"))
      val fruit = hashMapOf(Pair("fruit_1", "apple"), Pair("fruit_2", "pear"), Pair("fruit_3", "banana"))
      val label = hashMapOf( Pair("date","2023-10-01"))
      val contents = listOf(food, fruit).toTypedArray()
      ltssdk.report(food, label) // Cache and report a single record, with labels
      ltssdk.report<String, String>(food)    // Cache and report a single record, without labels
      ltssdk.reportImmediately(food, label)    // Immediately report a single record, with labels
      ltssdk.reportImmediately<String, String>(food)    // Immediately report a single record, without labels
      ltssdk.report(contents, label)    // Cache and report multiple records, with labels
      ltssdk.report<String, String>(contents)    // Cache and report multiple records, without labels
      ltssdk.reportImmediately(contents , label)    // Immediately report multiple records, with labels
      ltssdk.reportImmediately<String, String>(contents)    // Immediately report multiple records, without labels
    • Java sample code:
      LTSSDK ltssdk;
      HashMap fruit = new HashMap<String, String>(){{
          put("fruit_1", "apple");
          put("fruit_2", "pear");
          put("fruit_3", "banana");
      }};
      HashMap food = new HashMap<String, String>(){{
          put("food_1", "rice");
          put("food_2", "wheat");
          put("food_3", "egg");
      }};
      HashMap<String, String> labels = new HashMap(){{
          put("date","2023-10-01");
      }};
      HashMap[] contents = {food, fruit};
      ltssdk.report(food);  // Cache and report a single record, without labels
      ltssdk.report(food, labels);  // Cache and report a single record, with labels
      ltssdk.reportImmediately(food);  // Immediately report a single record, without labels
      ltssdk.reportImmediately(food, labels);  // Immediately report a single record, with labels
      ltssdk.report(contents); // Cache and report multiple records, without labels
      ltssdk.report(contents, labels); // Cache and report multiple records, with labels
      ltssdk.reportImmediately(contents);  // Immediately report multiple records, without labels
      ltssdk.reportImmediately(contents , labels);  // Immediately report multiple records, with labels

Parameters

Table 4 Initialization parameters

Parameter

Type

Mandatory

Default Value

Description

projectId

String

Yes

-

Project ID of the account.

accessKey

String

Yes

-

AK of the account.

Hard-coded or plaintext AK and SK of the account for authentication are risky. For security, encrypt them. Decrypt them only when necessary.

secretKey

String

Yes

-

SK of the account. Note: Hard-coded or plaintext AK and SK of the Huawei Cloud account for authentication are risky. For security, encrypt them. Decrypt them only when necessary.

region

String

Yes

-

Region of LTS.

groupId

String

Yes

-

LTS log group ID.

streamId

String

Yes

-

LTS log stream ID.

url

String

No

null

Public network domain name for reporting. If it is not set, a link is automatically generated based on the region. The format is as follows: https://lts-access.{region}.myhuaweicloud.com.

cacheThreshold

Long

No

200

When the number of logs stored in the local database reaches this threshold, an alarm is reported. Value range: 30 to 1000.

timeInterval

Long

No

3

Interval for triggering reporting. Value range: 1 to 1800, in seconds.

isReportBackground

Boolean

No

true

Whether to report logs when the app switches to running in the background.

isReportLaunch

Boolean

No

false

Whether to report logs upon app startups.

Log Reporting Policies

This SDK supports the following log reporting policies: threshold-based reporting, scheduled reporting, reporting when the app is switched to the background, and reporting upon startup. Set parameters such as cacheThreshold, timeInterval, isReportBackground, and isReportLaunch based on the scenario.

Configuring Permissions

The LTS SDK requires the network permission of Android. If this permission is not configured, add the following content to the Manifest.xml file of the app:

manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
...
// Add the following content:
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>

Obfuscation Configuration

To achieve code obfuscation, add the following code segment to the obfuscation configuration file:

-keep class com.cloud.**{*;} 

Debugging

Enable debugging if needed. Then you can monitor log reporting in real time on the console, and make adjustments accordingly.

Call the static method of the LTS SDK to make adjustments. The SDK supports five log levels: Debug, Info, Warning, Error, and Off. The default log level is Off. The five levels correspond to the following parameters: LogLevel.DEBUG, LogLevel.INFO, LogLevel.WARNING, LogLevel.ERROR, and LogLevel.OFF. The following is a code example:

LTSSDK.setLogLevel(LogLevel.DEBUG)

Obtaining Parameters

  • Regions
    Table 5 Regions

    Name

    Region

    CN North-Beijing4

    cn-north-4

    CN East-Shanghai1

    cn-east-3

    CN South-Guangzhou

    cn-south-1

  • To obtain a log group's ID, choose Log Management in the navigation pane of the LTS console and hover over the target log group's name.
  • To obtain a log stream's ID, click the down arrow on the left of the corresponding log group name to expand the log stream list, then hover over the target log stream's name.