I’ve started to learn Android development reading their documentation and getting interested in the possibilities of the programming interface. But I’m not a Java guy.
I dislike it, mainly because I come from a more simpler background such as web development, where you can simply write a couple of lines of code and refresh your browser to get started doing something. That said, web development can be very complex, but debugging and testing is quite easy and straight forward to some point.
So although I will teach myself how to develop in Android, first of all I will play a little bit doing some fast sketches in Processing for Android, and I will try to do my first project: A simple screensaver.
If you are working in Linux like me, I’m on Xubuntu, you should setup things a bit before connecting to your android device. So here I list the pre-requisites.
Linux Prerequisites
Copying the instructions from developer.android.com :
In Ubuntu Linux you need to add a udev
rules file that contains a USB configuration for each type of device you want to use for development. In the rules file, each device manufacturer is identified by a unique vendor ID, as specified by the ATTR{idVendor}
property. For a list of vendor IDs, see USB Vendor IDs, below. To set up device detection on Ubuntu Linux:
-
Log in as root and create this file: /etc/udev/rules.d/51-android.rules
.Use this format to add each vendor to the file:
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", MODE="0666", GROUP="plugdev"
In this example, the vendor ID is for HTC. The MODE
assignment specifies read/write permissions, and GROUP
defines which Unix group owns the device node.
Note: The rule syntax may vary slightly depending on your environment. Consult the udev
documentation for your system as needed. For an overview of rule syntax, see this guide to writing udev rules.
-
Now execute: chmod a+r /etc/udev/rules.d/51-android.rules
Now onto the Processing side of the story
Instructions
Download and install the Android SDK from here. Instead of the default “ADT Bundle” link, click “Use an Existing IDE” toward the bottom of the page, and then click the “Download the SDK Tools” button.
After downloading, follow Google’s instructions here and then for the Adding Platforms and Packages section, do the following:
- Underneath “Tools”, check the box for “Android SDK Platform-tools”. (The “Android SDK Tools” item will already be installed.)
- Beneath “Android 2.3.3 (API 10)”, select “SDK Platform”. (As of Processing 2.0b7, you longer need to include the “Google APIs” item.)
- On Windows, select “Google USB Driver” beneath “Extras”. On Linux you don’t need to do this! (We solved that with the udev thing done before)
At the right is an image of what it looks like on Windows (current as of December 2012). If any of the items above are missing from the list, use the checkboxes next to “Show” at the bottom of the window.
64-bit Linux users should also install 32-bit compatibility libraries using something like the following on the command line:
sudo apt-get install ia32-libs
I use 64 bits Ubuntu and didn't need to do this, so there is a big chance that ia32-libs is already installed in your system.
More about platform-specific issues can be found in the links to Google’s documentation (above).
You can install other versions of the Android API as well, but you must include version 10. You can’t install API 13 or 14 or 17 just because it’s newer. Also note that these revisions are for the individual “SDK Platforms” (which refer to Android OS versions), which is different from the revision numbers (see the Rev. column in the GUI) on the “Android SDK Tools” mentioned above. Confusing? Yes.
Android Mode in Processing
To build for Android, select “Android” from the drop-down box in the toolbar (by default it reads “Standard”). This places the current editor window into Android mode. To show you that you’ve now entered a new world, the window will also get a lot more green.
The Run and Export features change a bit in Android mode:
- Run in Emulator – preprocess the current sketch, create an Android project, and run (debug) it in the Android emulator.
- Run on Device – the same as Run, but run on a device (phone) that’s attached by USB.
A sample sketch for Processing
Took this one from openprocessing.org where there are a lot of open source sketches
int num_part = 30;
Particle[] cuties = new Particle[num_part];
Boolean isSetup = false;
//
void setup() {
size(700, 400);
}
void draw() {
if (mousePressed == true) {
//this sets up all the particles
for (int i = 0; i<num_part; i++) {
cuties[i] = new Particle(mouseX, mouseY);
}
isSetup = true;//we have to use this to check if they are setup or not
}
if (isSetup == true) { //if setup, then move and display them
for (int i = 0; i<num_part; i++) {
cuties[i].update();
cuties[i].display();
}
}
}//end of draw
class Particle {
//variables that describe attributes of the particle.
float x_pos;
float y_pos;
float x_speed;
float y_speed;
int w;
Particle (int _x, int _y) {//constructor statement. It constructs the particle
x_pos = _x;
y_pos = _y;
x_speed = random(-4, 4);
y_speed = random(-4, 4);
w = int( random(2, 15) );
}//end constructor. It should only run once when you setup the particle, like the setup() function
//
void update() {//i use update to do math. In this case move the particle
x_pos = x_pos + x_speed;
y_pos = y_pos + y_speed;
y_speed += .1;//this makes it behave like gravity.
}//end update
void display() {//if you wanted to draw a different shape, this is where you would do it
ellipse(x_pos, y_pos, w, w);
}//display
}//end of particle