Yellow Rabbit

Frozen

Here is an active version

Google Play Games Services

Connect Google Play Games Services Manually

If you follow Google’s instructions to integrate with GPGS (Google Play Games Services), then wild glitches are guaranteed when trying to compile the resulting monster. And the point here is not that you do not find the item File > Import Module in Android Studio, the problem is that the module “BaseGameUtils” is designed for older versions of GPGS and SDK.

So we have:

We clone android basic samples and copy the contents of android-basic-samples/BasicSamples/libraries/BaseGameUtils/src/main/java/ to main/java для того чтобы сохранить иерархию com/google/example/games/basegameutils/.

Also, take android-basic-samples/BasicSamples/libraries/BaseGameUtils/src/main/res/values/gamehelper_strings.xml, then add the contents of strings.xml to your string resources.

We try make project and fix the little things, like the not found R module, it’s easy.

Main Activity

Some imports:


import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;

import com.google.example.games.basegameutils.BaseGameUtils;
import com.google.example.games.basegameutils.GameHelper;

In addition to the variables mentioned in instructions, we need one more:


private GameHelper gameHelper;

It is needed to set the number of authorization requests. In onCreate:


        // init game services
        gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
        gameHelper.enableDebugLog(true);
        gameHelper.setMaxAutoSignInAttempts(1);
        gameHelper.setup(this);


Keys

The inevitable part of adding GPGS are the problems with the keys. This is aided by inadequate documentation from Google. In fact, everything is simple, if you act carefully. So we should have the following state:

  1. A fully working version is published without GPGS.
  2. A Google Play Console game has been created, ready for testing.
  3. Alpha version with GPGS is published.

Point 2 provides for listing testers’ addresses in the “Testing” and testing permission for the alpha version, the same as in the description of the alpha version (paragraph 3).

Next, you need to check that all the keys of our application (final and debugging) are reflected in the “Linked applications” item. To do this, we launch the Developer Console and view all the keys with the type of “Android”, for each key there must be a linked application.

How simple!

By the way, if the key is not reflected, then you just need to add our application again, it will pick up the first free key.

Declined Signing-in

As Google requires, you do not have to offer to authorize in GPGS if the user once refused.

Keep this flag in the settings is not difficult, all that’s left is to catch the very fact of failure. To do this, you need a handler:


    public void onActivityResult(int requestCode, int responseCode,
                                 Intent intent) {
        gameHelper.onActivityResult(requestCode, responseCode, intent);

        Settings.autoLoginGPGSOn = (responseCode != Activity.RESULT_CANCELED);
    }

The Settings class, just like this:


public class Settings {
    public static boolean soundOn = true;
    public static boolean musicOn = true;
    public static boolean introOn = true;
    public static boolean autoLoginGPGSOn = true;


    public final static String file = ".my_cool_settings";

    private static SharedPreferences prefs ;

    public static void load(GLGame game) {
        prefs = game.getSharedPreferences(file, 0);
        soundOn = prefs.getBoolean("soundOn", true);
        musicOn = prefs.getBoolean("musicOn", true);
        introOn = prefs.getBoolean("introOn", true);
        autoLoginGPGSOn = prefs.getBoolean("autoLoginGPGSOn", true);
    }

    public static void save(GLGame game) {
        prefs = game.getSharedPreferences(file, 0);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("soundOn", soundOn);
        editor.putBoolean("musicOn", musicOn);
        editor.putBoolean("introOn", introOn);
        editor.putBoolean("autoLoginGPGSOn", autoLoginGPGSOn);
        editor.apply();
    }
}