Gaming technologies are constantly evolving. Nevertheless, a lot of core gameplay elements have remained unchanged for decades. High scores, leaderboards, quests, achievements, and multiplayer support are examples. If you are developing a game for the Android platform, you don’t have to implement any of those elements manually. You can simply use the Google Play games services APIs instead.

In this tutorial, I’ll show you how to create a simple Android game, add Google Play games services to it, and use the leaderboards and achievements APIs.

Prerequisites

1. Create a Simple Game

There’s no fun in adding the Play games services APIs to a blank Android Studio project. Therefore, let us now create a game where the user gains points by simply clicking on a button. More precisely, the objective of the game will be to click on a button as often as possible within 60 seconds.

Step 1: Create the Game Layout

The layout of the game will have one Button widget, which the user will click to gain points, and two TextView widgets to display the score and the time left. If you put all of them inside a RelativeLayout and center them both horizontally and vertically, you should have a layout file that looks like this:

We can use the playing variable to differentiate between the first click and all the subsequent clicks. To keep track of the time, we can use the abstract CountDownTimer class, which is ideal for our requirements. It has an onTick() method inside, which can accurately update timeView to display the number of seconds remaining. It also has an onFinish() method, which is called when the countdown is over.

2. Add Games Services Dependencies

Configuring a project to use the Play games services APIs involves a lot of steps. By using Android Studio’s Firebase Assistant, you can automate some of them.

Open the assistant window by going to Tools > Firebase. Next, in the Analytics section, click on the Log an Analytics event link. You can now connect your Android Studio project to a Firebase project by clicking on the Connectto Firebase button. Make sure that you choose the Create new Firebase project option in the dialog that pops up.

Once the connection has been established, press the Add Analytics to your app button to add all the required changes to the build.gradle files.

You must add the dependency for Play games services manually. Therefore, go to the build.gradle file of the app module and add the following compile dependency:

compile 'com.google.android.gms:play-services-games:9.8.0'

Furthermore, the games services APIs need an XML configuration file. Create one called games-ids.xml inside the res/values folder. We’ll add content to it in a later step.

3. Register the Game

All games that use Google Play games services must be registered on the Play developer console. You’ll have to pay a one-time registration fee of $25 to be able to access the console.

In the console, click on the gamepad icon to open the Game Services screen.

Next, click on the Set up Google Play game services button to start registering the game.

In the dialog that pops up, choose the second tab because our game is already using Google APIs. You must now be able to see your Firebase project’s name in the list of available projects. After selecting it, choose a category for the game. For now, you can go with the Casual category.

After you press Continue, you can link Firebase Analytics and Play games services by clicking on the Link Firebase button.

Next, go to the Linked apps section to link your Android Studio project to the Play developer console. In the dialog that pops up, press the Android button and type in your project’s package name in the Package name field.

Press the Save and continue button to generate a client ID for your game.

Our game has now been successfully registered with the Google Play developer console.

4. Add a Test User

The Play games services APIs will work only if your game is published on Google Play. However, to allow you to test your game, the developer console lets you associate a few test user accounts. By going to the Testing section, you can add or remove test user accounts. Make sure that you add the Google account you use on your phone or emulator here.

5. Create a Leaderboard

A leaderboard is nothing but a screen that displays the users’ high scores. Play games services leaderboards allow users to see their daily, weekly and all-time high scores.

Creating a leaderboard on the Play developer console takes just a few clicks. Go to the Leaderboards section and press the Add leaderboard button. In the next screen, give a meaningful name to the leaderboard and press the Save button.

Our leaderboard is now ready.

6. Create an Achievement

Achievements are in-game awards users get for managing to do something special. A game that has a lot of achievements to unlock is usually more fun than one that doesn’t. Consequently, most of the popular games on Google Play today have dozens, if not hundreds of achievements.

In this tutorial, we’ll add just one achievement to our game. Its name will be Lightning Fast, and it will be unlocked when the user manages to tap the button more than 100 times in a minute. To create the achievement, go to the Achievements section and press the Add achievement button. After you type in the name and the description of the achievement, press the Save button.

7. Update the Game Configuration XML

Both the leaderboard and the achievement have unique identifiers. We must now add those identifiers to our Android Studio project, along with the application ID that was generated during the registration. You can do so by manually updating the games-ids.xml file we created earlier. However, I suggest that you use the automatically generated configuration code available in the developer console.

To get the auto-generated configuration code, you can go to either the Achievements section or the Leaderboards section, and press the Get resources link. You will see XML code that looks like this:

8. Connect to Play Games Services

Before using the leaderboards and achievements APIs, we must create a GoogleApiClient instance and connect it to Play games services. Therefore, add a GoogleApiClient object as a member variable of your activity.

We must use the GoogleApiClient.Builder class to build the GoogleApiClient instance. While building the client, we can specify the API and API scope we are interested in by using the addApi() and addScope() methods.

Additionally, I suggest that you call the enableAutoManage() method to make sure that the client automatically manages the connection to the games services. The method, however, needs an OnConnectionFailedListener, which will be called when the connection fails. For now, we’ll simply call the finish() method to close the app in case of a connection failure.

Accordingly, add the following code at the beginning of the onCreate() method:

If you run the app now, you will be prompted to create a Gamer ID for yourself. Type in a Gamer ID of your choice and press the Sign in button.

9. Use the Leaderboard

Submitting a score to the leaderboard takes just one line of code. All you need to do is call the submitScore() method of the Games.Leaderboards class. As its arguments, it expects the GoogleApiClient instance, the ID of the leaderboard, and the score.

In our game, we must submit the score at the end of the 60 seconds. Therefore, add the following code to the onFinish() method:

To view the leaderboard, we must start a new activity with a leaderboard intent. To fetch the leaderboard intent, call the getLeaderboardIntent() method of the Games.Leaderboards class and pass the GoogleApiClient instance and leaderboard ID to it.

Our game’s layout already has a button that can be pressed to open the leaderboard. The value of its onClick attribute is showLeaderboard. Therefore, add the following code to your activity:

The code needed to display the achievements screens is very similar to the one we wrote to display the leaderboard screen. All you need to do is pass the return value of the getAchievementsIntent() method to the startActivityForResult() method.

If you run your app and play the game again, you will see an achievement pop up when your score crosses 100 points for the first time.

Conclusion

You now know how to use the Google Play games services APIs in your Android Studio project. Play games services are not limited to the Android platform alone. They can be used just as easily on both the web and iOS platforms too. So Play games services allow you to create cross-platform games and offer consistent gaming experiences to users on multiple platforms.

To learn more about the Google Play games services APIs, you can refer to their official guides or, even better, check out some of our tutorials here on Envato Tuts+!