Android

Android

Edit on GitHub
Platform:

Add Couchbase Lite to your app

Gradle

  1. Expand the app folder and open the build.gradle file.

  2. Add some required rules in the android section of the application's Gradle file (usually at app/build.gradle).

    android {
      // ...
      // workaround for "duplicate files during packaging of APK" issue
      // see https://groups.google.com/d/msg/adt-dev/bl5Rc4Szpzg/wC8cylTWuIEJ
      packagingOptions {
          exclude 'META-INF/ASL2.0'
          exclude 'META-INF/LICENSE'
          exclude 'META-INF/NOTICE'
      }
    }
    
  3. Next, add Couchbase Lite as a dependency in the dependencies section.

    dependencies {
      // ...
      compile 'com.couchbase.lite:couchbase-lite-android:1.3.1'
    }
    
  4. In the Android Studio tool bar, click Sync Project with Gradle Files.

Available libraries

The following libraries can be used in an Android Studio project to import different components.

Gradle Dependency Line Service
com.couchbase.lite:couchbase-lite-java-listener:1.3.1 To access the database through HTTP (often used for hybrid development and peer-to-peer sync).
com.couchbase.lite:couchbase-lite-android-forestdb:1.3.1 To use ForestDB as the storage type.
com.couchbase.lite:couchbase-lite-android-sqlcipher:1.3.1 To enable encryption on SQLite databases.
com.couchbase.lite:couchbase-lite-java-javascript:1.3.1 JavaScript view (map/reduce) engine for the REST API.

Supported architectures

The list of supported Android architectures depends on the storage type (i.e. SQLite or ForestDB) you are using. For SQLite databases, all architectures are supported:

armeabi-v7a x86 arm64-v8a x86_64 mips64 armeabi mips
Yes Yes Yes Yes Yes Yes Yes

For ForestDB databases, the supported architectures are:

armeabi-v7a x86 arm64-v8a x86_64 mips64 armeabi mips
Yes Yes Yes Yes Yes No No

Getting Started

Create a new Android Studio project and install Couchbase Lite by following one of the methods above.

Open MainActivity.java in Android Studio and add the following in the onCreate method.

// Create a manager
Manager manager = null;
try {
    manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS);
} catch (IOException e) {
    e.printStackTrace();
}
// Create or open the database named app
Database database = null;
try {
    database = manager.getDatabase("app");
} catch (CouchbaseLiteException e) {
    e.printStackTrace();
}
// The properties that will be saved on the document
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("title", "Couchbase Mobile");
properties.put("sdk", "Android");
// Create a new document
Document document = database.createDocument();
// Save the document to the database
try {
    document.putProperties(properties);
} catch (CouchbaseLiteException e) {
    e.printStackTrace();
}
// Log the document ID (generated by the database)
// and properties
Log.d("app", String.format("Document ID :: %s", document.getId()));
Log.d("app", String.format("Learning %s with %s", (String) document.getProperty("title"), (String) document.getProperty("sdk")));

// Create replicators to push & pull changes to & from Sync Gateway.
URL url = null;
try {
    url = new URL("http://10.0.2.2:4984/hello");
} catch (MalformedURLException e) {
    e.printStackTrace();
}
Replication push = database.createPushReplication(url);
Replication pull = database.createPullReplication(url);
push.setContinuous(true);
pull.setContinuous(true);

// Start replicators
push.start();
pull.start();

Build and run. Filter on the term "D/app" in LogCat and you should see the document ID and property printed to the console. The document was successfully persisted to the database.

Installing Sync Gateway ⟶