使用 Android 適用的 Cast 應用程式架構 (CAF) 進行開發的設定

必要條件

Android 版 Google Cast SDK 是 Google Play 服務 SDK 的一部分 不必另外下載

注意:Google Play 服務提供一系列 API:用於建立廣告、收集數據分析、驗證使用者 整合地圖等更多功能詳情請參閱 Google Play 總覽 服務。 請務必確保使用正確的 Google Play 服務 APK: 已安裝 使用者的裝置不會立即收到更新。

將 Google Play 服務新增至專案

請在下方選取您的開發環境,然後透過下列方式將 Google Play 服務加入專案中: 按照我們提供的步驟進行

Android Studio

如要讓應用程式使用 Google Play 服務 API,請按照下列步驟操作:

  1. 在應用程式模組目錄中開啟 build.gradle 檔案。

    注意:Android Studio 專案包含頂層 build.gradle 檔案和每個模組都有一個 build.gradle 檔案。請務必 編輯應用程式模組的檔案。詳情請見 使用 Gradle 建構專案,進一步瞭解 Gradle。

  2. 確認 google() 已包含在列出的 repositories 中。
    repositories {
        google()
    }
    
  3. dependencies 下新增適用於最新版本的建構規則 play-services。例如:
    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:21.5.0'
        }
    

    每次 Google Play 服務更新時,請務必更新這組版本號碼。

    注意:如果應用程式中的方法參照數量超過 限制為 65, 000,則應用程式可能無法 下一步是使用 compile 方法 指定訓練方式,完成模型編譯您只要在編譯應用程式時指定 應用程式所用的特定 Google Play 服務 API,而非所有 API。資訊 操作方法,請參閱 選擇性將 API 編譯到執行檔中

  4. 儲存變更,然後按一下工具列中的「Sync Project with Gradle Files」

其他 IDE

如要讓應用程式使用 Google Play 服務 API,請按照下列步驟操作:

  1. 從以下位置複製程式庫專案: <android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/ 放置於維護 Android 應用程式專案的位置
  2. 在應用程式專案中,參照 Google Play 服務程式庫專案。詳情請見 在指令列中參照程式庫專案進一步瞭解如何進行。

    注意:請參照您的程式庫副本 複製到開發工作區中,請勿直接從 Android SDK 目錄。

  3. 將 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