Android - Processing
Android - Processing
http://wiki.processing.org/w/Android
Android
From Processing
Contents
1 About Processing for Android 2 Instructions 3 Download 4 Android Mode 5 Troubleshooting, Discussion, and Known Issues 5.1 Common problems 5.2 Running on a Device 6 Developing for Android 7 Understanding changes to processing.core 7.1 Mouse, Motion, Keys, and Input 7.2 Screen, Orientation, and the size() command 7.3 API Changed, Gone, or Forgotten 8 Loading files, connecting to the internet, and permissions 9 What about Multi-touch, GPS, SMS, Compass? 9.1 Examples 9.2 Contributed Examples 10 Using Eclipse for Development 11 Integrating with other Android code 12 Distributing Apps 12.1 Signing 12.2 Icons 12.3 AndroidManifest.xml
1 of 13
03.06.2012 06:51
Android - Processing
http://wiki.processing.org/w/Android
Instructions
Download and install the Android SDK from here (http://developer.android.com /sdk/index.html) . You do not need the ADT Plugin for Eclipse. However when looking at the Available packages (see the installation guide (http://developer.android.com /sdk/installing.html) ), make sure the following are also installed: Underneath Tools, check the box for Android SDK Platform-tools. The Android SDK Tools item should already be installed. Beneath Android 2.2 (API 8), select SDK Platform and Google APIs by Google Inc. At the right is an image of what it looks like on Mac OS X (current as of November 2011). If any of the items above are missing from the list, use the checkboxes next to Show at the bottom of the window. On Windows, it looks like this (http://wiki.processing.org/images/d/da/Android-sdk-manager-15-windows.png) , and you should also install the Google USB Driver Package under Extras. Linux users also need the USB Driver Package. You can install other versions of the Android API as well, but you must include version 8. You can't install API 10 or 13 or 14 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. Once the Android SDK is installed, use the download links below to grab a version of Processing that includes the new Android mode. In keeping with the spirit of the Processing project, we hope to remove the Android SDK installation step from the download/install process (Issue 203 (http://code.google.com/p/processing/issues/detail?id=203) ). We also hope to no longer require installation of the Google APIs, since we're not using them (Issue 613 (http://code.google.com/p/processing/issues/detail?id=613) ).
Download
These are pre-release versions of the Processing code. When it's ready for prime time, the Android components will be available as a separate download, or added to the main download. Due to significant SDK changes by Google, Processing 2.0a3 requires Android SDK Tools revision 15 (or later).
2 of 13
03.06.2012 06:51
Android - Processing
http://wiki.processing.org/w/Android
Please do not use earlier releases of Processing for Android. Help us out by using the most current release and filing a bug (http://code.google.com/p/processing/issues/entry) if you run into a problem. Do not use the forums to post bug reports, I cannot fix bugs if they only show up on the forum, and I cannot track bugs that are posted there. Note that due to major changes by Google to the Android dev tools, Android mode in Processing 1.5.x no longer works. You must use 2.0a4 for Android development. 2011 11 10 | 2.0 alpha 4 | Windows standard (http://processing.googlecode.com/files/processing-2.0a4windows.zip) or without Java (http://processing.googlecode.com/files/processing-2.0a4-windows-expert.zip) | Mac OS X (http://processing.googlecode.com/files/processing-2.0a4-macosx.zip) | Linux x86 (http://processing.googlecode.com/files/processing-2.0a4-linux.tgz) 2011 11 05 | 2.0 alpha 3 | Windows standard (http://processing.googlecode.com/files/processing-2.0a3windows.zip) or without Java (http://processing.googlecode.com/files/processing-2.0a3-windows-expert.zip) | Mac OS X (http://processing.googlecode.com/files/processing-2.0a3-macosx.zip) | Linux x86 (http://processing.googlecode.com/files/processing-2.0a3-linux.tgz) Read the revisions.txt (http://processing.googlecode.com/svn/trunk/processing/build/shared/revisions.txt) document in the download to find out what's new and old in this release.
Android Mode
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. Export Android Project - creates an 'android' folder that contains the files necessary to build an APK using Ant. Export Signed Package - same as export, but creates a signed version of the 'release' build for the market. (not yet implemented, coming soon (http://code.google.com/p/processing/issues/detail?id=222) ) (As of 0193, these are thankfully no longer called Run, Present, Export, and Export Application.)
03.06.2012 06:51
Android - Processing
http://wiki.processing.org/w/Android
Component-Android, so that it's routed to the correct location. Please report issues: as much as I'd like to sit over the should of you and the other 30,000 people who will do battle with Processing this week, current laws of physics disqualify me. If it's not in the bugs database, I don't know about it, and it probably won't be fixed. Second, it's much easier for me to mark a bug as duplicate rather than trying to separate a new issue that's been added to an existing bug, so please just file things separately.
Common problems
If you get an error that reads Error: Android Virtual Device 'Processing-Android-7' already exists., select Android SDK & AVD Manager beneath the Processing Android menu, and delete the Processing-Android-7 AVD. It will be automatically re-created the next time you try to run a sketch.
Running on a Device
Before testing your sketch on your device, you must: Turn on "USB Debugging" on your device. Press Menu, select Applications Development, then enable USB debugging. If you are running Windows or Linux, you need additional setup (refer to this page on android.com (http://developer.android.com/guide/developing/device.html) ) to install a special USB driver. We've also had a report that Ubuntu Lucid (10.04) users may want to rename the USB config file 70-android.rules to make it work, but we've not tested that officially.
4 of 13
03.06.2012 06:51
Android - Processing
http://wiki.processing.org/w/Android
Using createFont() (instead of loadFont()) can help improve text quality, particularly with 2D rendering. Use PFont.list() to get a list of available fonts on the system, and select one of those, or include a .ttf or .otf file in the data directory of your sketch. Because all code for Android exists inside packages, any .java files included with your sketch that do not specify a package will be placed into the same package as the main class for your sketch. This breaks the "we won't touch .java files with the preprocessor" rule we have on the desktop side, but the alternatives are truly much worse. We simply don't support packages at this point on the desktop side, so don't get any weird ideas about adding package declarations to your .java files in general. If this bothers you deeply, stop taking yourself so seriously you'll probably be happier using Eclipse for development.
motionPressure is only updated when the screen is touched. So you will need to set its value to zero to avoid false-touch inputs. The mouseButton variable is not available, for perhaps obvious reasons. Watch out for rollovers. While commonly used in mouse-based interfaces, they don't make any sense for touch interfaces. Lots of sketches (most of them, really) will need to be rewritten for use on Android to make better use of the affordances given by the hardware. To use the virtual keyboard, hold down the MENU key on the device. This is an Android default, but some users might not be familiar with it. The 'back' key works similar to ESC in the desktop version of Processing. (Pressing ESC in the emulator actually emulates hitting the 'back' key, though it may beep at you first.) By default, this will exit your application. If you want to do something smarter, override keyPressed() and set 'key' to 0 so that the key doesn't get picked up by the base class:
void keyPressed() { // doing other things here, and then: if (key == CODED && keyCode == KeyEvent.KEYCODE_BACK) { keyCode = 0; // don't quit by default } }
5 of 13
03.06.2012 06:51
Android - Processing
http://wiki.processing.org/w/Android
The keyEvent and mouseEvent variables are not available. If you want to work with event objects, you'll need to override the key and motion handler. Because onTouchEvent() is called inside the SurfaceView, you'll need to override the Processing-specific touch handler:
public boolean surfaceTouchEvent(MotionEvent event) { // your code here // if you want the variables for motionX/motionY, mouseX/mouseY etc. // to work properly, you'll need to call super.surfaceTouchEvent(). return super.surfaceTouchEvent(event); }
You can use a handler like this to incorporate other libraries that support multitouch or fancier touch event handling than just the basics we currently provide. The key methods work in a similar fashion:
public boolean surfaceKeyDown(int code, KeyEvent event) { return super.surfaceKeyDown(code, event); } public boolean surfaceKeyUp(int code, KeyEvent event) { return super.surfaceKeyDown(code, event); }
These three functions behave a bit differently than their counterparts in java.awt, so make sure you know what you're doing before messing with them. The reason that the variables are not kept around is because the KeyEvent and MotionEvent objects are reused, which means they can't be reused without an expensive clone operation. And because it's so rare that they're used in Processing code, it's better to simply remove them and let advanced users who would otherwise rely on the events to handle the overrides themselves. Constants for ALT, CONTROL, and SHIFT are not present. SHIFT may come back, but the other two don't exist on Android. The LEFT, RIGHT, UP and DOWN constants are mapped to the directional pad. To get the center button on the d-pad, use DPAD (added in revision 0190). Keep in mind that the meaning of UP, DOWN, LEFT and RIGHT can feel different based on the hardware: it might be a trackball, or an optical pad, or even hard buttons.
void keyPressed() { if (key == CODED) { if (keyCode == DPAD) { // user pressed the center button on the d-pad } else if (keyCode == UP) { // user triggered 'up' on the d-pad } else if (keyCode == DOWN) { // user triggered 'down' on the d-pad } else if (keyCode == LEFT) { // user triggered 'left' on the d-pad } else if (keyCode == RIGHT) { // user triggered 'right' on the d-pad } } }
Starting with revision 0190, new constants for the BACK and MENU keys are available, as seen in this example:
6 of 13
03.06.2012 06:51
Android - Processing
http://wiki.processing.org/w/Android
void keyPressed() { if (key == CODED) { if (keyCode == BACK) { // do something here for the back button behavior // you'll need to set keyCode to 0 if you want to prevent quitting (see above) } else if (keyCode == MENU) { // user hit the menu key, take action } } }
Other key codes can be found in the Android documentation for KeyEvent (http://developer.android.com /reference/android/view/KeyEvent.html) . We're including what we feel are the essentials, so as not to pollute the namespace too much. To use one of those constants, you'd do something like the following, replacing KEYCODE_HOME with whatever other key code you wish. For instance, you might want KEYCODE_VOLUME_DOWN, KEYCODE_VOLUME_UP, KEYCODE_CAMERA, etc.
void keyPressed() { // doing other things here, and then: if (key == CODED && keyCode == KeyEvent.KEYCODE_HOME) { // override the home key here } }
Android is even more particular about the "no variables in the size() method" rule found in the desktop version. This is because the size() method is actually removed from the sketch. Behind the scenes, source like:
void setup() { size(screenWidth, screenHeight, P3D); }
will be removed from the source when it's preprocessed, and turned into:
void setup() { } public int sketchWidth() { return screenWidth;
7 of 13
03.06.2012 06:51
Android - Processing
http://wiki.processing.org/w/Android
} public int sketchHeight() { return sketchHeight; } public String sketchRenderer() { return P3D; }
Basically, we opted not to duplicate the (error-prone) dark magic used in the desktop version to change the sketch size mid-setup. There are only two renderers on Android. If you specify P2D or JAVA2D, that simply uses the built-in Android 2D renderer. Similarly, both P3D and OPENGL specify 3D graphics rendered OpenGL. In previous releases we also had A2D and A3D (which were aliases to the same 2D and 3D renderers), but those only create confusion. In the lead-up to 2.0 (revision 0198 and later), those constants have been removed. Because the size() command is handled by the preprocessor, Eclipse users will have to write sketchWidth(), sketchHeight(), and sketchRenderer() methods if they want to control the size. In most cases, you shouldn't need to mess with the size, but you may want to override the sketchRenderer() method so that you can use P3D. If you're more capable with Eclipse than reading comprehension, the previous points boil down to: When using Eclipse, 1) never call size(), 2) don't try to specify dimensions via sketchWidth/Height methods, and 3) if you want to use 3D instead of 2D, add this single method to your sketch:
public String sketchRenderer() { return P3D; }
Including both size() and the sketchXxxx() methods in a sketch will cause an error. Don't do that. Use screenWidth and screenHeight instead of screen.width and screen.height. Since Processing 1.2, both variables have been added to the desktop version of processing.core, where the screen variable has been deprecated. Aside from this being more consistent with the rest of the Processing API, the java.awt.Dimension type that's used for screen is not present on Android. The screen variable was something that was being used internally and then others began to use it so we felt compelled to document it. Not a terribly well thought out decision. Use keypad 7 or keypad 9 to rotate the emulator one direction or another. Or if you don't have a keypad or numlock, like you're using a laptop from the all-knowing geniuses from Cupertino, then press Ctrl-F12 (Ctrl-Fn-F12 on OS X) to rotate the screen. If you want to lock orientation to one direction, you can use one of the following commands inside setup(). These are new as of revision 0190.
orientation(PORTRAIT); // the hamburger way orientation(LANDSCAPE); // the hot dog way
We considered using size() as a hint for whether the emulator should open in portrait or landscape mode, but decided against it because it 1) added additional complexity, and 2) there wasn't a good way to control what actually happened one you're started up. Would it lock in one orientation? Hm, too clever.
8 of 13
03.06.2012 06:51
Android - Processing
http://wiki.processing.org/w/Android
Instead, we're keeping it simple and we'll play it by ear. Screen density and pixels/dots per inch can vary greatly between devices. If you need specific information about density to make your application work, you can use the DisplayMetrics class from android.util. Use the following to get the density and densityDpi for the sketch:
// Place this at the top of your sketch import android.util.DisplayMetrics; // Place this inside your setup() method DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); float density = dm.density; int densityDpi = dm.densityDpi; println("density is " + density); println("densityDpi is " + densityDpi);
The Android documentation describes the 'density' value like so: This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc. The densityDpi value is simply 160 on a 160dpi screen, and you can guess what it is on a 240dpi screen. More can be found in the DisplayMetrics (http://developer.android.com/reference/android /util/DisplayMetrics.html) documentation. The OpenGL renderer is built-in on Android, no need to use 'import processing.opengl.*;' at the beginning of your sketch. The preprocessor will actually remove that text since it would otherwise cause a conflict. This is handled by the preprocessor so that users can quickly move between desktop and Android versions without needing to incessantly comment/uncomment the import line.
9 of 13
03.06.2012 06:51
Android - Processing
http://wiki.processing.org/w/Android
Registering methods (registerPost(), registerDraw(), etc) is not supported. Too much overhead for too little benefit. We'll need to bring these back in a future release. All cursor-related methods have been removed. (They don't make sense in a touch-controlled interface.) The all-lowercase version of arraycopy() (deprecated from the desktop version) has been removed. The (long since) deprecated openStream() method has been removed. Use createInput(). PImage.image is PImage.bitmap, to reflect that a different object type is there, in spite of its identical purpose. PFont.font is PFont.typeface, similar as above. The 'online' variable is not available. It's for applets to determine whether they're running online or not, which doesn't make sense on Android, where there's no such thing as an applet.
Examples
In release 0193, examples have been added for the Compass and Accelerometer. These are very basic (and need additional improvements and API tweaks), but should be helpful for getting started.
Contributed Examples
From Rolf van Gelder, NL (http://cage.nl)
10 of 13
03.06.2012 06:51
Android - Processing
http://wiki.processing.org/w/Android
How to read the GPS (http://cagewebdev.com/android/gps_example.zip) , Feb 2011 How to read the compass (http://cagewebdev.com/android/compass_example.zip) , Mar 2011 From Eric Pavey (http://www.akeric.com) Interacting via multitouch (http://www.akeric.com/blog/?p=1435) , Mar 2011 Accessing the camera (http://www.akeric.com/blog/?p=1342) , Mar 2011 Making the phone vibrate (http://www.akeric.com/blog/?p=1334) , Mar 2011 Accessing the sensors (http://www.akeric.com/blog/?p=1313) (accelerometer, magnetic field, etc.), Mar 2011
11 of 13
03.06.2012 06:51
Android - Processing
http://wiki.processing.org/w/Android
To fix the issue above, the preprocessor would have to instead detect whether using 2D or 3D, and then subclass PApplet2D and PApplet3D (or something along those lines) at compile time. This is a significant departure from the desktop version (and the current implementation) which allows the user to dynamically specify the renderer. Similarly, for implementing wallpapers or widgets, they also have a different base class, which would mean additional duplication. These issues are not insurmountable, since we could heavily refactor the PApplet code so that the Activity class is a much thinner layer, and to make a series of base classes that handle 2D, 3D, Wallpaper, and Widget. However, this is a very large undertaking, and is a huge departure from the desktop version, which means a lot more code for us to maintain. Where us is a couple people working (largely) in their free time. So for the time being, we've made the decision to get Android up and running for the vast majority of Processing users first, and once that's reached a state where we're happy with it, we can address these issues in a future release.
Distributing Apps
It's a lot more fun to share your applications with other people. I'll build out this section as we clear up the features that make it easier to publish your apps on the market (or to make them available yourself from a web page). You might want to read Google's guidelines on Preparing to Publish (http://developer.android.com/guide /publishing/preparing.html) .
Signing
To publish with the market, your application must be signed. At the moment, you can Export your code, and self-sign it by following these instructions (http://developer.android.com/guide/publishing/app-signing.html) from Google. We're also working on an easier method (http://code.google.com/p/processing/issues/detail?id=222) to do this from within the PDE. A great, clear instruction on signing and deploying your app you can find here (http://www.akeric.com /blog/?p=1352) ! (added by RvG)
Icons
Starting with revision 0190, it's possible to specify an icon that goes with your sketch. Actually Android wants lots of icons, which cover the varying screen resolutions where your app might show up, and the different contexts (launcher icons or a long list) where your app might be used. All sketches use a default icon unless you specify otherwise. To create your own, save images as icon-36.png, icon-48.png, and icon-72.png. These should be 36x36, 48x48, and 72x72 pixel icons. Place them in the sketch folder (not the data folder or any other subfolder). The next time you build your app, these will be used instead of the defaults. Your icons shouldn't just be images that use the full area of the icon. See the design guidelines (http://developer.android.com/guide/practices/ui_guidelines/icon_design.html) for information on how to size, color, and shade your icons.
12 of 13
03.06.2012 06:51
Android - Processing
http://wiki.processing.org/w/Android
AndroidManifest.xml
Starting with revision 0190, Android sketches have an AndroidManifest.xml file in their sketch folder. This is manipulated by the various options that control permissions, naming of the sketch, etc. You can also make edits yourself, but keep in mind that breaking the manifest file can cause a lot of trouble. If you start making changes, be sure to back up your previous file first. Also, be sure to modify the copy in your sketch folder, not the file created by using Export or Export to Application. Those will be replaced next time you use the Export function. In some cases, you may want to make tweaks to the exported folder, but be sure to move it to a new location first so that you don't inadvertently lose changes when using the Export command again. Retrieved from "http://wiki.processing.org/w/Android" This page was last modified on 25 January 2012, at 16:15. Content is available under Attribution-Noncommercial-Share Alike 3.0 Unported. Powered by MediaWiki
13 of 13
03.06.2012 06:51