Noob Android Q: I can't create a DB

Its hard to get started. I am using “implementation ‘com.couchbase.lite:couchbase-lite-android:2.6.0’”
I am supposed to evaluate this tech, but I can’t seem to get to square one…
Any suggestion is appreciated.

From what I read from the docs the following should work. But the new Database( dbname, config) fails
dbname is “salesfloor”

And while I am new to Android development, as an old Java guy, I would have thought the exception would have been caught (but alas no, the app just exits).

public void initCouchbaseLite(Context context) {
    appContext = context;

// -------------------------
// Database operation
// -------------------------

private void openDatabase(String dbname) {
    DatabaseConfiguration config = null;
    try {
        config = new DatabaseConfiguration();
    } catch ( Exception e1 ) {
        setStatus( e1.getMessage() );

// config.setDirectory(String.format("%s/%s", appContext.getFilesDir(), dbname));
try {
database = new Database(dbname, config);
} catch (Exception e) {
int i = 0;

protected void onCreate(Bundle savedInstanceState) {
setStatus(“Not Connected”);
findViewById( View.OnClickListener() {
public void onClick(View view) {

How does it fail?


So, to repeat @borrrden, you say “it fails”. We, seriously, are going to need a little more detail. What is “it”?

As an old Java guy, you will know that the way to catch an exception is to surround the call that throws it with a try-catch that catches an exception of an assignable type.

I have several comments about this code:

  1. does startSession call openDatabase?
  2. Unless you have a really good reason to set the directory for the database, you probably should use the default. Android is picky about where it puts stuff, and well throw if you attempt to put things in the wrong spot, without the right permissions
  3. while you can call initCouchbaseLite repeatedly, there is no reason to do so. Put a single call in your Application.onCreate
  4. this might work for some kind of evaluation. In a real app you are going to have to get your database manipulation off the UI thread.
  5. … aaaaand finally, the reason, almost certainly, that your app is failing is that an Error (not an Exception) is being thrown when you try to initialize the DB. This is because you are missing the gradle clause that causes the JVM to use Java 1.8, required by CBL. Add this to your android block
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'

Yep, #5 was the ticket to ride.
You should probably make an explicit dependency somehow.