Vault: an Android library for content sync

May 29, 2015


Vault is a new addition to our Android developer tool set. This post is written for the developers of Android apps, and it describes how Vault simplifies syncing content between a mobile device and Contentful.

Content sync in mobile apps

Synchronizing content between the server and local device storage greatly improves the user experience in mobile applications. In addition to making the content available in offline mode and improving loading times, it reduces data consumption and battery usage.

Contentful provides a Sync API, which enables delta content updates. This endpoint delivers only new and changed content and notifies the client(s) about deleted content.

Vault: a library for syncing content in Android apps

We've decided to further simplify developing Contentful-powered Android apps. Meet Vault: an Android library that simplifies persistence of data from Contentful via SQLite. It enables defining Java representations for your existing Contentful models. On the compilation step Vault creates a corresponding database schema by generating all the required boilerplate code and injecting it into the classpath.

Vault is bundled with a complementary lightweight runtime which exposes a simple ORM-like API for pulling resources from the generated database.


First add the dependencies to the project (assuming that you use Gradle for the builds):

compile 'com.contentful.vault:compiler:VERSION'
compile 'com.contentful.vault:core:VERSION'

Replace VERSION with the latest version of Vault, which you can find on the GitHub repository.


Have a look at the Cat content type of the official demo space:

The models are defined by declaring a subclass of the Resource class. Annotate the class with @ContentType, which takes the content type ID as the value.

public class Cat extends Resource {
  @Field String name;

  @Field List<String> likes;

  @Field String color;

  @Field Cat bestFriend;

  @Field String birthday;

  @Field Integer lives;

  @Field Asset image;

Notice that fields are defined by annotating class attributes with the @Field annotation, which by default uses the name of the attribute as the field ID, but can also be specified explicitly:

String someField;

The spaces can be defined by declaring a class annotated with the @Space annotation, which requires the space ID and an array of model classes:

  value = "cfexampleapi", 
  models = { Cat.class }
public class DemoSpace { }

Arrays of resources are also supported:

// Array of assets
@Field List<Asset> images;

// Array of Dog entries
@Field List<Dog> dogs;

And so is the embedded JSON type:

@Field Map someJsonField;


At runtime Vault should be used with a pre-configured CDAClient:

// Client
CDAClient client = new CDAClient.Builder()

// Configuration
SyncConfig config = SyncConfig.builder()

// Sync
Vault.with(context, DemoSpace.class).requestSync(config);

Calling requestSync() will spawn a worker thread that fetches content via the Sync API to synchronize the remote state with its database. Once the sync is complete, a broadcast with the action Vault.ACTION_SYNC_COMPLETE will be fired. This should only be done if you want to refresh the state from the server, because the locally cached data is available without it.

Alternatively you can provide a SyncCallback which will be invoked once the sync is complete, but make sure to cancel it according to its host lifecycle events:

class SomeActivity extends Activity {
  SyncCallback callback;

  @Override protected void onCreate(Bundle savedInstanceState) {

    Vault.with(this, DemoSpace.class).requestSync(config, callback = new SyncCallback() {
      @Override public void onComplete(boolean success) {
        // Sync completed \o/

  @Override protected void onDestroy() {


Vault provides a wrapper around the databases it generates, which can be easily used to pull persisted resources. Here are some examples:

Vault vault = Vault.with(context, DemoSpace.class);

// Fetch the first Cat

// Fetch the most recently updated Cat
    .order("updated_at DESC")

// Fetch a Cat with a specific name
    .where("name = ?", "Nyan Cat")

// Fetch all Cats, oredered by creation date:


Vault helps Android developers to save time on syncing content between local storage and the server: instead of writing lots of boilerplate code, in particular to represent the remote content model locally, they can now just include the library in the project and have proper models and syncing available at once. Vault is open source and available on GitHub. If you need help in setting this up, let us know, our customer success team is happy to assist you.

About the author

Don't miss the latest

Get updates in your inbox
A monthly newsletter to help you build better digital experiences with Contentful.
add-circle remove subtract-circle