---
metadata:
  - name: generator
    content: Diplodoc Platform v5.64.0
alternate:
  - https://appmetrica.yandex.com/docs/en/sdk/android/analytics/quick-start.md
  - https://appmetrica.yandex.com/docs/ru/sdk/android/analytics/quick-start.md
  - https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/quick-start.md
  - href: https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/quick-start.md
    type: text/markdown
    title: Markdown version
  - href: https://appmetrica.yandex.com/docs/zh/llms.txt?revision=r21920615
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://appmetrica.yandex.com/docs/zh/llms.txt?revision=r21920615

# SDK 集成

Android 的 AppMetrica SDK 以 AAR 库的形式发布。  该库可在 [Maven 仓库](https://mvnrepository.com/artifact/io.appmetrica.analytics/analytics) 中获取。

下面的步骤说明如何启用并初始化 AppMetrica SDK：

## 步骤 1. 将AppMetrica库添加到你的项目中 {#integration}

如果你使用 Gradle 构建应用，请在应用的 Gradle 文件中添加以下依赖：

{% list tabs %}

- build.gradle.kts

  ```kotlin translate=no
  dependencies {
      // AppMetrica SDK.
      implementation("io.appmetrica.analytics:analytics:8.5.1")
  }
  ```

- build.gradle

  ```groovy translate=no
  dependencies {
      // AppMetrica SDK.
      implementation 'io.appmetrica.analytics:analytics:8.5.1'
  }
  ```

{% endlist %}

## 步骤 2. 初始化 AppMetrica 库 {#initialization}

{% note alert "" %}

在初始化过程中，请考虑 AppMetrica 库的特点。 要了解更多信息，请查看[库的工作原理AppMetrica](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-features.md)。

{% endnote %}

在您的应用中初始化 AppMetrica 库。 要做到这一点，请扩展`Application`类，并重写`onCreate()`方法，如下所示：

{% list tabs group=instructions %}

- Kotlin

  ```kotlin translate=no
  class YourApplication : Application() {
      override fun onCreate() {
          super.onCreate()
          // Creating an extended library configuration.
          val config = AppMetricaConfig.newConfigBuilder(API_KEY).build()
          // Initializing the AppMetrica SDK.
          AppMetrica.activate(this, config)
      }
  }
  ```

- Java

  ```java translate=no
  public class YourApplication extends Application {
      @Override
      public void onCreate() {
          super.onCreate();
          // Creating an extended library configuration.
          AppMetricaConfig config = AppMetricaConfig.newConfigBuilder(API_KEY).build();
          // Initializing the AppMetrica SDK.
          AppMetrica.activate(this, config);
      }
  }
  ```

{% endlist %}

{% cut "API 密钥是什么？" %}

<!-- source: zh/sdk/_includes/api-key.md -->
API 密钥是在AppMetrica网页界面进行[应用注册](https://appmetrica.yandex.com/application/new)时分配的唯一应用 ID。 你可以在 **设置** → **主页** 中找到它。
<!-- endsource: zh/sdk/_includes/api-key.md -->

{% endcut %}

在用扩展的启动配置初始化库时，[日志记录](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-logs.md) 会被启用。

## 步骤 3. (可选) 配置位置检测 {#location}

位置检测帮助您了解用户的地理位置。 默认情况下，发送设备位置被禁用，但可以在启动配置中启用。 详情请参见[使用库发送设备位置](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-operations.md#track-location)。

## 第 4 步。 (可选) 配置事件发送、用户属性和收入 (#send)

要收集用户在应用中的操作信息，请设置发送自定义事件。 要了解更多信息，请参见[发送自定义事件](https://appmetrica.yandex.com/docs/zh/data-collection/about-events.md)。

要收集用户信息，请设置发送用户属性。 要了解更多信息，请查看[用户配置文件](https://appmetrica.yandex.com/docs/zh/data-collection/about-profiles.md)。

{% note info "" %}

与事件不同，个人资料属性只能有一个值。 当您发送新的属性值时，旧的值会被覆盖。

{% endnote %}

要跟踪应用内购买，请设置收入发送。 要了解更多信息，请查看[应用内购买](https://appmetrica.yandex.com/docs/zh/data-collection/about-revenue.md)。

## 第5步。 测试库的操作 {#test}

{% note alert "" %}

在测试库之前，请确保 SDK 已按照[推荐](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-features.md#recommendations)的方式进行初始化。

{% endnote %}

在开始测试之前，我们建议设置[将数据传输到额外的API密钥](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-operations.md#reporter-different-apikey)或[添加使用新API密钥的应用](https://appmetrica.yandex.com/about)。 这会帮助你将测试数据与核心统计数据分开。

要测试库的功能：

1. 启动包含 AppMetrica SDK 的应用，并使用一段时间。
2. 确保您的设备已连接到互联网。
3. 在 AppMetrica 界面中，请确保：
   - 在 [受众](https://appmetrica.yandex.com/docs/zh/mobile-reports/audience-report.md) 报告中有新用户。
   - 在 **Engagement** → **Sessions** 报告中的会话数已增加。
   - 在 [Events](https://appmetrica.yandex.com/docs/zh/mobile-reports/events-report.md) 和 [Profiles](https://appmetrica.yandex.com/docs/zh/mobile-reports/profile-report.md) 报告中有事件和个人资料属性.

## 了解更多 {#learn-more}

- [发送自定义事件](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-operations.md#report-event)
- [发送用户属性](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-operations.md#send-attribute-profile)
- [发送电商事件](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-operations.md#send-ecommerce)
- [发送收入数据](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-operations.md#send-revenue)
- [发送广告收入数据](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-operations.md#send-adrevenue)
- [如何启用发送用户位置？](https://appmetrica.yandex.com/docs/zh/troubleshooting/troubleshooting.md#region)
- [如何确保已安装最新版本的Android库？](https://appmetrica.yandex.com/docs/zh/troubleshooting/troubleshooting.md#newest-android-version)

## 故障排查 {#faq}

- [会话数量保持不变](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-errors.md#session)
- [报告中没有事件](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-errors.md#events)
- [向项目添加库时出错](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-errors.md#add-in-project)
- [用户会话时长无效，手动跟踪](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-errors.md#listen-root)
- [由 AppMetrica 库导致的高功耗](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-errors.md#power)
- [使用自动收集 Fyber 广告收入数据时出现代码冲突](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-errors.md#fyber-unsupport-adrevenue)
- [我的问题没有列出](https://appmetrica.yandex.com/docs/zh/sdk/android/analytics/android-errors.md#not-found)

<!-- source: zh/_includes/feedback-button-3.md -->
如果您没有找到所需答案，可以通过反馈表单提交您的问题。 请尽可能详细地描述问题。 如果可能，请附上截图。

<a href="../troubleshooting/feedback-new">
  <span class="button">联系支持</span>
</a>

<a href="../troubleshooting/feedback-docs">
  <span class="button">为文档提出改进建议</span>
</a>
<!-- endsource: zh/_includes/feedback-button-3.md -->
