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 https://github.com/playgameservices/android-basic-samples
  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 build.tools 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 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    compile project (':BaseGameUtils')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

 

5. Add ids.xml to res – values directory

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

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="http://schemas.android.com/apk/res/android"
    package="ro.tutorial.funcode.gameleaderboard">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <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"/>
    </application>

</manifest>

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;

import com.google.android.gms.games.Games;
import com.google.example.games.basegameutils.BaseGameActivity;

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


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

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

                    return;
                }

                try {
                    startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mHelper.getApiClient(),
                            getString(R.string.leaderboard_id)), REQUEST_LEADERBOARD);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }



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

        mHelper.onActivityResult(request, response, data);
    }

    @Override
    public void onSignInFailed() {

    }

    @Override
    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.

Menu