Quickstart with Couchbase Lite, Android Studio, and Gradle
This post will illustrate all the steps needed to get started with Couchbase Lite in an Android project using Android Studio and the standard Gradle build system. This is the easiest way to get set up to use Couchbase Lite with Android.
We assume you have Android Studio already installed and know how to create a new project. See the Android Studio documentation for more details.
If you have Android Studio installed and ready, the estimated time to complete these steps is under 5 minutes.
Create a new project (or open a current one). This shows what Android Studio looks like after opening a new “Empty Activity” project.
In the Project pane (left-hand navigation area), expand the section entitled “Gradle Scripts”. Double click the build.gradle entry tagged “Module: app”. The arrow in the following image indicates the line to click.
You should see the build.gradle file open for editing in the right-hand editor pane.
Find the “dependencies” section of the build.gradle file. It should look something like this.
compile fileTree(dir: 'libs', include: ['*.jar'])
Add the following line to this section:
The arrow in the following image shows where the entry should be and what the results will look like.
At this point, you will most likely see a message telling you a project sync may be necessary. Click on “Sync Now”. You can also sync at any time using the toolbar button, as shown next.
A synch will take a little time. The Android Studio build system (using Gradle) handles downloading all the necessary files and making the necessary changes to your project to include Couchbase Lite.
Once Gradle has finished, you are ready to go. As one quick test, you can see if Android Studio starts including Couchbase Lite in autocompletion suggestions. In the following example, we’ve started to declare a variable of type “Manager”. You can see Android Studio suggests the Manager class from the com.couchbase.lite package as a completion.
This tutorial was created using Couchbase Lite version 1.2.1 and Android Studio version 2.1.2. Some older versions of Android Studio had problems with duplicate manifest files. This will cause Gradle to show an error. If this happens, try adding these lines to the “android” section of the build.gradle:
// workaround for "duplicate files during packaging of APK" issue
// see https://groups.google.com/d/msg/adt-dev/bl5Rc4Szpzg/wC8cylTWuIEJ
Check out more resources on our developer portal and follow us on Twitter @CouchbaseDev. You can post questions on our forums. And we actively participate on Stack Overflow.
You can follow me personally at @HodGreeley
I look forward to talking with you.