B4A Library Google Play Game Services

Informatix

Expert
Licensed User
This is a wrapper for the Google Play Services related to games. This library was tested successfully with the API 37 of Google Play Services.

List of features:
  • Sign-in: supported;
  • Achievements: supported;
  • Leaderboards: supported;
  • Real-time multiplayer: supported (NEW!);
  • Turn-based multiplayer: supported;
  • Level and XP: supported;
  • Gifts and requests: supported;
  • Events and quests: not supported;
  • Saved games: supported;
  • Notifications: supported;
  • Player stats: supported;
  • Nearby connections: supported.
To compile, you need B4A v6+, JDK v7+ and android.jar v21+ (cf. Tools/Configure paths).

The library is provided with its Java source code and a few B4A modules:
  • ClsConnection: manages the sign-in, sign-out and connection events;
  • CodConverter: converts your data map to and from the byte array needed for the Saved Games service (this module requires three extra libraries: JSON, ByteConverter and RandomAccessFile) in case you don't own the DataCollection library;
  • CodTurnBasedMatch: helper module for turn-based matches.
Before using this library, you need to:
  • download the Google Play services SDK and Android Support Repository with the Android SDK manager;
  • copy the GooglePlayGameServices library (Jar+Xml) to your additional B4A libraries folder.
When you create a project requiring this library, you need to:
  • add this line in the project attributes of your Main module:
    B4X:
    #AdditionalJar: com.google.android.gms:play-services-games
  • add also this line if you want to access the Nearby Connections API:
    B4X:
    #AdditionalJar: com.google.android.gms:play-services-nearby
  • add these lines to your manifest:
    B4X:
    AddApplicationText(
    <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" />)
  • add also these lines to the manifest if you want to access the Nearby Connections API:
    B4X:
    AddApplicationText(
    <meta-data android:name="com.google.android.gms.nearby.connection.SERVICE_ID"
        android:value="$PACKAGE$" />)
  • copy the games-ids.xml file generated by your Google Play developer console in the Objects\res\values folder.
To generate the games-ids.xml file, click on the link at the bottom of some screens (e.g. achievements) in your developer console (you need of course a game project properly configured to see it):
ressources.png

This file should look like this:
B4X:
<?xml version="1.0" encoding="utf-8"?>
<!--
Google Play game services IDs.
Save this file as res/values/games-ids.xml in your project.
-->
<resources>
  <string name="app_id">01234567890</string>
  <string name="achievement_win_on_a_very_large_map">CgkIuqeG8-kOEAIQAQ</string>
  <string name="achievement_win_on_a_large_map">CgkIuqeG8-kOEAIQAg</string>
  <string name="achievement_win_on_a_medium_map">CgkIuqeG8-kOEAIQAw</string>
  <string name="achievement_win_on_a_small_map">CgkIuqeG8-kOEAIQBA</string>
  <string name="achievement_win_on_a_very_small_map">CgkIuqeG8-kOEAIQBQ</string>
  <string name="leaderboard_main_score">CgkIuqeG8-kOEAIQBw</string>
</resources>
To set up a game in the Google Play developer console, please read this, then this.

To get details about a returned status code, please read this.
 

Attachments

Last edited:

Informatix

Expert
Licensed User
[DEPRECATED - The new demo files are in the first post]

Here are a few demos: AchievementsLeaderboards, NearbyConnections, SavedGames and TurnBasedMatch.
To run them with an existing game project, you have to change their package name, copy the res data, set the path to the res folder at the beginning of Main and add the games-id.xml file in Objects\res\values.
Note: The SavedGames demo requires that you add the Drive API in your developper console.
 

Attachments

Last edited:

Informatix

Expert
Licensed User
This is fantastic! Does it work in B4J also?
I don't know. My knowledge of B4J is next to none. But I really doubt that the lib can be useful on your desktop because it needs the Google Play Services (an application installed on mobile devices).
 
Last edited:

Informatix

Expert
Licensed User
Ok, I asked since I saw they have it available for Android, iOS and web apps. But since this lib is only for Android, we could then use the REST API from B4J, right?:

https://developers.google.com/games/services/web/gettingstarted
https://developers.google.com/games/services/web/api/index


EDIT: It indeed seems possible:
http://dbaelz.de/2014/02/15/secludedness-libgdx-and-google-play-game-services/
If you're able to send a HTTP POST, GET or PUT with B4J, then you can use the GPGS.
Note that some features are unavailable this way, e.g. real-time multiplayer and saved games.
 
Last edited:

joseluis

Active Member
Licensed User
Thank you Informatix. I'm gonna try to make a wrapper/library to use the REST API.
 

Marcela Calixto

Member
Licensed User
I've tried to follow the recipe but I'm getting an error when trying to use

Initialize: creating a GoogleAPIClient instance
java.lang.NoSuchMethodError: com.google.android.gms.common.api.GoogleApiClient$Builder.addApi
at flm.b4a.googleplay.GooglePlayConnection.Initialize2(GooglePlayConnection.java:165)
at flm.b4a.googleplay.GooglePlayConnection.Initialize(GooglePlayConnection.java:139)
 

Marcela Calixto

Member
Licensed User
No idea. Check your google-play-services.jar (2 765 459 bytes), and check the version of your Google Play Services app on your device (should be 6.1.xxx).
Yes, my google-play-services.jar was old. I replaced and it fixed it. But know, when I'm clicking Sign In, I'm getting

java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 5089000 but found 4323000. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@Integer/google_play_services_version" />

In my Manifest editor I have
AddApplicationText(
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<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"/>
)
 

Informatix

Expert
Licensed User
Yes, my google-play-services.jar was old. I replaced and it fixed it. But know, when I'm clicking Sign In, I'm getting

java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 5089000 but found 4323000. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@Integer/google_play_services_version" />

In my Manifest editor I have
AddApplicationText(
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<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"/>
)
Did you update the res folder as well?
 

Informatix

Expert
Licensed User
No. There seems to be no solution
During the development of the library, I got a few strange errors while testing. Some of them happened after I changed something in my project on the dev console. We obviously have to wait after any change. For example, I had to wait a couple of hours before one of my leaderboards work as expected. The other errors were due to the network. So you should retry later; things might suddenly work.
About leaderboards, I'm still unable to make the social variant work (I can only see the current's player; no one else from the same circles). I probably did not understand something.
 
Top