Cài đặt SDK

Tìm hiểu cách tải xuống và cài đặt SDK Android.

Trước khi bạn bắt đầu

Bạn cần Android Studio để làm theo các hướng dẫn này.

Cài đặt SDK Android

Cài đặt Android SDK bằng phương thức ưa thích của bạn: Qua Gradle hoặc theo cách thủ công.

Install using Gradle

Được đề xuất

Bước 1: Khai báo kho lưu trữ
In the Project build.gradle file, declare the mavenCentral repository:

// ...
repositories {
   mavenCentral()
}
/// ...

Bước 2: Thêm phần phụ thuộc
In the application build.gradle file, add the latest Android SDK package:

dependencies {
    // Get the latest version from https://mvnrepository.com/artifact/com.appsflyer/af-android-sdk
    implementation 'com.appsflyer:af-android-sdk:6.9.0' 
}

Manual install

  1. Trong Android Studio, chuyển cấu trúc thư mục từ Android sang Project (Dự án):
  2. Tải xuống SDK Android mới nhất và dán nó vào dự án Android của bạn, trong app > libs (ứng dụng > thư viện).
  3. Nhấp chuột phải vào jar mà bạn đã dán và chọn Add As Library. Khi được nhắc, hãy nhấp vào Refactor.

    Nếu được nhắc thực hiện lưu vào git, hãy nhấp vào OK

    .

Đặt các quyền cần thiết

Add the following permissions to AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

The AD_ID permission

In early 2022, Google announced a change to the behavior of Google Play Services and fetching of the Android Advertising ID. According to the announcement, apps targeting Android 13 (API 33) and above must declare a Google Play services normal permission in their AndroidManifest.xml file in order to get access to the device’s Advertising ID.

Starting V6.8.0, the SDK adds the AD_ID permission automatically.

📘

Lưu ý

  • Nếu ứng dụng của bạn tham gia chương trình Được thiết kế dành cho Gia đình:
    • If using SDK V6.8.0 and above, you should Revoke the AD_ID permission.
    • If using SDK older than V6.8.0, don't add this permission to your app.
  • Đối với các ứng dụng nhắm mục tiêu API cấp 32 (Android 12L) trở lên, không cần quyền này.

Apps that use SDK versions older than V6.8.0 and target Android 13 (API 33) and above must manually include the permission in their AndroidManifest.xml to have access to the Advertising ID:

<uses-permission android:name="com.google.android.gms.permission.AD_ID" />

Thu hồi quyền AD_ID

Theo Chính sách của Google, các ứng dụng nhắm mục tiêu đến trẻ em không được truyền ID Quảng cáo.

When using SDK V6.8.0 and above, children apps targeting Android 13 (API 33) and above must prevent the permission from getting merged into their app by adding a revoke declaration to their Manifest:

<uses-permission android:name="com.google.android.gms.permission.AD_ID"
 tools:node="remove"/>

Để biết thêm thông tin, hãy xem tài liệu Google Play Services.

Quy tắc ProGuard

Tùy chọn
If you are using ProGuard and you encounter a warning regarding our AFKeystoreWrapper class, then add the following code to your proguard-rules.pro file:

Quy tắc AppsFlyer SDK ProGuard

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

Thêm thư viện liên kết giới thiệu cửa hàng

SDK AppsFlyer hỗ trợ một số thư viện liên kết giới thiệu cửa hàng. Sử dụng liên kết giới thiệu cửa hàng cải thiện độ chính xác của phân bổ.

Bạn chỉ cần thêm phần phụ thuộc liên kết giới thiệu, SDK sẽ lo phần còn lại.

Google Play Install Referrer

Add the following dependency to your build.gradle:

dependencies {
    // ...
    implementation "com.android.installreferrer:installreferrer:2.2"
}

Quy tắc ProGuard Người giới thiệu Cài đặt Google Play

-keep public class com.android.installreferrer.** { *; }

Xiaomi GetApps store referrer

V6.9.0
Add the following dependency to your build.gradle:

dependencies {
  // ...
  implementation "com.miui.referrer:homereferrer:1.0.0.6"
}

Quy tắc ProGuard liên kết giới thiệu cửa hàng Xiaomi GetApps

-keep public class com.miui.referrer.** {*;}

📘

Lưu ý

Huawei and Samsung store referrers are supported out-of-the-box starting SDK V6.1.1 and do not require any additional integration.

Các sự cố xác định

Backup rules

If you add android:fullBackupContent="true" inside the tag in the AndroidManifest.xml, you might get the following error:

Manifest merger failed : Attribute [email protected] value=(true)

To fix this error, add tools:replace="android:fullBackupContent" in the <application> tag in the AndroidManifest.xml file.

If you have your own backup rules specified (android:fullBackupContent="@xml/my_rules"), in addition to the instructions above, please merge them with AppsFlyer rules manually by adding the following rule:

<full-backup-content>
    ...//your custom rules
    <exclude domain="sharedpref" path="appsflyer-data"/>
</full-backup-content>

Missing resource files

SDK V5
If you are using Android SDK V5 and above, make sure that in the APK file, in addition to the classes.dex and resources files, you also have a com > appsflyer > internal folder with files a- and b- inside.
Note: Before SDK 5.3.0, file names are a. and b.

Kiểm tra đảm bảo bạn có các tệp tin cần thiết bằng cách mở APK của bạn trong Android Studio:

Nếu thiếu các tệp tin đó, SDK không thể thực hiện các yêu cầu mạng đến máy chủ của chúng tôi và bạn cần liên hệ với CSM hoặc bộ phận hỗ trợ của mình.