Starter App for Couchbase Lite on Android


    Description — A starter app for Couchbase Lite on Android
    Abstract — This content provides sample code and instructions that enable you to test your Couchbase Lite for java-android installation.

    Open in Android Studio and copy the following code in the onCreate method. This snippet demonstrates how to run basic CRUD operations, a simple Query and running bi-directional replications with Sync Gateway.

    Example 1. Sample code to test install
    // Initialize the Couchbase Lite system
    // Get the database (and create it if it doesn’t exist).
    DatabaseConfiguration config = new DatabaseConfiguration();
    Database database = new Database("getting-started", config);
    // Create a new document (i.e. a record) in the database.
    MutableDocument mutableDoc = new MutableDocument()
    .setFloat("version", 2.0F)
    .setString("type", "SDK");
    // Save it to the database.;
    // Update a document.
    mutableDoc = database.getDocument(mutableDoc.getId()).toMutable();
    mutableDoc.setString("language", "Java");;
    Document document = database.getDocument(mutableDoc.getId());
    // Log the document ID (generated by the database) and properties
    Log.i(TAG, "Document ID :: " + document.getId());
    Log.i(TAG, "Learning " + document.getString("language"));
    // Create a query to fetch documents of type SDK.
    Query query =
    ResultSet result = query.execute();
    Log.i(TAG, "Number of rows ::  " + result.allResults().size());
    // Create replicators to push and pull changes to and from the cloud.
    Endpoint targetEndpoint = new URLEndpoint(new URI("ws://localhost:4984/getting-started-db"));
    ReplicatorConfiguration replConfig = new ReplicatorConfiguration(database, targetEndpoint);
    // Add authentication.
    replConfig.setAuthenticator(new BasicAuthenticator("sync-gateway", "password"));
    // Create replicator (be sure to hold a reference somewhere that will prevent the Replicator from being GCed)
    Replicator replicator = new Replicator(replConfig);
    // Listen to replicator change events.
    replicator.addChangeListener(change -> {
      if (change.getStatus().getError() != null) {
        Log.i(TAG, "Error code ::  " + change.getStatus().getError().getCode());
    // Start replication.

    Build and run. You should see the document ID and property printed to the console. The document was successfully persisted to the database.