Skip to Content

Enable Offline OData in Your Android Application

0 %
Enable Offline OData in Your Android Application

Enable Offline OData in Your Android Application

20 min.
Use the Offline OData library to enable data access from an on device database.

You will learn

  • How to enable Offline OData in your Android application

THIS TUTORIAL SERIES CAN ONLY BE EXECUTED AT TECHED as it is. Please find us at the Google booth in the AppSpace and we will provide everything you will need.

Step 1: Add dependency to build.gradle document

In Android Studio, add the following line to the list of dependencies in the app’s build.gradle file to include the Offline OData framework and click on Sync Now. After the sync completes, it will be possible to use the libraries in the offline-odata component.

implementation group:'', name:'offline-odata', version: sdkVersion
Offline OData Gradle Dependency
Log on to answer question
Step 2: Add code to open offline store

Press Ctrl+N and enter

Add the following imports. Note they will appear grey until they are used in the following steps.

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

In the SAPServiceManager class, select the variable, OnlineODataProvider and replace it with the two following variables.

//private OnlineODataProvider provider;
private OfflineODataProvider provider;
private static final String TAG = SAPServiceManager.class.getName();

Comment out the methods openODataStore and getServiceRoot.

Add the below methods.

public void openODataStore(Action0 callback, Context context) {
    setupOfflineOData(callback, context);

private void setupOfflineOData(Action0 callback, Context context) {
    try {
        if (configurationData.loadData()) {
            Toast.makeText(context, "Opening the offline store which may take a few moments the first time it is opened.", Toast.LENGTH_LONG).show();
            //Initialize application context for use by OfflineODataProvider
            //commonly set parameters include setStoreEncryptionKey, setStoreName, setStorePath
            OfflineODataParameters offParam = new OfflineODataParameters();
            String serviceUrl = configurationData.getServiceUrl();
            URL url = new URL(serviceUrl + CONNECTION_ID_ESPMCONTAINER);
            provider = new OfflineODataProvider(url, offParam, ClientProvider.get());
            //Specifies which entities we wish to include in the offline store
            provider.addDefiningQuery(new OfflineODataDefiningQuery("Customers", "Customers", false));
  } catch (MalformedURLException e) {
  } catch (OfflineODataException e) {
  //opens the offline store.  This can take a minute or so the first time it is called as it is created and populated.
  Log.d(TAG, "About to call open on the offline store."); -> {
      Log.d(TAG, "Offline store opened.");
      eSPMContainer = new ESPMContainer(provider);;
      syncOfflineData();  //TODO could be triggered via a menu action
  }, (OfflineODataException offlineODataException) -> {
      Log.d(TAG, "Offline store did not open.", offlineODataException);

public void syncOfflineData() {
    //send the local changes to the back end OData service
    Log.d(TAG, "About to call provider.upload.");
    provider.upload(() -> {
        Log.d(TAG, "Successfully uploaded the changed data.");
        //get changes from the back end OData service. -> {
            Log.d(TAG, "Successfully downloaded the changed data.");
        }, (error) -> {
            Log.d(TAG, "Failed when downloading the changed data with error: " + error.getMessage());
    }, (error) -> {
        Log.d(TAG, "Failed when uploading the changed data with error: " + error.getMessage());

public String getServiceRoot() {
    if (configurationData.loadData()) {
        String serviceUrl = configurationData.getServiceUrl();
        if (serviceRoot == null) {
            serviceRoot = serviceUrl + "/" + CONNECTION_ID_ESPMCONTAINER;
    else {
        throw new IllegalStateException("SAPService manager configurationData.loadData() failed.");
    return serviceRoot;

Press Ctrl+N and enter

Press Ctrl+F and enter openODataStore. In the add the application context to the openODataStore call as a parameter as shown below. Note, this can be done by replacing the second last line of the method with the below code.

getApplicationContext() parameter added
}, getApplicationContext());
Log on to answer question
Step 3: Run and explore the application

Run the application, upon reaching the home screen, it may take a few moments for the offline store to open.

The first time the app is opened it will take longer to open the offline store as it needs to be created and populated. Make sure Wi-Fi is enabled. Consult the Logcat log and wait for the following messages to appear.

Log Message from Opening offline store

Turn on airplane mode on your device (swipe down from the top of the emulator and click on the airplane icon) which simulates having no network connection. Notice that Wi-Fi and mobile data are switched off.

Airplane mode on your device

Navigate to the list of customers by tapping the Customers row. Now tap on a customer.

To edit the customer tap the “Edit Customer” icon.

Edit Customer Button

Change a field of a customer such as the city, and save your changes.

Change the customer’s city

To show that the data is stored offline and then synchronized turn off airplane mode and fully close the app.

Close the app

Re-open the app and check the Logcat. It shows the upload and download operations were successful. The previously made change is now synced with the backend OData service.

Successful upload/download

This example does not handle conflicts. Details on this topic are available at Offline OData Conflicts.

Congratulations! You have created an offline enabled application and have completed this TechEd App Space mission. A version of these tutorials that includes the full setup is available at Get Started with SAP Cloud Platform SDK for Android.

See also the blog Step by Step with the SAP Cloud Platform SDK for Android.

Which of the following are benefits of using the OData Offline library?

Next Steps

Back to top