CouchDB on Android 入门例子

CouchDB on mobile 是一个将CouchDB应用在移动终端上的项目,他包括一个CouchDB数据库,一个Javascript解析中间层和一个极简地轻量级Web Server,利用这些工具,即可在移动设备上构建本地和远程云存储端相同步地一个数据存储服务,然后利用此服务构建你的移动 App。下面是一个在Android上利用CouchDB构建的一个最简单的例子。

这个App被取名为 native Android App ,你可以直接点击并在你的Android手机上安装体验。对CouchApp还不熟悉的同学可以猛击这个链接 在github上你也可以获得 the couchapp 的源码和相关的 android project 的代码。

Warning CouchDB on Android 目前还在beta版本,API的格式可能在以后还会有所更改,所以请最好在别处备份你的重要数据。


  1. Install the Android SDK
  2. Install CouchApp
  3. Download libcouch-android, tgz or git
  4. Install CouchDB on your device / emulator



$ adb logcat

如果你的设备上已经运行有CouchDB了,当出现“CouchDB Running”的提示后,运行下面的命令:

$ adb forward tcp:5985 tcp:5984


然后在elipse里打开你的 libcouch-android 项目 (步骤:File > New Project > Android Project > Open from existing directory 然后选择你下载的libcouch-android 文件夹)

然后创建一个新的项目, File > New Project > Android Project

打开 AndroidManifest.xml 文件,输入如下内容:

然后确认你的app能够与CouchDB的REST API进行沟通(例子)

将 libcouch-android 添加为你应用的一个类包, (Properties > Android 并选择 libcouch-android)

将 的内容复制到你项目的 /src/com/myname/myapp/ 文件中。(别忘了保存你的代码包和类名) 然后再按下面链接对 bootStrapDatabases 和 appToLaunch 进行设置, 设置例子

此json文件 拖到 assets 目录, 这个json文件也可以使用下面命令创建

$ couchapp push --export > filename.json

你的App基本上就完成了,点击eclipse上面的绿色的Run按钮,你的应用就会安装到你的电话里,如果中间有什么问题,查看一下日志 $ adb logcat, 下面就是这个项目的结构:


FAQ – Developing on android

How do I access CouchDB on my computer

$ adb forward tcp:5985 tcp:5984

Will let you access CouchDB via

How do I view the CouchDB / Console logs

$ adb logcat

Something broke?

That isnt a question, but please email me at with as much information as possible. The output from $ adb logcat is extremely useful.

How do I build CouchDB for Android myself?

You dont need to build CouchDB yourself to program against it, but if you really want to then there are instructions on the CouchDB wiki

Much thanks to Matt Adams.

How do I use CouchApp to push to my phone?

The same way you push to any other CouchDB, you need to have set up forwaring first, my .couchapprc file looks like:

  "env" : {
    "default" : {
      "db" : "http://myname:mypass@"
    "android" : {
      "db" : "http://com_arandomurl_couchnotes:pass@"

I tried to create a database with name X and it doesnt exist?

You cant directly create databases as they may conflict with other applications, your databases will be created with your packagename appended (with periods replaced by underscores) so the database created for CouchNotes is names couchnotes-com_arandomurl_couchnotes

How do I find out the credentials to CouchDB databases?

When you create a database you provide a password, a user is created with the username being the package name of your app with periods replaced with underscores (for example com_arandomurl_couchnotes) and they are set as admin on any databases you create. provides a readOrGeneratePass function to help generate and store a password, you can find the generated password by running:

adb shell cat /data/data/com.myname.myapp/files/com_myname_myapp.passwd

Click on the menu button while futon is open and select Admin Password

Is my data really secure?

No, although the SDK provides seperation between applications, currently Android CouchDB stores all data on the SD Card of your phone due to the size of the data. Other applications can access your data directly though the SD Card.

I want a native app, not a CouchApp

The process for building a native application that uses CouchDB is exactly the same, just include libcouch-android as a library to your project, ICouchService.aidl are the functions you use to control CouchDB

How big is CouchDB? / CouchDB is too big for my app

Currently CouchDB is around 17MB uncompressed (about a 10MB download), There is a lot of work being done to reduce the size for both downloading and runtime memory footprint.

How do I use my own CouchApp

$ couchapp push --export > filename.json

Will produce a json file that can be loaded into Couch, the will look for a json file in the assets folder for every database that is bootstrapped.

How do I access Geolocation etc from my CouchApp?

PhoneGap is a library that allows you to access native API’s via Javascript and is an excellent compliment to CouchDB on Android.

How do I publish my App?

Just follow the standard Android publishing procedure

CouchDB Reloads when I change orientation

Android by default will destroy your activity when your phone changes orientation, you can use


to avoid that, here is a working example.