This guide helps you setup an Android emulator on your machine as well as compile and install an Android application that launches a CouchDB service. Unlike the Android docs, this guide assumes you do not use Eclipse and walks you through the command line steps.
The official place to start with Android is http://developer.android.com/sdk/index.html. Refer to that if you get stuck here.
Install the SDK
First, download the latest Android SDK for your operating system at http://developer.android.com/sdk/index.html as of this writing I used Release 14 (R14).
Copy the zip file to a location like /Users/<username>/dev and unzip it.
It will extract into a sub-folder like android-sdk-macosx. Then cd into that
directory and you should see a tools/android binary.
Put that on your path:
export PATH=/Users/<username>/dev/android-sdk-macosx/tools:/Users/<username>/dev/android-sdk-macosx/platform-tools:$PATH
And launch it:
android
Then click on the latest Android checkbox, Android 4.0 as of this writing, and make sure its sub-items are checked. Then in the bottom right corner click Install 5 packages to install the SDK. This will take a while.
Create and launch Emulator
From the Android SDK manager click Tools and Manage AVDs, then New. Then for the SD Card Size put 200 and then click Start.
Tail emulator log
You can see various things that are happening on the system by tracing the Android logs:
adb logcat
Hello World
Setup directory
mkdir ~/dev/helloAndroid
cd ~/dev/helloAndroid
Create project
android create project \
--target 1 \
--name HelloWorld \
--path ./HelloWorldProject \
--activity HelloWorldActivity \
--package com.example.helloworld
# target 1 corresponds to `android list targets`
Edit boilerplate
Edit HelloWorldProject/src/com/example/helloworld/HelloWorldActivity.java
and make it look something like this:
package com.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWorldActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android.");
setContentView(tv);
}
}
Build debug version
If you are putting stuff on the emulator you want to compile in debug mode.
ant debug
Install onto emulator
adb install bin/HelloWorld-debug.apk
If that succeeds you can now browse for the app on the emulator and launch it. Success!
Sometimes adb does not recognize the emulator, restart adb and try again.
adb kill-server
adb start-server
Listing devices should show the emulator
$ adb devices
List of devices attached
emulator-5554 online
Android Couchbase
Android-Couchbase and Android-EmptyApp provide enough for a hello world app bundled with CouchDB.
Clone Android EmptyApp
git clone https://github.com/couchbase/Android-EmptyApp.git
Download and unzip Android Couchbase
cd Android-EmptyApp
curl -O http://files.couchbase.com/developer-previews/mobile/android/android-couchbase-dp.zip
unzip android-couchbase-dp.zip
Build the two packages
ant -f couchbase.xml
ant debug
The second command will probably fail because it was developed on a different SDK build platform and needs an upgrade, continue below. You will also notice a symlinked couchbase-build.xml to an unknown destination, ignore that.
Upgrade project
Since we are running a different release of the SDK we have to upgrade the project’s build system to support our release. You can upgrade it with the following command:
mv build.xml build.xml.orig
android update project --target 1 -p .
Compile again and install:
ant debug
adb install bin/EmptyAppActivity-debug.apk
Now the app is installed on the emulator. Browse the apps list in your emulator and launch the EmptyApp application should display the hello world message. Success!
Forward ports
Connect to CouchDB by forwarding a local port to it:
adb forward tcp:59844 tcp:5984
Then launch http://localhost:59844 in your browser and you should see a response from your mobile CouchDB that looks something like:
{"couchdb":"Welcome","version":"1.2.0a-361a925-git"}
Mobile Futon
Another neat thing you can do is install a mobile version of futon. The following assumes you have the port forwarding setup.
Install couchapp http://couchapp.org/page/installing
Push mobilefuton to your mobile couch:
git clone https://github.com/daleharvey/mobilefuton
cd mobilefuton
couchapp push . http://localhost:59844/futon
Then launch http://localhost:5984/futon/_design/mobilefuton/index.html
in the emulator’s web browser.
Thanks
Couchbase http://couchbase.com/
Dale Harvey https://github.com/daleharvey
Help?
Check out the mobile-couchbase mailing list or contact me on github.-
mandric posted this