Welcome to Android game development with the Play Game services!
The Play Game services SDK provides cross-platform Game services that lets you easily integrate popular gaming features such as achievements, leaderboards, Saved Games, and real-time multiplayer (on Android) in your tablet and mobile games.
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
- Set up your Android development environment. If you are new to developing Android applications, see Building Your First App.
- Have an Android device running Android 2.3 (Gingerbread) or higher on-hand for testing.
Step 1: Download the sample app
For this tutorial, you will need to download the Type-a-Number Challenge sample Android application.
To download and set up the sample application in Android Studio:
- Download the Android samples by following the link in the Downloads page, then extract the sample files to your development machine.
- Import the
BasicSamplesproject. This project includes
TypeANumberand other Android game samples. To do this in Android Studio:
- Click File > Import Project.
- Browse to the directory where you downloaded
android-basic-sampleson your development machine. Select the file
android-basic-samples/BasicSamples/build.gradleand click OK.
- In the
AndroidManifest.xmland change the package name from
com.google.example.games.tancto a different package name of your own. The new package name must not start with
Step 2: Set up the game in the Google Play Developer Console
The Google Play Google Play Developer Console is where you manage Game services for your game, and configure metadata for authorizing and authenticating your game.
To set up the sample game in the Google Play Developer Console:
- Point your web browser to the Google Play Developer Console, and sign in. If you haven't registered for the Google Play Developer Console before, you will be prompted to do so.
- Follow these instruction to add your game to the Google Play Developer Console.
- When asked if you use Google APIs in your app, select I don’t use any Google APIs in my game yet.
- For the purpose of this tutorial, 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.
- Follow these instructions to generate an OAuth 2.0 client ID for your Android app.
- When linking your Android app, make sure to specify the exact package name you used previously when renaming sample package.
- 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.
- Make sure to record the following information for later:
- Your application ID: This is a string consisting only of digits (typically 12 or more), at the beginning of your client ID.
- 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.
- Configure achievements for Type-a-Number Challenge:
- Select the Achievements tab in the Google Play Developer Console.
- 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.
- Record the IDs (long alphanumeric strings) for each achievement that you created.
- Configure achievements that are appropriate for your game. To learn more, see the concepts behind achievements and how to implement achievements in Android.
- Configure the leaderboards for Type-a-Number Challenge:
- Select the the Leaderboards tab in the Google Play Developer Console.
- 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.
- Record the IDs (long alphanumeric strings) for each leaderboard you created.
- Configure leaderboards that are appropriate for your game. To learn more, see the concepts behind leaderboards and how to implement leaderboards in Android.
- Add test accounts for your game. This step is needed only for apps that have not yet been published in the Google Play Developer Console. Before the app is published, only the test accounts listed in the Google Play Developer Console can log in. However, once an application is published, everyone is allowed to log in.
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
res/values/ids.xmland replace the placeholder IDs. If you are creating an Android game from scratch, you will need to create this file first.
- Specify your application ID in the
- Specify each achievement ID that you created earlier in the corresponding
- Specify each leaderboard ID that you created earlier in the corresponding
- Specify your application ID in the
AndroidManifest.xmland enter your package name in the
packageattribute of the <
manifest> element. If you are creating an Android game from scratch, make sure that you also add the following code inside the <
<meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
Step 4: Test your game
To ensure that Game 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:
- Verify that you have set up the test account that you are using to log in to the app (as described in Step 2).
- Export an APK and sign it with the same certificate that you used to set up the project in Google Play Developer Console. To export a signed APK in Android Studio, click Build > Generate Signed APK.
- Install the signed APK on your physical test device by using the
adbtool. To learn how to install an application, see Running on a Device.
Learn more about using Game services with Android:
- Accessing the Games APIs
- Implementing Sign-In
- Adding achievements to your game
- Building your own leaderboards
- Saving game data in the cloud
- Understanding how anti-piracy works
- Developing real-time multiplayer games
- Developing turn-based multiplayer games
- Adding events and quests to games
- Enabling verbose logging