Setup for Developing with the Cast Application Framework (CAF) for Android

Prerequisites

The Google Cast SDK for Android is part of the Google Play services SDK and does not need to be downloaded separately.

Note: Google Play services gives you access to a range of APIs for building ads, collecting analytics, authenticating users, integrating maps, and much more. For more information, see Overview of Google Play Services. It is important for you to ensure that the correct Google Play services APK is installed on a user's device since updates might not reach all users immediately.

Add Google Play Services to your project

Select your development environment below and add Google Play services to your project by following the steps provided.

Android Studio

To make the Google Play services APIs available to your app:

  1. Open the build.gradle file inside your application module directory.

    Note: Android Studio projects contain a top-level build.gradle file and a build.gradle file for each module. Be sure to edit the file for your application module. See Building Your Project with Gradle for more information about Gradle.

  2. Verify that google() is included in the listed repositories.
    repositories {
        google()
    }
  3. Add a new build rule under dependencies for the latest version of play-services. For example:
    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'
        }

    Be sure you update this version number each time Google Play services is updated.

    Note: If the number of method references in your app exceeds the 65K limit, your app may fail to compile. You may be able to mitigate this problem when compiling your app by specifying only the specific Google Play services APIs your app uses, instead of all of them. For information on how to do this, see Selectively compiling APIs into your executable.

  4. Save the changes and click Sync Project with Gradle Files in the toolbar.

Other IDE

To make the Google Play services APIs available to your app:

  1. Copy the library project at <android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/ to the location where you maintain your Android app projects.
  2. In your app project, reference the Google Play services library project. See Referencing a Library Project on the Command Line for more information on how to do this.

    Note: You should be referencing a copy of the library that you copied to your development workspace—you should not reference the library directly from the Android SDK directory.

  3. 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 <application> element:
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Once you've set up your project to reference the library project, you can begin developing features with the Google Play services APIs.

Create a Proguard exception

To prevent ProGuard from stripping away required classes, add the following lines in the /proguard-project.txt file:

-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