Search code examples
androidcouchbasecouchbase-lite

CouchBase - JavaContext not found in Android Studio


I am following the instruction to set up CouchBase in Android Studio.

The problem is that JavaContext does not seem to exist. Any idea why ?

private void test(){
    // Enable logging
    Logger log = Logger.getLogger("app");
    log.setLevel(Level.ALL);
    JavaContext context = new JavaContext(); // THIS LINE DOES NOT COMPILE
    // Create a manager
            Manager manager = null;
            try {
                manager = new Manager(context, 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.info(String.format("Document ID :: %s", document.getId()));
    log.info(String.format("Learning %s with %s", (String) document.getProperty("title"), (String) document.getProperty("sdk")));
}

Here is my gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.woxthebox.draglistview.sample"
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    // 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'
    }
}

repositories {
    jcenter()
    maven {
        url "http://files.couchbase.com/maven2/"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':library')
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:cardview-v7:23.3.0'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.couchbase.lite:couchbase-lite-android:1.3.0'
}

EDIT:

After some playing around I found that this code works:

    private void test(){
        // Enable logging
        Logger log = Logger.getLogger("app");
        log.setLevel(Level.ALL);
       // JavaContext context = new JavaContext();

        // 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.info(String.format("Document ID :: %s", document.getId()));
        log.info(String.format("Learning %s with %s", (String) document.getProperty("title"), (String) document.getProperty("sdk")));
    }

Can someone explain what was wrong with the original instructions ?


Solution

  • Can someone explain what was wrong with the original instructions ?

    Probably whoever wrote the documentation didn't know there was a AndroidContext for the Android Couchbase Lite.

    Whereas, for a standard Java (embedded?) project, there actually is a JavaContext.

    The Getting Started code on Github does actually use an AndroidContext too, so that page is just wrong.

    See here for the (seemingly correct) Couchbase Android - Getting Started Guide