Android sdk with google play services

Setting Up Google Play Services

In this document

To develop an app using the Google Play services APIs, you need to set up your project with the Google Play services SDK.

If you haven’t installed the Google Play services SDK yet, go get it now by following the guide to Adding SDK Packages.

To test your app when using the Google Play services SDK, you must use either:

  • A compatible Android device that runs Android 2.3 or higher and includes Google Play Store.
  • The Android emulator with an AVD that runs the Google APIs platform based on Android 4.2.2 or higher.

Add Google Play Services to Your Project

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

    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.

Add a new build rule under dependencies for the latest version of play-services . For example:

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.

  • Save the changes and click Sync Project with Gradle Filesin the toolbar.
  • You can now begin developing features with the Google Play services APIs.

    Selectively compiling APIs into your executable

    In versions of Google Play services prior to 6.5, you had to compile the entire package of APIs into your app. In some cases, doing so made it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit.

    From version 6.5, you can instead selectively compile Google Play service APIs into your app. For example, to include only the Google Fit and Android Wear APIs, replace the following line in your build.gradle file:

    with these lines:

    Table 1 shows a list of the separate APIs that you can include when compiling your app, and how to describe them in your build.gradle file. Some APIs do not have a separate library; include them by including the base library. (This lib is automatically included when you include an API that does have a separate library.)

    Table 1. Individual APIs and corresponding build.gradle descriptions.

    Google Play services API Description in build.gradle
    Google+ com.google.android.gms:play-services-plus:7.3.0
    Google Account Login com.google.android.gms:play-services-identity:7.3.0
    Google Actions, Base Client Library com.google.android.gms:play-services-base:7.3.0
    Google App Indexing com.google.android.gms:play-services-appindexing:7.3.0
    Google Analytics com.google.android.gms:play-services-analytics:7.3.0
    Google Cast com.google.android.gms:play-services-cast:7.3.0
    Google Cloud Messaging com.google.android.gms:play-services-gcm:7.3.0
    Google Drive com.google.android.gms:play-services-drive:7.3.0
    Google Fit com.google.android.gms:play-services-fitness:7.3.0
    Google Location, Activity Recognition, and Places com.google.android.gms:play-services-location:7.3.0
    Google Maps com.google.android.gms:play-services-maps:7.3.0
    Google Mobile Ads com.google.android.gms:play-services-ads:7.3.0
    Google Nearby com.google.android.gms:play-services-nearby:7.3.0
    Google Panorama Viewer com.google.android.gms:play-services-panorama:7.3.0
    Google Play Game services com.google.android.gms:play-services-games:7.3.0
    SafetyNet com.google.android.gms:play-services-safetynet:7.3.0
    Google Wallet com.google.android.gms:play-services-wallet:7.3.0
    Android Wear com.google.android.gms:play-services-wearable:7.3.0

    Note: ProGuard directives are included in the Play services client libraries to preserve the required classes. The Android Plugin for Gradle automatically appends ProGuard configuration files in an AAR (Android ARchive) package and appends that package to your ProGuard configuration. During project creation, Android Studio automatically creates the ProGuard configuration files and build.gradle properties for ProGuard use. To use ProGuard with Android Studio, you must enable the ProGuard setting in your build.gradle buildTypes . For more information, see the ProGuard topic.

    Читайте также:  Android 6 форматировать sd как внутренний накопитель

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

    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.

  • 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 element:
  • 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

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

    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.

  • 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 element:
  • 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

    Ensure Devices Have the Google Play services APK

    As described in the Google Play services introduction, Google Play delivers service updates for users on Android 2.3 and higher through the Google Play Store app. However, updates might not reach all users immediately, so your app should verify the version available before attempting to perform API transactions.

    Important: Because it is hard to anticipate the state of each device, you must always check for a compatible Google Play services APK before you access Google Play services features.

    Because each app uses Google Play services differently, it’s up to you decide the appropriate place in your app to verify the Google Play services version. For example, if Google Play services is required for your app at all times, you might want to do it when your app first launches. On the other hand, if Google Play services is an optional part of your app, you can check the version only once the user navigates to that portion of your app.

    You are strongly encouraged to use the GoogleApiClient class to access Google Play services features. This approach allows you to attach an OnConnectionFailedListener object to your client. To detect if the device has the appropriate version of the Google Play services APK, implement the onConnectionFailed() callback method. If the connection fails due to a missing or out-of-date version of the Google Play APK, the callback receives an error code such as SERVICE_MISSING , SERVICE_VERSION_UPDATE_REQUIRED , or SERVICE_DISABLED . To learn more about how to build your client and handle such connection errors, see Accessing Google APIs.

    Another approach is to use the isGooglePlayServicesAvailable() method. You might call this method in the onResume() method of the main activity. If the result code is SUCCESS , then the Google Play services APK is up-to-date and you can continue to make a connection. If, however, the result code is SERVICE_MISSING , SERVICE_VERSION_UPDATE_REQUIRED , or SERVICE_DISABLED , then the user needs to install an update. In this case, call the getErrorDialog() method and pass it the result error code. The method returns a Dialog you should show, which provides an appropriate message about the error and provides an action that takes the user to Google Play Store to install the update.

    To then begin a connection to Google Play services (required by most Google APIs such as Google Drive, Google+, and Games), read Accessing Google APIs.

    Источник

    Get Started with Play Games Services for Android

    Welcome to Android game development with the Google Play games services!

    The Play Games SDK provides cross-platform Google Play games services that lets you easily integrate popular gaming features such as achievements, leaderboards, and Saved Games in your tablet and mobile games.

    Читайте также:  Что такое android 8 0 oreo

    This training will guide you to install a sample game application for Android and quickly get started to create your own Android game. The Type-a-Number Challenge sample app demonstrates how you can integrate achievements and leaderboards into your game.

    Before you begin

    To prepare your app, complete the steps in the following sections.

    App prerequisites

    Make sure that your app’s build file uses the following values:

    • A minSdkVersion of 19 of higher
    • A compileSdkVersion of 28 or higher

    Set up your game in Google Play Console

    The Google Play Console is where you manage Google Play games services for your game, and configure metadata for authorizing and authenticating your game. For more information, see Setting Up Google Play Games Services.

    Configure your app

    In your project-level build.gradle file, include Google’s Maven repository and Maven central repository in both your buildscript and allprojects sections:

    Add the Google Play services dependency for the Play Games SDK to your module’s Gradle build file, which is commonly app/build.gradle :

    Sample App Guide

    This training will guide you to install a sample game app for Android and quickly get started to create your own Android game. The Type-a-Number Challenge sample app demonstrates how you can integrate achievements and leaderboards into your game.

    Step 1: Download the sample app

    For this developer guide, you will need to download the Type-a-Number Challenge sample Android application.

    To download and set up the sample application in Android Studio:

    1. Download the Android samples from the samples download page.
    2. Import the android-basic-samples project. This project includes TypeANumber and other Android game samples. To do this in Android Studio:
      1. Click File >Import Project.
      2. Browse to the directory where you downloaded android-basic-samples on your development machine. Select the file android-basic-samples/build.gradle and click OK.
    3. In the TypeANumber module, open AndroidManifest.xml and change the package name from com.google.example.games.tanc to a different package name of your own. The new package name must not start with com.google , com.example , or com.android .

    Step 2: Set up the game in the Google Play Console

    The Google Play Console is where you manage Google Play games services for your game, and configure metadata for authorizing and authenticating your game.

    To set up the sample game in the Google Play Console:

    1. Point your web browser to the Google Play Console, and sign in. If you haven’t registered for the Google Play Console before, you will be prompted to do so.
    2. Follow these instructions to add your game to the Google Play Console.
      1. When asked if you use Google APIs in your app, select I don’t use any Google APIs in my game yet.
      2. For the purpose of this developer guide, you can fill up the form with your own game details. For convenience, you can use the placeholder icons and screenshots provided in the Downloads page.
    3. Follow these instructions to generate an OAuth 2.0 client ID for your Android app.
      1. When linking your Android app, make sure to specify the exact package name you used previously when renaming the sample package.
      2. You can use the Generate Signed APK Wizard in Android Studio to generate a new keystore and signed certificate if you don’t have one already. To learn how to run the Generate Signed APK Wizard, see Signing Your App in Android Studio.
    4. Make sure to record the following information for later:
      1. Your application ID: This is a string consisting only of digits (typically 12 or more), at the beginning of your client ID.
      2. Your signing certificate: Note which certificate you used when setting up your API access (the certificate whose SHA1 fingerprint you provided). You should use the same certificate to sign your app when testing or releasing your app.
    5. Configure achievements for Type-a-Number Challenge:
      1. Select the Achievements tab in the Google Play Console.
      2. Add the following sample achievements:
        Name Description Special Instructions
        Prime Get a score that’s a prime number. None
        Humble Request a score of 0. None
        Don’t get cocky, kid Request a score of 9999 in either mode. None
        OMG U R TEH UBER LEET! Receive a score of 1337. Make this a hidden achievement.
        Bored Play the game 10 times. Make this an an incremental achievement with 10 steps to unlock.
        Really Really Bored Play the game 100 times. Make this an an incremental achievement with 100 steps to unlock.
      3. Record the IDs (long alphanumeric strings) for each achievement that you created.
      4. Configure achievements that are appropriate for your game. To learn more, see the concepts behind achievements and how to implement achievements in Android.
    6. Configure the leaderboards for Type-a-Number Challenge:
      1. Select the Leaderboards tab in the Google Play Console.
      2. Add two sample leaderboards: one named “Easy High Scores” and another named “Hard High Scores”. Both leaderboards should use Integer score formatting with 0 decimal places, and an ordering type of Larger is better.
      3. Record the IDs (long alphanumeric strings) for each leaderboard you created.
      4. Configure leaderboards that are appropriate for your game. To learn more, see the concepts behind leaderboards and how to implement leaderboards in Android.
    7. Add test accounts for your game. This step is needed only for apps that have not yet been published in the Google Play Console. Before the app is published, only the test accounts listed in the Google Play Console can log in. However, once an application is published, everyone is allowed to log in.
    Читайте также:  Teamspeak 3 для андроида

    Warning: If you try to make Play Games SDK calls for an unpublished game by using an account that’s not listed as a test account, the Google Play games services will behave as if the game did not exist and you’ll get back the ConnectionResult.SIGN_IN_REQUIRED return code. If you attempt to launch ConnectionResult.startResolutionForResult() , you’ll get back GamesActivityResultCodes.RESULT_SIGN_IN_FAILED .

    Step 3: Modify your code

    To run the game, you need to configure the application ID as a resource in your Android project. You will also need to add games metadata in the AndroidManifest.xml .

    1. Open res/values/ids.xml and replace the placeholder IDs. If you are creating an Android game from scratch, you will need to create this file first.
      1. Specify your application ID in the app_id resource.
      2. Specify each achievement ID that you created earlier in the corresponding achievement_* resource.
      3. Specify each leaderboard ID that you created earlier in the corresponding leaderboard_* resource.
    2. Open AndroidManifest.xml and enter your package name in the package attribute of the manifest > element. If you are creating an Android game from scratch, make sure that you also add the following code inside the application > element:

    Step 4: Test your game

    To ensure that Google Play games services are functioning correctly in your game, test the application before you publish it on Google Play.

    To run your game on your physical test device:

    1. Verify that you have set up the test account that you are using to log in to the app (as described in Step 2).
    2. Export an APK and sign it with the same certificate that you used to set up the project in Google Play Console. To export a signed APK in Android Studio, click Build >Generate Signed APK.
    3. Install the signed APK on your physical test device by using the adb tool. To learn how to install an application, see Running on a Device.

    Warning: When you run the application directly from Android Studio, Android Studio will sign the application with your debug certificate by default. If you did not use this debug certificate when setting up the application in Google Play Console, this will cause errors. Make sure to run an APK that you exported and signed with a certificate that matches one of the certificates you used during the application setup in Google Play Console.

    Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

    Источник

    Оцените статью