Backup / Restore / Cloud Save

Cloud Save – Part 1

play-services-logoIn a previous series on Styling Android we looked at persisting data using the Data Backup APIs. However the Google documentation suggests that this technique is unsuitable for persisting app state across multiple devices, but more for persisting app state when a user replaces a device. At Google I/O 2013 a new API was introduced, which is part of Google Play Services, that is specifically designed to persist app state across multiple devices: Cloud Save. In this short series fo articles we’ll look at how to implement this.

Cloud Save is actually being pitched as a mechanism for saving game state across devices as it is part of the new Google Play Game Services. However, the technique works equally well in apps which are not games, but we need to understand the constraints of Cloud Save in order to decide whether it is appropriate for our needs. Cloud Save has limited storage – it has 4 x 128KB slots that we can save data to. This is actually quite a lot for persisting simple app state, but if you have a requirement to persist more than this, then it is likely that it is data rather than simply state that you need to persist. If that’s the case, then you’ll probably need to look at a rather larger capacity cloud data storage mechanism such as Google Drive, Dropbox, etc. However, assuming modest size requirements, Cloud Save is actually pretty easy to implement.

We first need to create a new project and add the Google Play Services library. I’m using the new Gradle build system (using the Android plugin version 0.4.2, at the time of writing), and this is really easy. First you’ll need to go in to the Android SDK Manager app and make sure that you have “Extras|Google Repository” installed. This contains a AAR packaged version of the Play Services library that is much simpler to include than when using the older Ant (or Maven) based builds. Once “Google Repository” has been installed, all we need to do is add the dependency to build.gradle in our project and Gradle will do the rest:

[groovy highlight=”14″] buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath ‘com.android.tools.build:gradle:0.4.2’

}
}

apply plugin: ‘android’

dependencies {
compile ‘com.google.android.gms:play-services:3.1.36’
}

android {
compileSdkVersion 17
buildToolsVersion “17.0.0”

defaultConfig {
minSdkVersion 14
targetSdkVersion 16
}
}
[/groovy]

If you’re not using Gradle, you’ll need to manually reference the library project which comes with the “Extras|Google Play services” component from SDK Manager. Details of how to do this can be found here.

Next you will need to create a project in the Google APIs console and enable the Google Play App State for this project in the “Services” section.

The next thing that we need to do is generate a Client ID as detailed here. This client ID is based upon the package name of the app, and the signing key that you’re using. Even if you’re doing a debug build, your APK gets signed and, by default, this uses .android/debug-keystore in your home directory. I have omitted the client ID from the code which accompanies this series of articles because it will be of no use to you unless you have my debug-keystore. Therefore you’ll need to generate your own client ID, and you may need to use a different package name for your APK, and the package specified in the Manifest will already have been assigned to me.

Once you have your client ID, you’ll need to update your Manifest:




    

    
        
            
                

                
            
        
        
    


We now have a basic project configured to use Google Play Services, in the next article in this series we’ll get in to the code to see how to implement Cloud Save.

FInal note: I usually like to have some working code at the end of each article, but in this case there was rather a lot of background information and setup required. Apologies for this, but we’ll definitely have something working at the end of the next article.

© 2013 – 2014, Mark Allison. All rights reserved.

Copyright © 2013 Styling Android. All Rights Reserved.
Information about how to reuse or republish this work may be available at http://blog.stylingandroid.com/license-information.

2 Comments

  1. Hi, I also want to use the cloud save for an app that’s not a game. To have the API app id I needed to start a new game project in the developer console. Currently it’s in the “ready to test” state. I guess this limits who can use the API. So do I have to publish the “game” with all it’s mandatory details to keep using the API?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.