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:
D

Deleted member 30048

Guest
Hi

I am getting

internal error
Error=0 Code=8

any ideas?

Are you trying to access saved games?

check in the game information section if the saved games are as "yes".

Best regards!
 

MaxApps

Active Member
Licensed User
Longtime User
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.


Almost a day later and still the same
 

Marcela Calixto

Member
Licensed User
Longtime User
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.
I see now that may Google Play Services app is version 5.0.89 (ref your previous post about it had to be 6.1....). Could that be the problem? I cannot see how I can update this?
 

Informatix

Expert
Licensed User
Longtime User
I see now that may Google Play Services app is version 5.0.89 (ref your previous post about it had to be 6.1....). Could that be the problem? I cannot see how I can update this?
The version should be 6.1.xxx on all devices, whatever Android version you have. You can update the Google Play Services app with your Play Store app as any other app. On my own devices, it has been updated automatically.
 

hanyelmehy

Active Member
Licensed User
Longtime User
first i get this error
B4X:
AndroidManifest.xml:25: error: Error: No resource found that matches the given name (at 'value' with value '@string/app_id').

i try to remove above lines from manifest to run project ,then i get this error
B4X:
Initialize: creating a GoogleAPIClient instance
State change: Unconfigured -> Disconnected
Initialize: ready for connection
** Activity (main) Resume **
Connect: state = Disconnected
Connect: no auto sign-in, FirstAttempt is false
NetworkInfo.onConnectivityChange: Bundle[{networkInfo=NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true, inetCondition=0}]
Connect: state = Disconnected
Connect: first attempt
Connect: isGooglePlayServicesAvailable returned 0
State change: Disconnected -> Connecting
java.lang.IllegalStateException: A fatal developer error has occurred. Check the logs for further information.
    at com.google.android.gms.internal.hc$h.b(Unknown Source)
    at com.google.android.gms.internal.hc$h.d(Unknown Source)
    at com.google.android.gms.internal.hc$b.fq(Unknown Source)
    at com.google.android.gms.internal.hc$a.handleMessage(Unknown Source)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4517)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
    at dalvik.system.NativeStart.main(Native Method)
 

Informatix

Expert
Licensed User
Longtime User
first i get this error
B4X:
AndroidManifest.xml:25: error: Error: No resource found that matches the given name (at 'value' with value '@string/app_id').

i try to remove above lines from manifest to run project ,then i get this error
B4X:
Initialize: creating a GoogleAPIClient instance
State change: Unconfigured -> Disconnected
Initialize: ready for connection
** Activity (main) Resume **
Connect: state = Disconnected
Connect: no auto sign-in, FirstAttempt is false
NetworkInfo.onConnectivityChange: Bundle[{networkInfo=NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true, inetCondition=0}]
Connect: state = Disconnected
Connect: first attempt
Connect: isGooglePlayServicesAvailable returned 0
State change: Disconnected -> Connecting
java.lang.IllegalStateException: A fatal developer error has occurred. Check the logs for further information.
    at com.google.android.gms.internal.hc$h.b(Unknown Source)
    at com.google.android.gms.internal.hc$h.d(Unknown Source)
    at com.google.android.gms.internal.hc$b.fq(Unknown Source)
    at com.google.android.gms.internal.hc$a.handleMessage(Unknown Source)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4517)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
    at dalvik.system.NativeStart.main(Native Method)
You missed a step in my first post (probably the copy of Games-id.xml). Please re-read it carefully.
 

MaxApps

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
 

hanyelmehy

Active Member
Licensed User
Longtime User
Sorry, but I don't understand. Did you copy the xml file in your res/values folder?
Did you copy the xml file in your res/values folder? yes i copy it and make all instruction as mention in first post
my question about xml file name ,i use (ids.xml) as a file name ,you say (probably the copy of Games-id.xml) ,so what is the correct name (ids.xml or Games-id.xml)
Thank you
 

Informatix

Expert
Licensed User
Longtime User
Did you copy the xml file in your res/values folder? yes i copy it and make all instruction as mention in first post
my question about xml file name ,i use (ids.xml) as a file name ,you say (probably the copy of Games-id.xml) ,so what is the correct name (ids.xml or Games-id.xml)
Thank you
I cannot say whether the name is really important, but as you can read in the file itself, Google suggests to name it "games-ids.xml", so there's no good reason to do otherwise.
 

ppgirl

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)
 

Informatix

Expert
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 don't know why you get this error, but you probably missed something. That looks like there's a library missing.
 

Spinter

Active Member
Licensed User
Longtime User
test login Google Play Services rev. 21 ok!
TurnBasedMatch (activate Multiplayer Turn)
save game test ok (activate game save)
AchievementsLeaderboards(activate game save)
 
Last edited:
Top