Home How to integrate Google Play games services
Post
Cancel

How to integrate Google Play games services

What is it?

Google play games is a group of APIs from google to simplify some games basic features like leaderboards, achievements, and online matches.

In this tutorial, I’m going to show you how to integrate google game service with your game, So you can add achievements and make a leaderboard to show the top players.

Let’s start!

First, you need to open your project then after that, navigate to the build.gradle. Now inside dependance, we will add the google play auth and games dependances.

1
2
implementation 'com.google.android.gms:play-services-games:18.0.1'
implementation 'com.google.android.gms:play-services-auth:17.0.0'

After that, we will go to the screen that we will show the leaderboard and achievement screens from it.

I have already designed the screen and added buttons to show the leaderboards and achievements screens.

Inside our activity, we will add a variable googleSignInClient and the type will be GoogleSignInClient.

1
2
3
4
5
6
7
8
9
10
11
class MainActivity : BaseActivity() {


  private var googleSignInClient: GoogleSignInClient? = null

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

  }
}

Next thing we will add a new method initGoogleClientAndSignin, from the name of the function what we will do is we will first make an instance from GoogleSignInClient then we will sign the user in.

1
2
3
4
5
6
7
8
9
10
11
12
fun initGoogleClientAndSignin() {
  googleSignInClient = GoogleSignIn.getClient(this, GoogleSignInOptions.Builder(
    GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build())

  googleSignInClient?.silentSignIn()?.addOnCompleteListener { task ->
    if (task.isSuccessful) {
      
    } else {
     
    }
  }
}

We will call this method from onCreate.

Now in case of success, we will make other two new objects to submit a score and an achievement to google or to open the achievement and leaderboards screens.

1
2
achievementsClient = Games.getAchievementsClient(this, task.result!!)
leaderboardsClient = Games.getLeaderboardsClient(this, task.result!!)

So the final code until this point will be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class MainActivity : BaseActivity() {

  private var googleSignInClient: GoogleSignInClient? = null
  private var achievementClient: AchievementsClient? = null
  private var leaderboardsClient: LeaderboardsClient? = null
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    initGoogleClientAndSignin()
  }
fun initGoogleClientAndSignin() {
    googleSignInClient = GoogleSignIn.getClient(this,
    GoogleSignInOptions.Builder(
    GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build())

    googleSignInClient?.silentSignIn()?.addOnCompleteListener 
      { task ->
        if (task.isSuccessful) {
         achievementClient = Games.getAchievementsClient(this,
         task.result!!)
         leaderboardsClient = Games.getLeaderboardsClient(this,
         task.result!!)
        } else {
         Log.e("Error", "signInError", task.exception)
        }
      }
  }
}

To open the achievements screen we will use the achievementsClient object to get the achievementsIntent, then we will add success listener that will give us an intent to start.

1
2
3
4
5
fun showAchievements(view: View) {
  achievementClient?.achievementsIntent?.addOnSuccessListener { intent ->
    startActivityForResult(intent, 0)
  }
}

And the same thing for the leaderboards.

1
2
3
4
5
fun showTopPlayers(view: View) {
  leaderboardsClient?.allLeaderboardsIntent?.addOnSuccessListener {intent ->
    startActivityForResult(intent, 0)
  }
}

If you run the app nothing will happen because still, we need to do more things to get it works.

Now you need to go to the google play console, Then select game service, Add a new game, enter the name and the category then press continue.

A new game will be created, enter the description of the game, the icon and feature graphics.

Next, you need to go to linked apps, click on android, then enter your package name, then click on save and continue.

Now, click on Authorize your app now button, a dialog will ask you for the SHA1, you can get the SHA1 by executing the following command line.

1
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

This SHA1 is the debug SHA1, when you publish your app on google play you need to get the production SHA1.

Go to publishing, Then click on publish your game.

Back to the android studio, we need to add two new meta-data to our app,

First one will have the app id:

1
2
3
<meta-data
        android:name="com.google.android.gms.games.APP_ID"
        android:value="@string/app_id" />

And the other will have the gms version:

1
2
3
<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Now run the app, click on achievements or leaderboards you will see that a new screen will open, but, you will notice that there are no achievements or leaderboards, don’t worry we will add them now.

We can add new leaderboard by going to the leaderboards then click on add leaderboard, enter the name of the leader board and add an icon then save.

And to add achievements go to achievements and add new achievement, enter the name of the achievement, the description, and the icon then click save.

Now again, you need to go to publishing and click on Publish your game.

Now run the app, click on achievements or leaderboards you will see that a new screen will open with the leaderboards and achievements.

To submit a new point to the leader board, we will use the submitScore method, this method takes two parameters the leaderboard id and the score.

1
leaderboardsClient?.submitScore("leader_board_id", score_here)

To get the leader board id go to google play console then select leader boards and copy the id from there.

So this is how you submit a new score, For unlocking achievements, we will use the unlock method, it takes one parameter the id of the achievement.

1
achievementClient?.unlock("achievement_id")

To get the achievement id go to google play console then select achievements and copy the id from there.

So that’s it, now your users can enjoy these beautiful features!!

-❤️~.
If you have any questions you can send me a message on Twitter or facebook. Also you can check my Github page or my Apps.

This post is licensed under CC BY 4.0 by the author.