Android

Android

Edit on GitHub
Platform: iOS Android WPF

Using Gradle

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

Add the following to the android section of the application's build.gradle (the one in the app folder).

// 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'
}

Next, add the following to the dependencies section.

compile 'com.couchbase.lite:couchbase-lite-android:+'

The dependencies section should look similar to this.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.couchbase.lite:couchbase-lite-android:+'
}

In the Android Studio tool bar, click Sync Project with Gradle Files.

Supported architectures

The list of supported Android architectures depends on the storage type (i.e. SQLite or ForestDB). 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

Optional packages

The SDK contains other dependencies that are optional. Here's what each one does:

  • couchbase-lite-java-listener: To access the database through HTTP (often used for hybrid development and peer-to-peer sync). Add it to your Android Studio project using Gradle.

    compile 'com.couchbase.lite:couchbase-lite-java-listener:+'
    

    Note: Android emulators run as separate VMs with a different network interface. You can use the adb forward tcp:5984 tcp:5984 command to access the Listener running on the Android emulator from the host machine.

  • couchbase-lite-android-forestdb: To use the ForestDB storage type.

    compile 'com.couchbase.lite:couchbase-lite-android-forestdb:+'
    
  • couchbase-lite-android-sqlcipher: To enable encryption on SQLite databases.

    compile 'com.couchbase.lite:couchbase-lite-android-sqlcipher:+'
    

Getting Started

Create a new project in Android Studio.

Add Couchbase Lite Android as a dependency using the instructions above.

Open com.couchbase.UntitledApp/MainActivity.java in Android Studio.

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", "Java");
// 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")));

Click the Debug button. The document ID and properties are logged in LogCat.