使用 Cast 应用框架 (CAF) 进行 Android 开发的相关设置

前提条件

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 构建您的项目

  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 服务时,请务必更新此版本号。

    注意:如果应用中的方法引用数量超出 65K 上限,那么您的应用可能无法 编译。您可以通过在编译应用时指定 您的应用使用的具体 Google Play Services 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 Services 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