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:

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

    注意:Android Studio 專案包含每個模組的頂層 build.gradle 檔案和一個 build.gradle 檔案。請務必編輯應用程式模組的檔案。如要進一步瞭解 Gradle,請參閱「 使用 Gradle 建構專案」。

  2. 確認 google() 包含在列出的 repositories 中。
    repositories {
        google()
    }
    
  3. 在最新版本的 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:21.3.0'
        }
    

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

    注意:如果應用程式中的方法參照數量超出 65K 限制,應用程式可能無法編譯。編譯應用程式時,您可以只指定應用程式使用的特定 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