B4A Library Google Play Game Services

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

  • Demos_37.zip
    71 KB · Views: 478
  • GooglePlayGameServices_372.zip
    241.3 KB · Views: 495
Last edited:

Philip Chatzigeorgiadis

Active Member
Licensed User
Longtime User
Thanks for the help Informatix

It works and I am a few hours, from releasing an update, to one of my games, with Leader-boards and achievements

MaxApps, I am getting the same error like you.
My Google Play Services version is 6.5.87.
I also did what Marcela Calixto suggested in post #34.
The problem persists.
How did you solve it?

Thanks.
 

paris7162

Member
Licensed User
Longtime User
Hi

I have followed your steps , and the APK will casue a error after run with "AchievementsLeaderboards”.

It looks like lost "flm.b4a.googleplay.GooglePlayConnection" , but I can find it in the APK package.

I use API19, and get ID for google console.

Please help me.

** Activity (main) Create, isFirst = true **

clsconnection_class_globals (java line: 87)
java.lang.NoClassDefFoundError: flm.b4a.googleplay.GooglePlayConnection

at com.example.clsconnection._class_globals(clsconnection.java:87)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at com.example.clsconnection.innerInitialize(clsconnection.java:23)
at com.example.clsconnection._initialize(clsconnection.java:264)
at com.example.main._activity_create(main.java:301)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at com.example.main.afterFirstLayout(main.java:98)
at com.example.main.access$100(main.java:16)
at com.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
at dalvik.system.NativeStart.main(Native Method)


I get this same error sporadically when updating AchievementLeaderboards. I've tried it on API 19 and API 20 with Google Play Services 6.1.xxxx. But for the most part this library works great.
 

Informatix

Expert
Licensed User
Longtime User
I get this same error sporadically when updating AchievementLeaderboards. I've tried it on API 19 and API 20 with Google Play Services 6.1.xxxx. But for the most part this library works great.
Are you sure that you quoted the right post? Because you cannot get the NoClassDefFound error sporadically unless you change the JAR file of GPGS between compilations (NoClassDefFound means that a class cannot be found in the library, and classes never disappear at runtime). I test my game on many (real) devices since weeks with API 19 and never encountered a NoClassDefFound error.
 
D

Deleted member 30048

Guest
I found an error. If you load or save a game and turn off the screen (or minimize the app) before the process of loading/saving are finished, when you turn on the screen again an error shows. The error occurs in Activity_Pause.

Is there any way to cancel the process of saving/loading?.

Because I don't speak English very well is very dificult to me explain this, here are an example.

Sub Activity_Pause
If SavingLoadingAreNotFinishedYet then
CancelSavingLoading
end if
end sub

Thanks!

Edit: I have the error in Activity_Pause. In your example the error happens on "main_sg_onsnapshotcreated"
 

Informatix

Expert
Licensed User
Longtime User
I found an error. If you load or save a game and turn off the screen (or minimize the app) before the process of loading/saving are finished, when you turn on the screen again an error shows. The error occurs in Activity_Pause.
What's the message? Could you provide a copy of your log?
Theoretically, it's your responsability that the saving and loading operations complete without issues whatever the user does with the device (e.g. you can prevent the screen from being off by acquiring a wake lock).

Is there any way to cancel the process of saving/loading?.

Because I don't speak English very well is very dificult to me explain this, here are an example.

Sub Activity_Pause
If SavingLoadingAreNotFinishedYet then
CancelSavingLoading
end if
end sub

Thanks!

Edit: I have the error in Activity_Pause. In your example the error happens on "main_sg_onsnapshotcreated"
To cancel, I think you have to call the Cancel function of the GPlayPendingResult class (so you have to store somewhere the instance of the pending result returned by CommitAndClose) but I didn't test it.
 

Philip Chatzigeorgiadis

Active Member
Licensed User
Longtime User
Do you use Java 7 to compile ? Check your javac.exe setting in your IDE ("Tools/Configure Paths").

My Java path is C:\Program Files (x86)\Java\jdk1.7.0_45\bin\javac.exe.
I have also checked the SHA1 key and it is OK. By the way, a wrong SHA1 will result in a different error (MISCONFIGURED).
Checked the package name - OK.
I even tried starting from scratch (i.e. creating a new game on google, with a different package name and SHA1 key. Of course I copied the new values to the games-ids.xml file and recompiled with the new package name and SHA1). I still got Error 0 Code 8.
I also read carefully and checked some of the suggestions mentioned in the stackoverflow discussion you posted previously - no luck.
I am now stuck with this error for more than a week, so any idea or suggestion will be appreciated.
Thanks.
 

Informatix

Expert
Licensed User
Longtime User
My Java path is C:\Program Files (x86)\Java\jdk1.7.0_45\bin\javac.exe.
I have also checked the SHA1 key and it is OK. By the way, a wrong SHA1 will result in a different error (MISCONFIGURED).
Checked the package name - OK.
I even tried starting from scratch (i.e. creating a new game on google, with a different package name and SHA1 key. Of course I copied the new values to the games-ids.xml file and recompiled with the new package name and SHA1). I still got Error 0 Code 8.
I also read carefully and checked some of the suggestions mentioned in the stackoverflow discussion you posted previously - no luck.
I am now stuck with this error for more than a week, so any idea or suggestion will be appreciated.
Thanks.
In Connection.Initialize, is the SavedGamesService parameter to True? If yes and you didn't add the Drive API in your console, then it's probably the reason.
 

Spinter

Active Member
Licensed User
Longtime User
Informatix@

for realtime I had from the connection with your library I could not use the realtime, perhaps something wrong but I can not interpret it all!
starting from the connection I was able to perform data exchange between two applications, remains correct some errors then posterolateral your code with the addition of my class always if you agree.
State code 50%
 

Informatix

Expert
Licensed User
Longtime User
Informatix@

for realtime I had from the connection with your library I could not use the realtime, perhaps something wrong but I can not interpret it all!
starting from the connection I was able to perform data exchange between two applications, remains correct some errors then posterolateral your code with the addition of my class always if you agree.
State code 50%
I'm not sure of the meaning of your post.
 

paris7162

Member
Licensed User
Longtime User
Screenshot_2014-12-17-13-39-20.png

I found an error that happens when one device is viewing the Leaderboard or Achievement Screen and receives a game invitation.

Here are the steps to duplicate error:

On Device 1
1. Sign into Google Play Services with Sign in button
2. Click the Achievements Button

On Device 2
1. Sign into Google Play Services with Sign in button
2. Click the Invite button
3. Invite Device 1


When you click out of the Achievement screen using the arrow on top or the back button, the following error will occur.
 
Top