Thiết lập để phát triển bằng Khung ứng dụng truyền (CAF) dành cho Android
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Điều kiện tiên quyết
SDK Google Cast dành cho Android là một phần của SDK Dịch vụ Google Play và bạn không cần tải xuống riêng.
Lưu ý: Dịch vụ Google Play cung cấp cho bạn quyền truy cập vào một loạt API để tạo quảng cáo, thu thập số liệu phân tích, xác thực người dùng, tích hợp bản đồ và làm nhiều việc khác. Để biết thêm thông tin, hãy xem bài viết Tổng quan về Dịch vụ Google Play.
Bạn cần đảm bảo rằng tệp APK Dịch vụ Google Play đã được cài đặt đúng cách trên thiết bị của người dùng vì bản cập nhật có thể không được phân phối ngay cho tất cả người dùng.
Thêm Dịch vụ Google Play vào dự án
Chọn môi trường phát triển của bạn ở bên dưới rồi thêm Dịch vụ Google Play vào dự án bằng cách làm theo các bước được cung cấp.
Android Studio
Cách cung cấp API Dịch vụ Google Play cho ứng dụng:
- Mở tệp
build.gradle
trong thư mục mô-đun ứng dụng.
Lưu ý: Các dự án Android Studio chứa một tệp build.gradle
cấp cao nhất và một tệp build.gradle
cho mỗi mô-đun. Hãy nhớ chỉnh sửa tệp cho mô-đun ứng dụng. Hãy xem phần
Xây dựng dự án bằng Gradle để biết thêm thông tin về Gradle.
- Xác minh rằng
google()
có trong repositories
được liệt kê.
repositories {
google()
}
- Thêm quy tắc bản dựng mới trong
dependencies
cho phiên bản mới nhất của play-services
. Ví dụ:
apply plugin: 'com.android.application'
...
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.mediarouter:mediarouter:1.2.5'
implementation 'com.google.android.gms:play-services-cast-framework:22.1.0'
}
Hãy nhớ cập nhật số phiên bản này mỗi khi Dịch vụ Google Play được cập nhật.
Lưu ý: Nếu số lượng tệp tham chiếu phương thức trong ứng dụng của bạn vượt quá giới hạn 65 nghìn, thì ứng dụng của bạn có thể không biên dịch được. Bạn có thể giảm thiểu vấn đề này khi biên dịch ứng dụng bằng cách chỉ chỉ định các API Dịch vụ Google Play cụ thể mà ứng dụng của bạn sử dụng, thay vì tất cả các API. Để biết thông tin về cách thực hiện việc này, hãy xem phần Biên dịch có chọn lọc các API vào tệp thực thi.
- Lưu các thay đổi rồi nhấp vào Sync Project with Gradle Files (Đồng bộ hoá dự án với tệp Gradle) trên thanh công cụ.
IDE khác
Cách cung cấp API Dịch vụ Google Play cho ứng dụng:
- Sao chép dự án thư viện tại
<android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/
sang vị trí mà bạn duy trì các dự án ứng dụng Android.
- Trong dự án ứng dụng, hãy tham chiếu dự án thư viện Dịch vụ Google Play. Hãy xem phần
Tham chiếu dự án thư viện trên dòng lệnh để biết thêm thông tin về cách thực hiện việc này.
Lưu ý: Bạn nên tham chiếu bản sao của thư viện mà bạn đã sao chép vào không gian làm việc phát triển của mình. Bạn không nên tham chiếu trực tiếp thư viện từ thư mục SDK Android.
- Sau khi thêm thư viện Dịch vụ Google Play làm phần phụ thuộc cho dự án ứng dụng, hãy mở tệp kê khai của ứng dụng và thêm thẻ sau làm phần tử con của phần tử
<application>
:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Sau khi thiết lập dự án để tham chiếu dự án thư viện, bạn có thể bắt đầu phát triển các tính năng bằng
API Dịch vụ Google Play.
Tạo trường hợp ngoại lệ Proguard
Để ngăn ProGuard xoá các lớp bắt buộc, hãy thêm các dòng sau vào tệp /proguard-project.txt
:
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}
-keepnames class * implements android.os.Parcelable
-keepclassmembers class * implements android.os.Parcelable {
public static final *** CREATOR;
}
-keep @interface android.support.annotation.Keep
-keep @android.support.annotation.Keep class *
-keepclasseswithmembers class * {
@android.support.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <methods>;
}
-keep @interface com.google.android.gms.common.annotation.KeepName
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@com.google.android.gms.common.annotation.KeepName *;
}
-keep @interface com.google.android.gms.common.util.DynamiteApi
-keep public @com.google.android.gms.common.util.DynamiteApi class * {
public <fields>;
public <methods>;
}
-dontwarn android.security.NetworkSecurityPolicy
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eEnsure you have the latest Android SDK, appcompat, mediarouter AndroidX Libraries, and Google Cast SDK installed via the Android SDK Manager.\u003c/p\u003e\n"],["\u003cp\u003eAdd Google Play Services to your project by modifying your \u003ccode\u003ebuild.gradle\u003c/code\u003e file (Android Studio) or referencing the library project and updating the manifest (Other IDEs).\u003c/p\u003e\n"],["\u003cp\u003eIf using ProGuard, include specific exceptions in your configuration file to prevent the removal of necessary Google Play services classes.\u003c/p\u003e\n"]]],[],null,["# Setup for Developing with the Cast Application Framework (CAF) for Android\n\nPrerequisites\n-------------\n\n- Install the latest version of the Android SDK using the [Android SDK\n Manager](//developer.android.com/tools/help/sdk-manager.html).\n- Install the latest version of the appcompat and mediarouter [AndroidX\n Libraries](//developer.android.com/jetpack/androidx) through the [Android SDK\n Manager](//developer.android.com/tools/help/sdk-manager.html).\n- Install the latest [Cast SDK in Google Play\n services](//developer.android.com/google/play-services/setup.html) through the [Android SDK\n Manager](//developer.android.com/tools/help/sdk-manager.html).\n\nThe Google Cast SDK for Android is part of the Google Play services SDK and does\nnot need to be downloaded separately.\n\n**Note** : Google Play services gives you access to a [range of\nAPIs](/android) for building ads, collecting analytics, authenticating users,\nintegrating maps, and much more. For more information, see [Overview of Google Play\nServices](/android/guides/overview).\nIt is important for you to [ensure that the correct Google Play services APK is\ninstalled](/android/guides/setup#ensure_devices_have_the_google_play_services_apk)\non a user's device since updates might not reach all users immediately.\n| **Note:** Since the libraries contribute resources, you cannot simply satisfy the dependencies by including their JAR files; instead you need to [import them as\n| library\n| projects](//developer.android.com/tools/support-library/setup.html#libs-with-res) for your IDE.\n\nAdd Google Play Services to your project\n----------------------------------------\n\n\nSelect your development environment below and add Google Play services to your project by\nfollowing the steps provided. \n\n### Android Studio\n\nTo make the Google Play services APIs available to your app:\n\n1. Open the `build.gradle` file inside your application module directory.\n\n **Note:** Android Studio projects contain a top-level\n `build.gradle` file and a `build.gradle` file for each module. Be sure to\n edit the file for your application module. See\n [Building Your Project with Gradle](//developer.android.com/sdk/installing/studio-build.html) for more information about Gradle.\n2. Verify that `google()` is included in the listed `repositories`. \n\n repositories {\n google()\n }\n\n3. Add a new build rule under `dependencies` for the latest version of `play-services`. For example: \n\n ```carbon\n apply plugin: 'com.android.application'\n ...\n\n dependencies {\n implementation 'androidx.appcompat:appcompat:1.3.1'\n implementation 'androidx.mediarouter:mediarouter:1.2.5'\n implementation 'com.google.android.gms:play-services-cast-framework:22.1.0'\n }\n ```\n\n Be sure you update this version number each time Google Play services is updated.\n\n **Note:** If the number of method references in your app exceeds the\n [65K limit](//developer.android.com/tools/building/multidex.html), your app may fail to\n compile. You may be able to mitigate this problem when compiling your app by specifying only\n the specific Google Play services APIs your app uses, instead of all of them. For information\n on how to do this, see\n [Selectively compiling APIs into your executable](/android/guides/setup#split).\n4. Save the changes and click **Sync Project with Gradle Files** in the toolbar.\n\n### Other IDE\n\nTo make the Google Play services APIs available to your app:\n\n1. Copy the library project at `\u003candroid-sdk\u003e/extras/google/google_play_services/libproject/google-play-services_lib/` to the location where you maintain your Android app projects.\n2. In your app project, reference the Google Play services library project. See [Referencing a Library Project on the Command Line](//developer.android.com/tools/projects/projects-cmdline.html#ReferencingLibraryProject) for more information on how to do this.\n\n **Note:** You should be referencing a copy of the library that you\n copied to your development workspace---you should not reference the library directly from the\n Android SDK directory.\n3. After you've added the Google Play services library as a dependency for your app project, open your app's manifest file and add the following tag as a child of the [`\u003capplication\u003e`](//developer.android.com/guide/topics/manifest/application-element.html) element: \n\n ```transact-sql\n \u003cmeta-data android:name=\"com.google.android.gms.version\"\n android:value=\"@integer/google_play_services_version\" /\u003e\n ```\n\nOnce you've set up your project to reference the library project, you can begin developing\nfeatures with the\n[Google Play services APIs](/android/reference/com/google/android/gms/package-summary).\n\n#### Create a Proguard exception\n\nTo prevent\n[ProGuard](//developer.android.com/tools/help/proguard.html) from stripping away\nrequired classes, add the following lines in the\n/proguard-project.txt file: \n\n```carbon\n-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {\n public static final *** NULL;\n}\n\n-keepnames class * implements android.os.Parcelable\n-keepclassmembers class * implements android.os.Parcelable {\n public static final *** CREATOR;\n}\n\n-keep @interface android.support.annotation.Keep\n-keep @android.support.annotation.Keep class *\n-keepclasseswithmembers class * {\n @android.support.annotation.Keep \u003cfields\u003e;\n}\n-keepclasseswithmembers class * {\n @android.support.annotation.Keep \u003cmethods\u003e;\n}\n\n-keep @interface com.google.android.gms.common.annotation.KeepName\n-keepnames @com.google.android.gms.common.annotation.KeepName class *\n-keepclassmembernames class * {\n @com.google.android.gms.common.annotation.KeepName *;\n}\n\n-keep @interface com.google.android.gms.common.util.DynamiteApi\n-keep public @com.google.android.gms.common.util.DynamiteApi class * {\n public \u003cfields\u003e;\n public \u003cmethods\u003e;\n}\n\n-dontwarn android.security.NetworkSecurityPolicy\n```"]]