Processing Android – Signing an Android app

How to sign an Android app, generated with Processing Android (or generated with another tool)?

Create a (Windows-) batch file (.bat) with the following code:

@echo off
ECHO.
set /p varSketchName="Please enter the Name of your sketch: "
ECHO.
set /p varSketchAlias="Please enter an Alias for your sketch: "
ECHO.
ECHO [KEYTOOL]
keytool -genkey -v -keystore %~dp0%varSketchName%-release-key.keystore -alias %varSketchAlias% -keyalg RSA -keysize 2048 -validity 10000
pause
ECHO [ANT RELEASE]
call ant release
pause
ECHO [JARSIGNER]
call jarsigner -verbose -keystore %~dp0%varSketchName%-release-key.keystore %~dp0bin\%varSketchName%-release-unsigned.apk %varSketchAlias%
pause
ECHO [JARSIGNER VERIFY]
call jarsigner -verify %~dp0bin\%varSketchName%-release-unsigned.apk
pause
ECHO [ZIPALIGN]
set /p varSignedAppName="Please enter name for final signed apk (w/o .apk extension): "
call zipalign -v 4 %~dp0bin\%varSketchName%-release-unsigned.apk %~dp0%varSignedAppName%.apk

Programs you need:

  • keytool.exe (part of the Java JDK)
  • ant.bat (part of Apache ant)
  • jarsigner.exe (part of the Java JDK)
  • zipalign.exe (part of the Android SDK)

Steps:

  1. In Processing: create your sketch in Android Mode
  2. Export the sketch as an Android Project (Ctrl-Shift-E)
  3. Copy the .bat-file above to the newly created android directory and run it
  4. Name of your sketch = name of the .pde-file (without .pde!)
  5. Alias = same as the name (or something else)
  6. Enter a password for your keystore
  7. Enter your personal data
  8. Enter the same keystore password again
  9. Enter the name of the final .apk-file (without the .apk extension!)

N.B. Make sure your folder names DON’T include any SPACES! That will break the batch file.

And there you go! You’ve got a signed .apk file!


Processing – How to find out if a sketch is in Java or Processing.js mode

A clever trick to find out if a sketch is currently running on a PC (Java mode) or in a browser (Processing.js)

// IS THIS INSTANCE RUNNING IN PROCESSING.JS OR PROCESSING JAVA?
boolean processingJS = true;
void setup()
{
   // DETERMINE IF THIS SKETCH IS RUNNING PROCESSING.JS OR PROCESSING JAVA MODE
   // PROCESSING.JS DOESN'T KNOW THE displayWidth VARIABLE AND WILL JUMP
   // TO THE EXCEPTION
   try {
       int dummy = displayWidth;
       processingJS = false;
   }
  catch (Exception e)
  {
  }
}