B4A Library Google Play Game Services

First off, my apologies on my Wrapping Skill, this is no where near as polished as from the big boys of wrapping here. Also, many thanks to Erel for helping me get through this.

The story: I need this library, and it is working for me. If you know what it is, and have been waiting for it, hopefully my tutorial will help you get going with it. If you don't know what this library does, or if you need it, check out here

Right now, this library can handle Leaderboards, Achievements, Room Creation/Communication (have not done RealTimeSocket yet), One-Click signIn, and Anti-Piracy Checks. Did not implement Cloud Save.

Still interested? Read on.

First, please review Google's branding guidelines, as there is no way I can keep that up to date on this thread. Here

Next, I recommend just studying the flow of events here particularly the Developer's Guide section.

Still awake? Good.

Before any of this works, you need to register your app within your Developer Account, to get the OAuth necessary. The Google/Java speak instructions are in that developer's guide.

For B4A speak. Must do the following:

1. Follow these directions for adding your app to the console here.

** EDIT - See Erel's post below on how to grab SHA1 fingerprint straight from B4A... **

When you get to the part about adding your SHA1 fingerprint, it's time to break out your command prompt. (If you already know how to get your SHA1 fingerprint, then continue on) First, find out where (or make a new) password file for B4A is kept by looking at your B4A->Tools->Private Sign Key.

Then in your command prompt, change to the directory with keytool in it (for me that is "c:\Program Files\Java\jre6\bin") and type
B4X:
keytool -exportcert -keystore <path-to-debug-or-production-keystore> -list -v
which for me would be
B4X:
keytool -exportcert -keystore C:\Programming\b4a.keystore -list -v
A prompt asking for your password should then pop up. Type your password in that you used to create it(shown on your B4A->Private Sign Key window)

Then, enter that SHA1 fingerprint (type it, copy paste it... I copy the whole command prompt to notepad, and then copy/paste it from there) into the final OAUTH Step and you should get back that your app is linked.
Note the code back will look something like this -> 211205627476-74off6bsgue1qbcka2878p3lurctabft.apps.googleusercontent.com ONLY THE FIRST PART BEFORE THE HASH IS YOUR APP_ID FOR USE IN THE XML FILE

2. Create your new B4A project and include this in your manifest:
AddApplicationText(
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" />
)

3. Create an XML file in your B4A (using your favorite text editor) project called ids.xml in your projectdirectory-> Objects -> res -> values folder, and put something like this in it
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2013 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<!-- TODO: Replace this by your app's app ID! -->
<string name="app_id">ReplaceME</string>
</resources>

Put your AppID obtained from before in the ReplaceMe section and then make sure to set the file to read only (just edit the file included in the sample app)

4. Now it is time for the achievements/leaderboard ID's Need to do that here and here

In the sample app created I use two leaderboards (easy and hard) and four achievements (put in five if you need to continue in your Developer account, but not required to use them all) the four achievements are:
B4X:
Dim easyLeaderboard As String = "ReplaceMe"
Dim hardLeaderboard As String = "ReplaceMe"
Dim EasyAchievement As String = "ReplaceMe" 'for playing a game of easy level
Dim HardAchievement As String = "ReplaceMe" 'for playing a game of hard leve
Dim IncrementAchievement As String = "ReplaceMe" 'for playing 10 games (either hard or easy)
Dim HiddenAchievement As String = "ReplaceMe" 'not visibile until hard and easy games played

Follow these directions, but use these sample Achievements and Leaderboards for the sample application and fill in with your own pithy comments, and Replace the ReplaceMe's for each one in the sample app **Note the IncrementAchivement is of type increment and I used 10 as the amount. The HiddenAchivement is of type Hidden, and you can see in the code how I went about unlocking it**

5. Now time for a choice, either download the attached xml/jar files that go in your library, or (advanced mode) download and compile the attached project source files. Either way, must have gameplayservices.jar, gameplayservices.xml (which are the library wrappers) installed in your Libraries folder and then have gameplayservices selected in B4A. Also, I am a big fan of having a bit of something in my demos, so you will also need Informatix' most excellent AnimationPlus library installed (do search for latest link) and Agraham's superbly efficient ByteConverter library (again, do search).

6. Not sure why, but in order to get the required dependency file "googleplayservices.jar" You must follow the directions here to download the SDK. Then, grab that file and put it in your library. (the file itself is a bit above the limit for me to include here, and I'm not sure about the legal ramifications of including it by itself after reading here) If anybody can tell me otherwise, I'll post it someplace else for easier access to download.

7. Three modules are included in the sample app. The main module is just the basics of logging in (once you get past that, then it's time for the other two) and displaying the SignIn button. The other two modules are my own take on the Type A Number and Button Clicker sample apps (no typing of numbers in my version, but I do add displaying player icons, more robust message passing)

8. Logic/documentation. As I said, trying to figure out how to wrap all of this was a very good challenge/learning process for me. As some of these wrappers included nested classes, and I didn't know exactly how to approach, I made the nested classes separate classes for exposing to B4A. What does that mean? Well, some of the things that happen "behind the scenes" in the original library, I had to make a choice on where they happen in my wrapper. I finally decided that almost anything to do with Room Stuff, happens in the gRoomConfig wrapper (As opposed to RoomConfig, Room and GamesClient in original flow). My samples show a lot of what was wrapped, but if you have a question about a function, and you go to look for more detail on Google's developer site, Where that function is exposed may be different in this library, and sometimes not at all. (If not exposed, it is generally something I didn't see how to write a wrapper for that functionality, at least not yet, but again... it's working for me with a lot of what I wanted to accomplish) I am also including all of the source files, which may help out for those looking to try and learn how to wrap things in the future. Please forgive my poor coding logic in advance. Also, some things are exposed that don't do anything (at least not for me) and are there to show where I think they should be exposed moving forward (most notably the RealTimeSocket stuff), but they are annotated with the hint system as not being ready for prime time!

Of note, many, many listeners in this, and I did include code in the Java to explain many of them, but I don't see how to bring up those hints when using the "Sub "space-Tab" auto-fill" feature.

9. Going forward - I am sure I will be updating some things as I go along, but I don't know at what pace, and who knows, maybe RTS's are really, really important to someone, or the ability to do cloud saving. So, I also am including the source files that I used to wrap. While a learning tool in and of themselves, I do ask, that if you add any functionality, that you pass those additions back to the community. I think we all benefit from using these libraries that others create, and let's face it, sometimes having that 1 library wrapped makes all the difference.. and until Erel figures out how to auto-wrap any library out there... some of us come to a halt going down certain paths unless someone out there can help boot strap our project. (which is why I started this in the first place :) ) Also, please note you'll probably see some ("why is THAT there") in the source code, and it's because I learned a LOT doing this, and changed how some implementations worked as I kept building wrapper classes. I would do it all from scratch to make it cleaner... but I need to get going on the projects that I built this for!
 

Attachments

  • GoogleGameServiceSampleApp.zip
    20.5 KB · Views: 369
  • GamePlayServicesLibraryFiles.zip
    56.3 KB · Views: 450
  • EclipseSourceFiles.zip
    87.6 KB · Views: 277
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Congratulations!
Note that you can find the SHA1 fingerprint in the sign key tool (Tools - Private Sign Key):
SS-2012-12-18_18.11.34.png
 

moster67

Expert
Licensed User
Longtime User
Might not use it myself but I do appreciate all the efforts you have put in there to wrap it all up. Well done.

..and many thanks for sharing!
 

capisx

Member
Licensed User
Longtime User
Awesome..

Wow... just notice this library today.. :icon_clap:
i will surely try this library on my current project. Thank you very much NFOBoy :sign0188:
 

tpakis

Active Member
Licensed User
Longtime User
I'm getting errors when i try to use gleaderboardscore and gleaderboardvariant, any ideas?
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
Thanks for writing this important wrapper. I am trying to get the sample app going, but get the following error. Any ideas?

Parsing code. 0.04
Compiling code. 0.09
Compiling layouts code. 0.03
Generating R file. 0.38
Compiling generated Java code. Error
B4A line: 49
Activity.AddView(gButton, btnTypeANumber.Left, (btnTypeANumber.Top - btnTypeANumber.Height)/2, btnTypeANumber.Width, btnTypeANumber.Height)
javac 1.6.0_26
src\b4a\google\game\test\app\main.java:263: cannot access com.google.android.gms.common.SignInButton
class file for com.google.android.gms.common.SignInButton not found
mostCurrent._activity.AddView((android.view.View)(mostCurrent._gbutton.getObject()),mostCurrent._btntypeanumber.getLeft(),(int)((mostCurrent._btntypeanumber.getTop()-mostCurrent._btntypeanumber.getHeight())/(double)2),mostCurrent._btntypeanumber.getWidth(),mostCurrent._btntypeanumber.getHeight());
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
So when I declare:

Dim g as gLeaderBoardScore

I get the following error:

Compiling generated Java code. Error
B4A line: 676
Dim g As gLeaderboardScore
javac 1.7.0_25
src\com\airlinemates\yahtzee\main.java:961: error: constructor LeaderboardScoreWrapper in class LeaderboardScoreWrapper cannot be applied to given types;
_g = new b4a.game.helper.LeaderboardScoreWrapper();Debug.locals.put("g", _g);
^
required: LeaderboardScore
found: no arguments
reason: actual and formal argument lists differ in length
1 error

This tells me that I need to supply some parameters somewhere - & I think it makes sense that I would need to pass the player ID & the leaderboard ID - but I'm struggling to figure out where & what I need to pass. I tried looking in the Google Services API reference documentation, but there's nothing there that helps me. Any suggestions? Thanks!
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hey NFOBoy, I've been using this wrapper pretty successfully up to this point - so thanks a lot for putting it together, as it works really well for what I need. I've had a couple of issues though. The first one is in the post above & I've pretty much put this aside for now because it's not a major issue, however I have one other problem that I can't seem to find a way around:

If I try to download the Achievements list when the device is not connected to the internet, I get the following error:

"An error has occurred in sub: java.lang.Exception: Sub ggs_onachievementsloaded signature does not match expected signature: Continue?" (I have also attached a screenshot of it.

I've looked at the Android documentation for this function & tried a couple of workarounds, however I can't get it to go away. I've even tried a Try...Catch...End Catch to see if I could use it to let the user know to check their internet connection, but it doesn't get caught - so no luck there.

Any suggestions?

Thanks!
 

Attachments

  • Screenshot_2013-09-05-20-30-24.png
    Screenshot_2013-09-05-20-30-24.png
    159.5 KB · Views: 232

Bas Hamstra

Member
Licensed User
Longtime User
Hi, can anyone help me out on the below error? Installed the lib and followed the instructions. Not sure what I am missing?

Thanks for any pointers!

Bas Hamstra


Parsing code. 0.02
Compiling code. 0.03
Compiling layouts code. 0.01
Generating R file. 0.36
Compiling generated Java code. Error
B4A line: 194
Room.Room = aRoom
javac 1.7.0_07
src\b4a\google\game\test\app\abuttonclicker.java:1061: error: package com.google.android.gms.games.multiplayer.realtime does not exist
mostCurrent._room.setRoom((com.google.android.gms.games.multiplayer.realtime.Room)(_aroom));
 

ppgirl

Member
Licensed User
Longtime User
Hii NFOBoy,

It is a great and hard work. Thank!

I followed your steps , and compiled a APK after many try errors :))

but , it cause a error (ANR) after click login google in my mobile .

I can not catch some useful information from the log . the error looks like google service JAR internal.

LogCat connected to: 0123456789ABCDEF
--------- beginning of /dev/log/main

--------- beginning of /dev/log/system

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

** Activity (main) Resume **

** Activity (main) Pause, UserClosed = false **

** Activity (main) Resume **

** Activity (main) Pause, UserClosed = false **

** Activity (main) Resume **

java.lang.IllegalStateException: A fatal developer error has occurred. Check the logs for further information.

at com.google.android.gms.internal.de$f.a(Unknown Source)
at com.google.android.gms.internal.de$f.a(Unknown Source)
at com.google.android.gms.internal.de$b.be(Unknown Source)
at com.google.android.gms.internal.de$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:4624)
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:965)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:732)
at dalvik.system.NativeStart.main(Native Method)


Could you get me any advice? Thanks!
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
You have the multiplayer functionality. Turn-based and real-time.

I agree with you Informatix, however it is somewhat unstable as it is (based on my experiences with it to-date). It actually works really well to set up a room, send & receive invites, connect & play a game - but I begin to run into issues if I disconnect from a room & then try to reconnect, or create a new room. Whether this is related to the wrapper, the SDK itself or specific hardware I'm not sure - but I do know I have more issues on my Galaxy S3 & Galaxy Tab 3 than on my Nexus 7 (although it's not 100% issue free on the Nexus).

- Colin.
 

Informatix

Expert
Licensed User
Longtime User
I agree with you Informatix, however it is somewhat unstable as it is (based on my experiences with it to-date). It actually works really well to set up a room, send & receive invites, connect & play a game - but I begin to run into issues if I disconnect from a room & then try to reconnect, or create a new room. Whether this is related to the wrapper, the SDK itself or specific hardware I'm not sure - but I do know I have more issues on my Galaxy S3 & Galaxy Tab 3 than on my Nexus 7 (although it's not 100% issue free on the Nexus).

- Colin.
I will release an alpha version before the end of the week to let you test the connection class and the turn-based stuff. The current wrapper is not very well done here and there so I prefered to rewrite it entirely.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
I will release an alpha version before the end of the week to let you test the connection class and the turn-based stuff. The current wrapper is not very well done here and there so I prefered to rewrite it entirely.

Thanks Informatix. I'm using real-time rather than turn-based, so not sure how much I'll be able to test that part, but I'll certainly be able to test the connection stuff.

- Colin.
 

Informatix

Expert
Licensed User
Longtime User
Here's the first alpha version of the rewritten library with the source code. It needs google-play-services.jar v14 (1,283,446 bytes) copied in your libraries folder.

What works (fully documented):
- Connection
- Turn-based matches
- Intents

Still to implement or to finish:
- Real-time
- Achievements
- Leaderboards
- AppState

To do a test, you can use the small example provided but IT WON'T WORK AS IS. First, you need to create a game in your developer console. Second, you have to modify the example to set the package name and the additional resources folder location. Please read the first post of this thread, the Google documentation and the post here.

[File deleted]
 
Last edited:
Top