Android Add Game Leaderboard

In order to display leaderboards from your game you will have to set up your project and to be signed in. Below you will learn how to do this.

Project Setup

  1. Download the Android samples from
  2. Import BaseGameUtils into your project as module
  •  Go to the directory where you downloaded android-basic-samples and select libraries -> BaseGameUtils

  • If you get the below error:



Tap on the link and let the ide install missing platform.

  • You might get another error:

Tap on this link also and then go build.gradle from BaseGameUtils.

Change and library versions to the latest ones and sync the project. At the time of this tutorial, the current versions are 3.0.0, 27.0.0 and 11.4.2

3. Add to your app build.gradle file compile project (‘:BaseGameUtils’)

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation ''
    implementation ''
    compile project (':BaseGameUtils')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation ''
    androidTestImplementation ''


5. Add ids.xml to res – values directory

<?xml version="1.0" encoding="utf-8"?>
    <string name="app_id">YOUR_APP_ID</string>
    <string name="leaderboard_id">YOUR_LEADERBOARD_ID</string>

You will learn further in this tutorial how to obtain these 2 ids.

6. Add meta-data code inside <application> tag from AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

        <meta-data android:name=""
            android:value="@string/app_id" />
        <meta-data android:name=""


How to get the app_id?

In order to get the app_id and be able to use Google Play games services, you need to:

  • set up the game in the Google Play Console
  • generate an OAuth 2.0 client ID for your Android app.

Below are the steps about how to do this:

1. Add new game in Google Play Console

2. Game Set Up

3.  Go to Linked Apps

4.  Select Android platform

5. Set package name (of your app)

Don’t forget that you have to copy-paste here your app package name. After that, click on the Save and Continue button.

6. Authorize your app now

7. Copy-paste your app SHA1 fingerprint

In order to obtain your SHA1 fingerprint follow the steps from this post.

8. Obtain Application Id

In the screenshot is our application id. You will have to copy yours and then paste it in the ids.xml file from your project.

9. Create your Leaderboard

10. Save Leaderboard

11. Copy Leaderboard id

Copy your leaderboard id and paste it in ids.xml file.

MainActivity class

package ro.tutorial.funcode.gameleaderboard;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends BaseGameActivity {
    //  arbitrary integer for the request code
    public static final int REQUEST_LEADERBOARD = 3245;

    protected void onCreate(Bundle savedInstanceState) {

        Button leaderboardButton = findViewById(;
        leaderboardButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (mHelper.isConnecting() || !mHelper.isSignedIn()) {

                    Toast.makeText(MainActivity.this, getString(R.string.log_to_games_first_warning),


                try {
                            getString(R.string.leaderboard_id)), REQUEST_LEADERBOARD);
                } catch (Exception e) {

    public void onActivityResult(int request, int response, Intent data) {
        super.onActivityResult(request, response, data);

        mHelper.onActivityResult(request, response, data);

    public void onSignInFailed() {


    public void onSignInSucceeded() {


We just used BaseGameActivity class from BaseGameUtils module. It is an example class that  akes care of setting up the API client object and managing its lifecycle.

IMPORTANT: If your app is unpublished and you haven’t added a test account, you will have to do this.  See the steps here.

Now you should be able to display your leaderboard. :)

You can also check the android developers documentation.

Exit mobile version