Thursday 12 April 2012

Installing a Processing sketch on your Android device

This year, my students and I have been learning the basics of programming using a programming language called Processing (see previous entry).  One of the great features of working with Processing is that you can run your programs on a variety of platforms, including your handheld devices.  After just a couple of weeks of playing around, I was able to create a program that ran on my Android phone.  I have been using my kids as the target audience for my creations so i haven't thought much about the details, such as customizing the icon or how to upload the app to Google Play (aka the Android Market).

I thought I would share the code I used to create the Android installer in case anyone is interested.  I got it from here but his instructions are for Windows and I am currently using a Mac.  I had to modify a couple of steps.  You can find more thorough explanations on the Processing wiki here and here is a post that goes through uploading your project to the Android market.  I haven't tried it yet.

You will need to install the Java Development Kit (JDK) and the Android Software Development Kit (SDK) before we get started.  These include the following tools, which we will be using in the process:

1) You will be running the following commands from a terminal:

keytool, jarsigner, zipalign, ant 

If you get errors along the way, make sure that you can run these commands from a terminal.  You may need to download ant separately.  The others should have been installed with JDK and the Android SDK.

I created a simple project called 'test01'.  You can just replace test01 with the name of your sketch and the name (ben) and path (benjamincooperman) to yours.  It makes things easier if there are no spaces in the name.  Here's what I did:

1) By default, new projects are in 'standard' mode.  At the top right, you will see 'standard' in a little box.  Change it to 'Android'.  You may have trouble if your project includes any additional libraries, such as the Minim library for using sounds.  There is probably an Android library that does the same thing but you will need to research that.  In one case, I was using Minim for sound, but as an Android project, I had to use one called 'apwidgets'. The nice folks on the Processing forum helped me work out how to switch libraries and you can check out the thread here.

2)  Export your Android project by going to file > Export Android Project.  You will see that there is an option to export as a signed package but that feature has not been implemented yet so these instructions should work for you in the meantime.

2) Open a terminal and browse to the android folder inside your sketch folder.  On a Mac, the easiest way to do this is to type 'cd ' then locate the android folder in your sketch folder and click and drag it onto the terminal.  (mine was: /Users/benjamincooperman/Documents/Processing/test01/android)  It will look something like this:

cd /Users/benjamincooperman/Documents/Processing/test01/android

3) Next, type the following into the terminal and hit enter:

keytool -genkey -v -keystore test01-release-key.keystore -alias ben -keyalg RSA -keysize 2048 -validity 10000

It will ask for a password.  Put something in. It will ask you to re-type the password.  Then, there will be a series of questions.  I answered the first one (what is your first and last name) with just my first (ben) and then I ignored the rest of the questions and just hit enter until I got to:

Is CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct?

Type 'yes' and hit enter  and enter again to enter the password you already used.

4) Type the following into the terminal, hit enter and wait a bit for it to finish:

ant release

5)  Then, type the following and hit enter after which it will ask you for the password from step 3.

jarsigner -verbose -keystore test01-release-key.keystore /Users/benjamincooperman/Documents/Processing/test01/android/bin/test01-release-unsigned.apk ben

6)  Next, type:

jarsigner -verify /Users/benjamincooperman/Documents/Processing/test01/android/bin/test01-release-unsigned.apk

It should say 'jar verified'.

7)   Next:

/Users/benjamincooperman/Documents/Processing/test01/android/zipalign -v 4 /Users/benjamincooperman/Documents/Processing/test01/android/bin/test01-release-unsigned.apk test01.apk

If all went well, it should say 'Verification successful' and you will now have a test01.apk file in the android folder that you can install on your phone.  Most file managers that you find in the Android Market allow you to install software.  Just be sure to have have your phone set up so that you can install from unknown sources.  You can find that under Development Settings.

I tried to make this as clear and simple as possible since these are the same instructions that I am giving to my students.  If you see any errors or you have suggestions, please let me know.

5 comments:

  1. I don't have a /zipalign folder in the android folder (no such file or directory). I got to the point where it says jar verified.

    ReplyDelete
  2. zipalign will be a file, not a folder. It's an application that optimizes the way that your sketch is packaged. There a pretty good explanation about what it is and what it does here:

    http://www.addictivetips.com/mobile/what-is-zipalign-in-android-and-how-it-works-complete-guide/

    But if it is not appearing in your sketch folder where it is supposed to be, don't worry. You can just go to the download folder where you downloaded the Android SDK and grab a copy of it and paste it in that spot. If this is the case, the next time you export a sketch, you may have to do it again, so remember where there is a copy of zipalign on your computer.

    Please write back and let me know if it works out. Sorry I took a while to respond. I should be updating more regularly now.

    ReplyDelete
  3. My mobile android proccesing stoped

    ReplyDelete
  4. I have also integrated an Arduino sketch with my Processing sketch. Basically its reading an RFID tag from Arduino and giving the input to Processing. Is it still possible to install my Processing sketch on my android phone or i have to code it seperately on Android without using Processing.

    ReplyDelete
  5. The new versions of Processing don't seem to be playing well with Android. I had to downgrade to an older version (2.0.3) and using the Android SDK build-tools release 10. It was a bit of a pain to tell the truth. From what I see on their wiki, there is no one actively working on these issues either. Shame.

    ReplyDelete

How to cut down a tree