Tutorial: Achievements and Leaderboards with Google Play Services

I took today off to play Grand Theft Auto 5 and relax. After a few hours of play, and a few more hours of relaxing, I realized that I hadn’t written an article about Android in a while. I also haven’t updated The Grind in a while, which is a shame because it’s one of my favorite casual games and also one of my more popular ones.

So, I decided to update The Grind with Achievements and Leaderboards using the new Google Play Services framework. As with all of my apps and games, this is fully open sourced on GitHub (BSD licensed) so you can use the code, icons, graphics, and ideas however you wish.

Before you do anything, you’ll need to have a Google Developer account, and a working knowledge of Android/Java programming. Feel free to go through the ~70 android apps I’ve open sourced on GitHub to brush up on things.


Follow the Google Player Services setup guide, and make sure your project can be built with the play services ‘library project’ referenced.

Now, clone the ‘basegameutils’ library for an easier time implementing your auth flow.

Setting Up Your Game Details On Google Play

Go through the text-heavy Google Play Developer Console “Game Services” setup and enter all your app information.


You can import most of your information from an existing listing, if you have one.


When you’re finished, you’ll also need to grab your generated app ID from the developer console or API console and append it to your metadata app tag in AndroidManifest.xml


Choosing your Achievements


Always make sure you throw in at least one Spaceballs reference.


Creating your Icons
One of the great things about Wikipedia Commons is the vast troves of images which are available for unlicensed use. If you’re not a great artist and you wish to create an armor icon, why not base it off a public domain image of a real piece of 15th century armor?



Creating a Leaderboard – Google Play Authentication

For any of this to work, you’re going to need to link it to your Google Play account. That means you’ll need to implement sign-in related functions in your app.

Derive from ‘BaseGameActivity’, then add the following methods:

    public void onSignInFailed() {```


 public void onSignInSucceeded() {


Depending on your game design, you may or may not need to actually implement anything in the above methods.

**Hooking Up Your Leaderboard In Your Game**  
 The developer documents are always a good reference, and the Leaderboard docs (though sparse) should still be your first stop when looking to code up a feature. Google has made it very easy to implement this functionality. Your leaderboard is essentially just a large integer table. Simply update your score in the same function you save your game state or in a menu option with the following snippet.

</br> getGameClient().submitScore(leaderBoard, player1.experience);


Displaying Your Leaderboard In Your Game
Google has also made is very easy to display your leaderboard and global rankings as follows.

startActivityForResult(getGamesClient().getLeaderboardIntent(LEADERBOARD_ID), JUST_AN_INTEGER_VALUE);<br></br>```


**Hooking Up Your Achievements In Your Game**  
 Hooking up achievements is as simple as implementing a function to send achievements to the server. Easy as pie.

</br> public void getAnAchievement(GamesClient gc, String achievementId) {
</br> gc.unlockAchievement(achievementId);
</br> }

From here, you’ll likely want to save all these achievement IDs as strings in your resource file so they can be referenced and adjusted in an easier fashion.


Displaying Your Achievements In Your Game
Finally, Google has once again made it easy to display your achievements as follows:

<br></br> startActivityForResult(getGamesClient().getAchievementsIntent(), JUST_AN_INTEGER_VALUE);<br></br>


And that’s that. You’ve just brought your Game into the age of achievements.