‹ Google Play Games Services Doc Home

Google Play Games Services Integration Guide

For the C++ version of cocos2d-x v3.x - (all other versions)

Integration

Open a terminal and use the following command to install the SDKBOX Google Play Games Services plugin. Make sure you setup the SDKBOX installer correctly.

$ sdkbox import gpg 

Warning: Account creation within iOS is no longer supported, please read this blog post for details

After Installation

Android

Update AndroidManifest.xml

Add this meta-data tag to your AndroidManifest.xml

<meta-data android:name="com.google.android.gms.games.APP_ID" 
    android:value="@string/google_app_id" />

Update string.xml

Add this entry to proj.android/res/values/string.xml

<string name="google_app_id">777734739048</string>

Replace google_app_id value with your own App Id.

iOS

Set a rootview controller for GPG:

Update proj.ios_mac/ios/RootViewController.h Add this:

#import <GoogleSignIn/GoogleSignIn.h>

// Change RootViewController class definition to:
@interface RootViewController : UIViewController<GIDSignInUIDelegate> 

Set Google Play signin listeners

Update proj.ios_mac/ios/AppController.mm

  1. Add this method:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
    return [[GIDSignIn sharedInstance] handleURL:url
                               sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                      annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}
  1. In this method:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

add a call to (before return YES):

    // _viewController could also be named 
    //  viewController, depending of the project type.
    [GIDSignIn sharedInstance].uiDelegate = _viewController;

Add URL types

Add the following URL types under your project > Info > URL Types

Further info

For more information check out the official documentation

Important Notice

Please make sure the following settings in your project to make the plugin work well.

Disable App Transport Security

Adding the following entry to the info.plist file:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

It should look like this:

Disable Bitcode support

You have to turn off Bitcode support. If you don't, cocos2d-x will fail to build.

Set your game requires full screen

If your game doesn't support all screen orientations, you will need to check Requires full screen in Xcode. If you do not, your app will fail Apple's submission process.

Whitelist canOpenURL function

This setting depends on what plugins are in your project. You may need to add the required entry to the info.plist, under LSApplicationQueriesSchemes.

Usage

Pre-requisites

Your must create your app on Google Play Developer console and all the services must be explicitly enabled and configured in the console.

Note: Google Play Games Services will use your release keystore by default, if you want to test your game in debug settings, please link an additional app with debug keystore

Initialize Google Play Games

Initialize the plugin where appropriate in your code. We recommend to do this in the AppDelegate::applicationDidFinishLaunching() or AppController:didFinishLaunchingWithOptions(). Make sure to include the appropriate headers:

AppDelegate::applicationDidFinishLaunching()
{
     sdkbox::PluginGPG::init();
}

Authorization

All Google Play Game services require user login, make sure you request user to login before using any of the services. For more information take a look at the official documentation

Achievements

For Achievements to work, make sure you linked Game Services with your game, also make sure the game is in "Published" state. For more information please take a look at achievements documentation

Leaderboards

For Leaderboards to work, make sure you linked Game Services with your game, also make sure the game is in "Published" state. For more information please take a look at leaderboards documentation

Saved Games

Saved Games feature requires to be turned on during initialization stage by calling EnableSnapshots() as well as Enable it in the Google Play Developer Console. For more information please take a look at Saved Games documentation

Real-time multiplayer

For Real-time multiplayer to work, make sure you linked Game Services with your game, also make sure the game is in "Published" state. For more information please take a look at Real-time Multiplayer documentation

Turn-based multiplayer

For Turn-based multiplayer to work, make sure you linked Game Services with your game, also make sure the game is in "Published" state. For more information please take a look at Turn-based Multiplayer documentation

Events and Quests

For Events and Quests to work, make sure you linked Game Services with your game, also make sure the game is in "Published" state. For more information please take a look at Events and Quests documentation

Player Statistics

Player Stats adds useful analytics data to your game. For more information please take a look at Player Stats documentation

Nearby Connections

Nearby Connections enables local multiplayer and screen casting for your game. For more information please take a look at Nearby documentation

API Reference

For C++ API reference for Google Play Games Services, please refer to the official documentation

Manual Integration

If the SDKBOX Installer fails to complete successfully, it is possible to integrate SDKBOX manually. If the installer complete successfully, please do not complete anymore of this document. It is not necessary.

These steps are listed last in this document on purpose as they are seldom needed. If you find yourself using these steps, please, after completing, double back and re-read the steps above for other integration items.

Manual Integration For iOS

Drag and drop the following frameworks from the plugins/ios folder of theGooglePlay bundle into your Xcode project, check Copy items if needed when adding frameworks:

sdkbox.framework

PluginGPG.framework

GoogleAppUtilities.framework

GoogleAuthUtilities.framework

GoogleNetworkingUtilities.framework

GoogleOpenSource.framework

GooglePlus.bundle

GooglePlus.framework

GoogleSignIn.bundle

GoogleSignIn.framework

GoogleSymbolUtilities.framework

GoogleUtilities.framework

gpg.bundle

gpg.framework

The above frameworks depend upon other frameworks. You also need to add the following system frameworks, if you don't already have them:

AddressBook.framework

AssetsLibrary.framework

CoreData.framework

CoreLocation.framework

CoreMotion.framework

CoreTelephony.framework

CoreText.framework

Foundation.framework

MediaPlayer.framework

QuartzCore.framework

SafariServices

Security.framework

StoreKit

Security.framework

SystemConfiguration.framework

libc++.dylib

libz.dylib

Add a linker flag, if your setup requires it, to: Target -> Build Settings -> Linking -> Other Linker Flags:

-ObjC

Code changes

Set a rootview controller for GPG:

Update proj.ios_mac/ios/RootViewController.h Add this:

#import <GoogleSignIn/GoogleSignIn.h>

// Change RootViewController class definition to:
@interface RootViewController : UIViewController<GIDSignInUIDelegate> 

Set Google Play signin listeners

Update proj.ios_mac/ios/AppController.mm

  1. Add this method:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
    return [[GIDSignIn sharedInstance] handleURL:url
                               sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                      annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}
  1. In this method:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

add a call to (before return YES):

    // _viewController could also be named 
    //  viewController, depending of the project type.
    [GIDSignIn sharedInstance].uiDelegate = _viewController;

Add URL types

Add the following URL types under your project > Info > URL Types

Further info

For more information check out the official documentation

Manual Integration For Android

SDKBOX supports three different kinds of Android projects command-line, eclipse and Android Studio.

Copy Files

Copy the following jar files from plugin/android/libs folder of this bundle into your project’s /libs folder.

Plugingpg.jar

sdkbox.jar

Copy jni libs

Copy and overwrite all the folders from plugin/android/jni to your <project_root>/jni/ directory.

Note: SDKBOX link with gnustl by default, if your project link with c++static please replace the files in <project_root>/jni/<plugin_name>/libs with files in <project_root>/jni/<plugin_name>/libs_c++_static folder

Edit AndroidManifest.xml

Add following meta-data tags:

<meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
<meta-data android:name="com.google.android.gms.games.APP_ID" 
    android:value="@string/google_app_id" />

Make sure to add an entry to the file res/values/string.xml of the form: <string name="google_app_id">777734739048</string> Change that value for your own generated play games App Id.

Edit Android.mk

Edit <project_root>/jni/Android.mk to:

Install in a folder named gpg inside your proj.android\jni directory

Add this to your android.mk file

Add include path

LOCAL_C_INCLUDES += ./gpg/include/

Add static libraries

Add additional requirements to LOCAL_WHOLE_STATIC_LIBRARIES:

LOCAL_WHOLE_STATIC_LIBRARIES += gpg-1
LOCAL_WHOLE_STATIC_LIBRARIES += PluginGPG
LOCAL_WHOLE_STATIC_LIBRARIES += sdkbox

Add a call to:

$(call import-add-path,$(LOCAL_PATH))

before any import-module statements.

Add additional import-module statements at the end:

$(call import-module, ./gpg)
$(call import-module, ./sdkbox)
$(call import-module, ./plugingpg)

Note: It is important to make sure these statements are above the existing $(call import-module,./prebuilt-mk) statement, if you are using the pre-built libraries.

Modify Application.mk (Cocos2d-x v3.0 to v3.2 only)

Edit <project_root>/jni/Application.mk to make sure APP_STL is defined correctly. If Application.mk contains APP_STL := c++_static, it should be changed to:

APP_STL := gnustl_static

Modify Cocos2dxActivity.java

Note: When using Cocos2d-x from source, different versions have Cocos2dxActivity.java in a different location. One way to find the location is to look in proj.android/project.properties. Example: android.library.reference.1=../../cocos2d-x/cocos/platform/android/java

In this case, Cocos2dxActivity.java should be located at:

../../cocos2d-x/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java
import android.content.Intent;
import com.sdkbox.plugin.SDKBox;
onLoadNativeLibraries();
SDKBox.init(this);
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if(!SDKBox.onActivityResult(requestCode, resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
          }
    }
    @Override
    protected void onStart() {
          super.onStart();
          SDKBox.onStart();
    }
    @Override
    protected void onStop() {
          super.onStop();
          SDKBox.onStop();
    }
    @Override
    protected void onResume() {
          super.onResume();
          SDKBox.onResume();
    }
    @Override
    protected void onPause() {
          super.onPause();
          SDKBox.onPause();
    }
    @Override
    public void onBackPressed() {
          if(!SDKBox.onBackPressed()) {
            super.onBackPressed();
          }
    }

Manual Integration for Google Play Services SDK (dependent library only)

Suggestion

Please try the SDKBOX installer first. It will do all the following step for you automatically.

$ sdkbox import googleplayservices

Modify project.properties

An Android Library Reference for Google Play Services is required. The path will be different depending upon your setup. Also, this is an additional download that does not come as part of a standard install. To install use the sdk installer and choose extras->google play services. Here is an example of what this line could look like:

android.library.reference.1=
../android/sdk.latest/extras/google/google_play_services/libproject/
google-play-services_lib

Note: if you already have an android.library.reference.1 you can add another by incrementing the number as android.library.reference.2, etc.

Integration manually

We make a lite version of Google Play Services, the project repo is https://github.com/darkdukey/Google-Play-Service-Lite

Copy Files

Copy the gps folder from plugin folder of this bundle into your project’s /libs folder.

Modify files for Eclipse

  1. Modify project.properties
# For source project
android.library.reference.2=../cocos2d/cocos/platform/android/java/libs/gps/

# Or
# For framework project
android.library.reference.1=libs/gps/

Modify files for Android Studio

1. Modify cocos2d/cocos/platform/android/libcocos2dx/build.gradle
 dependencies {
+    compile project(':gps')
     compile fileTree(dir: '../java/libs', include: ['*.jar'])
 }
2. Modify proj.android-studio/app/project.properties
 # Project target.
 target=android-10
+android.library.reference.1=../cocos2d/cocos/platform/android/java/libs/gps/
3. Modify proj.android-studio/settings.gradle
 project(':libcocos2dx').projectDir = new File(settingsDir, '../cocos2d/cocos/platform/android/libcocos2dx')
 include ':your_project_name'
 project(':your_project_name').projectDir = new File(settingsDir, 'app')
+
+include ':gps'
+project(':gps').projectDir = new File(settingsDir, '../cocos2d/cocos/platform/android/java/libs/gps')

Proguard (optional)

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# cocos2d-x
-keep public class org.cocos2dx.** { *; }
-dontwarn org.cocos2dx.**
-keep public class com.chukong.** { *; }
-dontwarn com.chukong.**

# google play service
-keep class com.google.android.gms.** { *; }
-dontwarn com.google.android.gms.**

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

#sdkbox
-keep class com.sdkbox.** { *; }
-dontwarn com.sdkbox.**

Note: Proguard only works with Release builds (i.e cocos run -m release) debug builds do not invoke Proguard rules.