Kivy recipe: google play services

Let's assume you have configured google play services as described in last post and you have created google play services project in your google play developer console.

Now I'll show how you can use other google play services.

Client

Clone client from this repo. It's just a sketch, not a fully functioning one, but it will show you the way, how you can adapt google play services in your kivy app. Of course you can extend the client for using other Games or Google API's using the same ideas. I use local settings in my app, so I can put all needed info there (examples are already there in repository). Copy gplay.py to your project.

Dependencies

v4-support

Google play services will need some magic thing called v4-support, that's why we need to add it to the project. Maybe it's only because I was using it in genymotion android x86 virtual machine (samsung galaxy s2) and you won't need it.
So from your sdk extras (something like ~/.buildozer/android/platform/android-sdk-23/extras/) copy android-support-v4.jar to your_project_path/jar and link it in buildozer.spec like this:

android.add_jars = jar/android-support-v4.jar  

Pyjnius

Google client will use new version of pyjnius (1.0.3), which is supporting PythonJavaClass, but it is not on PyPI, so you need to install it from github (https://github.com/kivy/pyjnius/archive/1.0.3.zip). Yolk says there is even 1.4.0 version on PyPI, no idea what's that.

ids.xml

Here is the sad part. build.py script from python-for-android supports adding resources to ant project, but buildozer doesn't. I like buildozer and I don't want to switch back to build.py, so I just created ids.xml like this (you can actually get it in google play developer console)

<resources>  
  <string name="app_id">XXXXXXXXXXX</string>
  <string name="achievement_faithful_comrade">XXXXXXXX</string>
  ...
  <string name="leaderboard_masters_of_analytics">XXXXXXXXX</string>
  ...
</resources>  

and before each android build I just rsync it to python-for-android dist/res/values/string. It would be something like this:

rsync -rav ./ids.xml .buildozer/android/platform/python-for-android/dist/kognitivo_dev/res/values/ids.xml  

Also you will need to set meta-data in buildozer.spec like this:

android.meta_data = com.google.android.gms.version=@integer/google_play_services_version,com.google.android.gms.games.APP_ID=@string/app_id  

PS

Note that you will need to sign the application with the same certificate, which you entered while creating the google play services project (fingerprint, remember?). Obviously you will need to do buildozer android release, not debug, to create the apk with the same signature, as you normally use in production. It slows down the whole thing, just be patient.

Warning: don't publish the project, until you're sure everything's fine. Check all the options for achievements and leaderboards (incremental or not, which is the initial state and so on), because once you published it, it's not possible to change almost anything anymore. The only one way to change something after publication is to recreate project and re-connect it to your app, which is shit. So double check everything to avoid time wasting.

Put client instantiation and connection code in build method of your app like this:

class KognitivoApp(App):  
    ...
    def build(self):
      ...
      from gplay import GoogleClient
      self.google_client = GoogleClient()
      ...

Set client to a property on the app object to share it among all the places in the code easily like this:

from kivy.app import App  
...
App.get_running_app().google_client.show_leaderboard(LEADER  
BOARDS_MAPPING[self.family])  

That's it. Happy competition!


comments powered by Disqus