Couchbase Listener for Android for total newbies

168 views
Skip to first unread message

Yaron Goland

unread,
Oct 23, 2013, 11:16:35 PM10/23/13
to mobile-c...@googlegroups.com

If you are not a total newbie then please move along. Or, better yet, if you actually know something please respond with the corrections to the outrageous mistakes I'm sure exist below.

 

The Listener is a project our dear friends at Couchbase mobile have put together for Android that lets one run a CouchDB server over HTTP. I need this because I'm working on a peer to peer project where both local apps as well as remote devices will need to talk over HTTP to the device's CouchDB Server.

 

In this mail I outline all the steps it took to get this all working.

I tried to build the listener in Android Studio but that caused endless problems. So instead I just built it on the command line as follows:

 

Step 1 - Point your Git client at from https://github.com/couchbase/couchbase-lite-android-listener.git and download.

Step 2 - Go download Gradle 1.8

Step 3 - Check the build.gradle file to make sure that "apply from: 'dependencies-test.gradle' is commented out and 'apply from: 'dependencies-archive.gradle' isn't commented. Now save.

Step 4 - Open a cmd line and issue "set UPLOAD_VERSION_CBLITE=1.0.0-beta".

Step 5 - Now cd into the directory for the Listener project that contains build.gradle and issue the equivalent (for your platform) of " c:\couchbase-lite-android-listener>c:\gradle-1.8\bin\gradle.bat build"

 

This 'just worked' for me and after 10 seconds I had a complete build.

 

Step 6 - Navigate down to build\libs and pick the .aar file of your choice. I use couchbase-lite-android-listener-debug.aar. I made a copy of it and changed the extension to .zip. Inside I found three jar file, classes.jar (in the root) and under 'libs' I found servlet-api-2.4.jar and webserver.jar.

Step 7 - I assume you have set up an Android Studio project to play with CouchDB following the instructions at http://docs.couchbase.com/couchbase-lite/cbl-android/.

Step 8 - Copy the three jar files to the libs directory you put td_collator_so.jar (see step 7).

Step 9 - Edit the build.gradle file for your CouchDB project (the one you built in step 7) and replace "compile fileTree(dir: 'libs', include: 'td_collator_so.jar')" with "compile fileTree(dir: 'libs', include: '*.jar')". This is arguably sleezy since it just sucks up anything in libs but that works for me. :)

Step 10 - Go to Tools->Android->Sync Project With Gradle files. This caused everything to download for me and set up the links to the jars.

 

At that point I could build and go.

 

Using the Listener is thankfully quite easy. Here is the code I currently use to start the listener up.

 

            String filesDir = getFilesDir().getAbsolutePath();

            CBLServer server = new CBLServer (filesDir);

            cblListener = new CBLListener(server, defaultCouchPort);

            cblListener.start();

 

            if (cblListener.getStatus().equals("0") == false)

            {

                throw new RuntimeException("CouchDB Server didn't start up correctly, alas you will have to check the log to see why.");

            }

 

At this point, unless something went wrong, it's up and running and ready to accept requests!

 

I've been trying to test it with Ektorp and that has been a miserable failure but I ran a bunch of tests using CURL over the Android bridge to the emulator and it ran like a champ.

 

                                Yaron

 

 

Traun Leyden

unread,
Oct 24, 2013, 8:19:57 PM10/24/13
to mobile-c...@googlegroups.com
Wow!  This is great stuff.

I turned it into a wiki document: Couchbase listener for android for total newbies 


I tried to build the listener in Android Studio but that caused endless problems.


I have a feeling you may have run into a version incompatibility where more recent versions of Android Studio expect the following in their build.gradle file:

        classpath 'com.android.tools.build:gradle:0.6.+'

(rather than 0.5.+)

I'll be pushing a fix shortly.

Also will get back to your other email.

 

So instead I just built it on the command line as follows:

 

Step 1 - Point your Git client at from https://github.com/couchbase/couchbase-lite-android-listener.git and download.

Step 2 - Go download Gradle 1.8

Step 3 - Check the build.gradle file to make sure that "apply from: 'dependencies-test.gradle' is commented out and 'apply from: 'dependencies-archive.gradle' isn't commented. Now save.

Step 4 - Open a cmd line and issue "set UPLOAD_VERSION_CBLITE=1.0.0-beta".

Step 5 - Now cd into the directory for the Listener project that contains build.gradle and issue the equivalent (for your platform) of " c:\couchbase-lite-android-listener>c:\gradle-1.8\bin\gradle.bat build"

 

This 'just worked' for me and after 10 seconds I had a complete build.

 

Step 6 - Navigate down to build\libs and pick the .aar file of your choice. I use couchbase-lite-android-listener-debug.aar. I made a copy of it and changed the extension to .zip. Inside I found three jar file, classes.jar (in the root) and under 'libs' I found servlet-api-2.4.jar and webserver.jar.

Step 7 - I assume you have set up an Android Studio project to play with CouchDB following the instructions at http://docs.couchbase.com/couchbase-lite/cbl-android/.

Step 8 - Copy the three jar files to the libs directory you put td_collator_so.jar (see step 7).

Step 9 - Edit the build.gradle file for your CouchDB project (the one you built in step 7) and replace "compile fileTree(dir: 'libs', include: 'td_collator_so.jar')" with "compile fileTree(dir: 'libs', include: '*.jar')". This is arguably sleezy since it just sucks up anything in libs but that works for me. :)

Step 10 - Go to Tools->Android->Sync Project With Gradle files. This caused everything to download for me and set up the links to the jars.

 

At that point I could build and go.

 

Using the Listener is thankfully quite easy. Here is the code I currently use to start the listener up.

 

            String filesDir = getFilesDir().getAbsolutePath();

            CBLServer server = new CBLServer (filesDir);

            cblListener = new CBLListener(server, defaultCouchPort);

            cblListener.start();

 

            if (cblListener.getStatus().equals("0") == false)

            {

                throw new RuntimeException("CouchDB Server didn't start up correctly, alas you will have to check the log to see why.");

            }

 

At this point, unless something went wrong, it's up and running and ready to accept requests!

 

I've been trying to test it with Ektorp and that has been a miserable failure but I ran a bunch of tests using CURL over the Android bridge to the emulator and it ran like a champ.

 

                                Yaron

 

 

--
You received this message because you are subscribed to the Google Groups "Mobile Couchbase" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Yaron Goland

unread,
Oct 24, 2013, 9:54:51 PM10/24/13
to mobile-c...@googlegroups.com

Re Wiki - I'm glad I could be of some use! :)

 

Gradle Version - Actually I did run into that issue and even after fixing it things still didn't work but honestly gradle and intellij/Android Studio and I haven't been having the best of time in general. I literally spent all of today putting https://peerly.codeplex.com/wikipage?title=Gradle and IDEA Intellij together.

Traun Leyden

unread,
Oct 25, 2013, 1:14:38 PM10/25/13
to mobile-c...@googlegroups.com
That link isn't working and I'd be interested in checking that out.  Can you double check the link?

Yaron Goland

unread,
Oct 25, 2013, 1:40:00 PM10/25/13
to mobile-c...@googlegroups.com

https://peerly.codeplex.com/wikipage?title=Gradle%20and%20IDEA%20Intellij

 

I also posted about this on the IDEA message board - http://devnet.jetbrains.com/thread/450454 but no responses as of when I sent this email.

 

                Thanks,

 

                                                Yaron

SomeDeveloper

unread,
Mar 20, 2014, 11:19:39 AM3/20/14
to mobile-c...@googlegroups.com, yar...@microsoft.com
Hi, 

I don't know if you can help me with my problem but I am stuck and don't know what to do.
My problem is :

I create project in which I start Couchbase Lite Listener to listen on specific port

        manager = startCBLite();
        startDatabase(manager, DATABASE_NAME);

        LiteListener listener = new LiteListener(manager, suggestedListenPort);
        int port = listener.getListenPort();
        Thread thread = new Thread(listener);
        thread.start();

and that is OK, but now I want from another device to connect to that host and port. How to do that?
I tried with Ektorp 

AndroidHttpClient httpClient = new AndroidHttpClient.Builder()
        .url("http://localhost:5984")
        .build();

CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
CouchDbConnector db = new StdCouchDbConnector("mydatabase", dbInstance);

db.createDatabaseIfNotExists();

but not work and I found that  comment (http://www.couchbase.com/communities/q-and-a/simple-ektorp-connection) - Couchbase is different from CouchDB. We do not support Ektorp. - See more at: http://www.couchbase.com/communities/q-and-a/simple-ektorp-connection#sthash.3ZpGY4Nk.dpuf

Help me

Yaron Goland

unread,
Mar 20, 2014, 11:31:19 AM3/20/14
to SomeDeveloper, mobile-c...@googlegroups.com

A few things to consider:


ADB - Are you running on an Android device or are you using an Android VM? If the later then it will only listen on the 'local' hidden VM network. To get around that you need to use the 'adb forward' command to forward a port from your machine to the VM. Note that this only works for applications (e.g. the Ektorp client) that are running on the same physical machine as the VM. This issue has caused me endless grief, especially for demos.


Ektorp - The good news is that Ektorp absolutely does work with CouchBase Lite. I know this because it's my main client. See the bottom of https://github.com/couchbase/couchbase-lite-java-listener for how to configure Ektorp to work with Listener. I see in your code below that you aren't using the useExpectContinue(false) flag so your code won't work.


Hope that helps,


            Yaron



From: SomeDeveloper <krasimir...@dinner.me>
Sent: Thursday, March 20, 2014 8:19 AM
To: mobile-c...@googlegroups.com
Cc: Yaron Goland

Subject: Re: Couchbase Listener for Android for total newbies

Manju

unread,
Aug 20, 2014, 2:26:03 AM8/20/14
to mobile-c...@googlegroups.com, yar...@microsoft.com

Yaron,

Thanks for putting this together. I think it will be of great help for us.

I just wanted to get some confidence on using Couchbase Lite for our project and I believe you have used Ektorp to connect to local Couchbase Lite as well as CBL Http listener. So would like to get to know your advice and recommendation.

Especially since we were _not_ successful in using Ektorp to connect to Couchbase Lite 1.0.1.

We see the following problems -

1. Jackson unable to parse because of character '<' (60) (I am sure the data is good because the same data works with Couchbase Mobile)
2. After sometime libc causes crash (dlfree accessing some wrong memory location)

Surprisingly, CBL 1.0.3-beta worked fine with the same data.

Thanks
Manju

Yaron Goland

unread,
Aug 20, 2014, 11:45:11 AM8/20/14
to Manju, mobile-c...@googlegroups.com

This is all still on Android right?


So right now I'm running on a fork of the CBL code from 6/14/2014. I will be updating to the latest master in a few weeks. But we decided to snap and stabilize while we get ready for our internal review [1]. So my experiences might not be identical to yours.


The gradle file (with our dependencies and such) for Android is available at [2]. As you can see we are just using vanilla org.ektorp:org.ektorp.android:1.4.2. No magic.


What's killing me in that the '<' issue is ringing a bell in my head. I know I've run into that!!!! But I can't remember where or how. I'm really sorry. It's driving me nuts. Maybe my brain will surface it later. But I know I've heard of this issue before! Arg.


For the libc error, I'm assuming that's thrown by couchbase and not Ektorp? Please clarify because either way that is scary.


If you want to see how we use Ektorp you would need to clone [3] and then look for all uses of the two CreateEktorpClient APIs in CreateClientBuilder in the UniversalUtilities project. You will see various tests. We use the same Ektorp code in both Java and Android. The Android binding, for whatever it's worth, is in AndroidUtilities in AndroidEktorpCreateClientBuilder. We run these tests pretty much daily and they are passing fine.


Sorry I can't be of more help,


            Yaron


[1]

https://pairlist10.pair.net/pipermail/thali-talk/2014-August/000020.html

[2]

https://github.com/thaliproject/thali/blob/master/Production/Utilities/AndroidUtilities/AndroidUtilities/build.gradle

[3]

https://github.com/thaliproject/thali.git


From: Manju <han...@gmail.com>
Sent: Tuesday, August 19, 2014 11:26 PM

To: mobile-c...@googlegroups.com
Cc: Yaron Goland
Subject: Re: Couchbase Listener for Android for total newbies
Reply all
Reply to author
Forward
0 new messages