使用 Android 適用的 Cast 應用程式架構 (CAF) 進行開發的設定
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
必要條件
Google Cast SDK for Android 是 Google Play 服務 SDK 的一部分,因此不需要另外下載。
注意:Google Play 服務可讓您存取各種 API,用於建立廣告、收集分析資料、驗證使用者、整合地圖等。詳情請參閱「Google Play 服務總覽」。您必須確保使用者的裝置已安裝正確的 Google Play 服務 APK,因為更新可能不會立即傳送給所有使用者。
將 Google Play 服務新增至專案
請在下方選取開發環境,然後按照提供的步驟將 Google Play 服務新增至專案。
Android Studio
如要讓應用程式使用 Google Play 服務 API,請按照下列步驟操作:
- 開啟應用程式模組目錄中的
build.gradle
檔案。注意:Android Studio 專案包含頂層 build.gradle
檔案,以及每個模組的 build.gradle
檔案。請務必編輯應用程式模組的檔案。如要進一步瞭解 Gradle,請參閱「
使用 Gradle 建構專案」。
- 確認
google()
已納入 repositories
清單。repositories {
google()
}
- 針對最新版的
play-services
,在 dependencies
下方新增建構規則。例如: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'
}
請務必在每次更新 Google Play 服務時更新這個版本號碼。
注意:如果應用程式中的方法參照數量超過 65K 上限,應用程式可能無法編譯。您可以在編譯應用程式時,只指定應用程式使用的特定 Google Play 服務 API,而非所有 API,藉此減輕這項問題。如要瞭解如何執行此操作,請參閱「選擇性地將 API 編譯至可執行檔」。
- 儲存變更內容,然後在工具列中按一下「Sync Project with Gradle Files」。
其他 IDE
如要讓應用程式使用 Google Play 服務 API,請按照下列步驟操作:
- 將
<android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/
中的程式庫專案複製到您維護 Android 應用程式專案的位置。
- 在應用程式專案中參照 Google Play 服務程式庫專案。如要進一步瞭解如何執行這項操作,請參閱「
在指令列中參照程式庫專案」。
注意:您應參照複製到開發工作區的程式庫副本,請勿直接從 Android SDK 目錄參照程式庫。
- 將 Google Play 服務程式庫新增為應用程式專案的依附元件後,請開啟應用程式的資訊清單檔案,並將下列標記新增為
<application>
元素的子項:<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
設定專案以參照程式庫專案後,您就可以開始使用
Google Play 服務 API 開發功能。
建立 Proguard 例外狀況
為避免 ProGuard 移除必要的類別,請在 /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
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\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```"]]