Android Adventures, part 6: Building a signed .apk from Processing

Go to part 5…, part 7…

Processing makes it really easy to “Present” a sketch to your Android phone via it’s “Present” button.  This creates a special ‘debug’ apk that can run on your specific device, but isn’t distributable (and I can’t even find it on the phone) to others.    There is no easy way (yet) to get these sketches to other users via the apk format.  But thanks to the great user community (and one specific post in question) I was able to get a signed apk built and installed on my phone.  Follow-up post will expose this apk to the world 😉

How to do it?

First Export the sketch.  Then Sign the Sketch.  Sounds easy.  The first part is… 😉  Below is the process I took to get my “floater08″ sketch “apk’ed”, so it’s what I’ll use as an example:

Part 1:  Export the sketch

  • From the Processing PDE, open the sketch (go into ‘Android -> Android Mode’), and press the “Export” button (arrow facing to right).  This will export the android data and put it in an \export folder under the sketch:
  • C:\sketchbook\floater08\android

That’s the easy part.

Part 2:  Signing the sketch

Docs:

The Android-Processing wiki (under the ‘Distributing Apps’ section) links to these Android Docs for signing an APK.  But I followed the steps on this incredibly helpful post (the ones by user ‘ckamath‘ specifically) to get me going.  Below is an overview of my process working with this information.

Install Tools:

Like the post says, make sure you can run these commands from a shell: keytool, jarsigner, zipalign, ant.  I didn’t have ant, so I installed it from here, following these steps (specifically everything it says to do under “The Short Story” & “Setup”).  jarsigner and keytool are provided via the JDK which I’d already installed.

Like the post says, I also (apparently) needed cygwin which provides OpenSSL.  After downloading and running the setup.exe cygwin installer, I figured out I needed to filter the install list for ‘openssl’, and then I needed to select each entry for install.  It was all a bit clunky and cryptic, and the installer seemed to crash a couple times.  But it did install (with a bunch of other stuff too apparently).

Run Code:

These are my steps, which more or less follow ckamath’s tutorial in the above post:

  • Open a shell.
  • Browse to the \android folder where your export data is.  In my case that was:
    • C:\sketchbook\floater08\android
    • In the below examples, this is the presumed path from the shell that the code is being entered from.
  • A: Make your secret key!
  • Create the ‘secret key’ that is required for signing your application:
    • Here is a template of what to type:
      • \android>keytool -genkey -v -keystore <SKETCHNAME>-release-key.keystore -alias <YOURNAME> -keyalg RSA -keysize 2048 -validity 10000
    • This is what I typed:
      • \android>keytool -genkey -v -keystore floater08-release-key.keystore -alias AKEric -keyalg RSA -keysize 2048 -validity 10000
  • It will then ask you several question like “What is your first and last name?”, What is the name of your City or Locality?”, etc.
  • When its done (successfully) it will finish by printing:
    • [Storing floater08-release-key.keystore]
  • And will store the corresponding file:  \android\floater08-release-key.keystore
  • B: Create ‘unsigned .apk’
  • Execute ant to build the unsiged apk:
    • \android>ant release
  • This should spit out a bunch of stuff in the shell.  When complete you should see something like:
    • BUILD SUCCESSFUL
      Total time: 18 seconds
  • If you didn’t have one before, you should now have a \bin folder with the unsigned apk in it:
    • C:\sketchbook\floater08\android\bin\floater08-unsigned.apk
  • C: Sign the unsigned .apk with the secret key:
  • Here is a template of what to type:
    • \android>jarsigner -verbose -keystore <SKETCHNAME>-release-key.keystore <FULL PATH TO>\android\bin\<SKETCHNAME>-unsigned.apk <YOUR NAME FROM SECRET KEY STEP>
  • This is what I typed:
    • \android>jarsigner -verbose -keystore floater08-release-key.keystore C:\sketchbook\floater08\android\bin\floater08-unsigned.apk akeric
  • The shell should have several lines of “adding” and “signing”.
  • D: Verify that jarsigner did its thing:
  • Here is a template of what to type:
    • \android>jarsigner -verify <FULL PATH TO>\android\bin\<SKETCHNAME>-unsigned.apk
  • This is what I typed:
    • \android>jarsigner -verify C:\sketchbook\floater08\android\bin\floater08-unsigned.apk
  • If this works you should see in the shell:
    • jar verified.
  • E: Make signed (and distributable) apk file:
  • Here is a template of what to type:
    • \android>zipalign -v 4 <FULL PATH TO>\android\bin\<SKETCHNAME>-unsigned.apk   <SKETCHNAME>.apk
  • This is what I typed:
    • \android>zipalign -v 4 C:\sketchbook\floater08\android\bin\floater08-unsigned.apk   floater08.apk
  • The shell will start by saying:
    • Verifying alignment of floater08.apk (4)…
  • Print a bunch more stuff, and finally:
    • Verification successful
  • You’ll now find a <SKETCHNAME>.apk file in your \android directory:
    • \android\floater08.apk

That apk is now directly consumable by an Android device.  As a test I put it on my ftp, browsed to it from my phone, and it installed immediately.

The next step would be to get it on the Android market, but I’ll work in that in another post.  Happy Holidays!

Go to part 5…, part 7…

Android Adventures, part 5: access the camera in Processing
New Android App: Floater
  • Trackback are closed
  • Comments (39)
    • Dave
    • December 30th, 2010 11:04am

    Brilliant, thanks to you and the OP from the Processing forum. Maybe add the ” ” cos currently it reads: “floater08-unsigned.apkfloater08.apk” – anyway, thanks again, really useful post, and great a blog.

  1. Oops, thanks for catching that. Needed some spaces in there. Post updated.

    • xSus
    • February 18th, 2011 4:31pm

    These examples/tutorials/guides that you are writing for processing are GREAT… keep up the good work! :)

  2. Glad to help :)

  3. if you just usb your phone in and have followed the instructions from google’s site you adb install path/to/apk
    you do need to go to settings applications and enable debug
    anyway, your example script reveals that my optimus one doesn’t allow much multitouch… at least not as much as your phone.

  4. Hey! So Ive been all over the net trying to figure this out, but I can’t find an answer. When I go to signed the unsigned.apk, it asks for a passphrase for keystore. It doesnt immediately write out more information, like you’ve said here: “The shell should have several lines of “adding” and “signing”.

    Is there something I’m doing wrong?

  5. Unfortunately I’ve only done this processes once… when I made the tutorial. So I have little more info than what’s posted above :(
    I like your art by the way 😉

  6. @AKeric

    Aww, that’s ok. Thanks for the quick response! And thanks! 😀 You’re site has been a big help during this 6 week processing class I’m taking. Thanks for the helpful posts!

  7. Sarah,

    One if your phone is in debug mode itwill run unsigned apks

    Two, the passphrase is whatever you put in when you created the key you are signing with
    Does just pressing enter not do it?

  8. @Andrew

    Hey! Yes, my phone is in debug mode, but I’d like to release my app on the market so my classmates can download it for final critique.

    And yeah, I’ve tried just hitting enter, and it says this:

    jarsigner: you must enter key password

    and when I tried to put in the first password from creating the key, it says:

    jarsigner: unable to open jar file:

    Stumped! Thanks for the reply!

  9. GASP OMG It worked!

    I was a durr and had the location of the unsigned.apk and the .keystore files switched. Hurr! Thank you guys so much for the help though. It’s nice to know There are helpful people on the net who will actually reply to a post. :]

  10. People usually do help with tech things I’ve found and I had trouble writing the path to my key and unsigned.apktoo.

    • jm
    • July 25th, 2011 4:35am

    Nice – fantastic stuff going on .. and for android no less. Keep it up, crew!

    • Pete
    • August 1st, 2011 2:43pm

    Hey Eric!

    Thank You for this great tutorial. Most of the stuff worked fine for me. Some changes have been made for example in keytool the keygen changed to genkeypair and my mac was very picky about that.

    But now I am really lost, because the whole zipalign thing does not work. My terminal does not accept the
    -v ! The terminal answers command not found. And I tried a lot, like putting the zipalign prog into the android folder or putting the hole path to the zipalign and my unsigned application into the command.

    I dont get it. Do you have any clue?

    Greetz
    PEte

  11. Are you sure you actually have zipalign installed, and on your path? Can you go to a shell, type in :
    > zipalign
    and it recognizes the command? -v is a flag to zipalign, so if it says ‘command not found’, my guess is it’s not the -v that’s the problem, but zipalign itself.

    • Pete
    • August 3rd, 2011 11:27am

    Hey Eric!

    Thanx for your reply.

    I am not quiet sure what you mean with installed.
    I thought it was enough to go to the folder and type in the name of the application?

    Greetz Pete

  12. At the top of the post in ‘Part 2: Install tools’ it says:
    “…make sure you can run these commands from a shell: keytool, jarsigner, zipalign, ant.”

    If you run zipalign from a shell and get something like (on my Windows box):
    “‘zipalign’ is not recognized as an internal or external command, operable program or batch file.”
    It means you don’t have it installed or it’s not in your path, and you’ll need to go through the steps of installing it, or adding it to your path.

    • Colin
    • August 18th, 2011 1:30pm

    Hello,

    So i’m trying to sign the apk but I keep getting this message:
    jarsigner: unable to open jar file: “path to apk without any slashes in it”

    Not sure what I’m doing wrong.

    Any ideas.

  13. Try flipping the slashes in your path?

    • Colin
    • August 18th, 2011 6:20pm

    @AKeric
    Now’s it’s doing the same thing but with slashes in the path?

  14. I’m no expert here, it just happened to work for me on the first try so I blogged about my success :) My only guess is the path itself is wrong, or something bad happened when the jar was made. Try the processing-android forums, I’ve seen a few other solutions for signing apks listed there that people have had success with.

    • Metabog
    • September 16th, 2011 3:24pm

    I had to stop and thank you. I’ve managed to bring my first Android app from zero to developed to signed to publish, with multitouch and everything, and your blog has been almost essential, so I don’t know how I can thank you enough. Android Processing forever. \m/

    I had to do all the command line signing stuff under Ubuntu because I had no luck getting java to work on Windows, unfortunately, but it worked a charm after I got all the paths right, but the hardest part was that, getting the tools to work properly.

    Good luck with all this stuff, Processing is a goldmine.

  15. That’s awesome, and I’ll take all the warm fuzzies I can get 😛 Glad it all worked out!

    • Naman Thakar
    • October 6th, 2011 7:27pm

    Hi Eric,

    Thanks for the brilliant language and the tutorials. Very helpful.

    I’m currently stuck on step 3: signing the unsigned .apk with secret key.

    when i enter the command..: jarsigner -verbose -keystore -release-key.keystore C:\Documents and Settings\R29\My Documents\Processing\\android\bin\-unsigned.apk

    .. it asks for the passphrase to execute the command, after that i get an error:

    certificate chain not found for: and. and must reference a valid Store key entry containing a private key and corresponding public key certificate chain.

    whats wrong? please help!

    Naman Thakar

    • Naman Thakar
    • October 7th, 2011 1:24am

    figured it out: for some reason the console didnt take spaces in the name too well. so i moved the application folder out of the c:\docs & settings\bla\bla\blah to c:

    jarsigner worked, followed by the verification command, and made the signed apk successfully.

    uploaded to my website and made the QR and tested on another phone! all worked!!

    as you can guess i’m very happy, and i’d like to thank you for your Android Adventures

    • JesseScott
    • February 24th, 2012 2:33am

    Hey Eric, et al…
    Thanks again for this…

    Oh man… I just went through this whole process, and everything worked, but I CAN’T FIND MY .APK !!!

    AAAHHH!!!

    Seriously, everything worked, I had verifies the jarsigning, etc etc… and during the zipalign process all the expected text showed in my shell, but the final .apk is nowhere to be found !?!?!

    Thoughts?

    • JesseScott
    • February 24th, 2012 2:50am

    @AKeric

    FYI, for mac/Terminal users, if you get a “command not found”, add a ‘./’ before… so it looks like

    ./ zipalign -v 4 /Users/…

    • JesseScott
    • February 24th, 2012 2:54am

    @JesseScott

    I just tried to do this whole thing from my OSX partition, starting from the same .apk I jarsigned…
    I verified that the .apk is jarsigned, then ran the zipalign (see other comment for syntax note) on it, got this in the terminal:

    Jesse-Scotts-MacBook-Pro:tools jessescott$ ./zipalign -f -v 4 /Users/jessescott/Desktop/SonicDrift/android/bin/SonicDrift-unsigned.apk SonicDrift.apk
    Verifying alignment of SonicDrift.apk (4)…
    50 META-INF/MANIFEST.MF (OK – compressed)
    654 META-INF/MEMELAB.SF (OK – compressed)
    1387 META-INF/MEMELAB.RSA (OK – compressed)
    2548 assets/about.png (OK)
    30428 assets/instructions.png (OK)
    62783 assets/license.txt (OK – compressed)
    75148 res/drawable/icon.png (OK)
    77684 res/layout/main.xml (OK – compressed)
    77977 AndroidManifest.xml (OK – compressed)
    78808 resources.arsc (OK)
    79764 res/drawable-hdpi/icon.png (OK)
    84405 classes.dex (OK – compressed)
    259778 license.txt (OK – compressed)
    Verification succesful

    (Interesting to know that Google spelled ‘successful’ incorrect 😉 btw )

    But… no apk !?!

    • JesseScott
    • February 24th, 2012 3:31am

    Sorry… can’t delete comments, but I found it.
    I didn’t specify a file path, so it went to my tools folder…

    oops! 😉

    @JesseScott

    • Raghav
    • September 20th, 2012 1:16am

    If I may interfere, “zipalign” is an executable in your android sdk
    so, if you go to
    ~/…/android-sdk-linux/tools , you will find it there , so instead of using zipalign -v 4…… go to the tools directory and type ./zipalign -v 4 .. and the rest of the string

    GREAT ARTICLE THOUGH

    • Togo
    • September 20th, 2012 9:33am

    Hi Eric!

    Thanks for this post, it was extremely helpful!

    I wrote an entry on my blog for my Japanese readers, showing how to make an .apk file based on your entry.

    http://www.togogo.jp/wordpress/archives/304

    I hope you don’t mind!

  16. Sounds great, spread the love :)

  17. Hi, I followed all the steps and got the updating/signing message but when I try to verify I get:

    This jar contains entries whose certificate chain is not validated.

    Any thoughts of what could be wrong? Thanks!

    • Joshua N
    • December 18th, 2013 1:20am

    Is it bad if I can’t get past part 1? May sound stupid, but how do I export it like that? The only export button is ‘Export Signed Package’ but that’s not implemented yet…

    Cheers

  18. Hello Akeric, thank you for this tutorial, it seams to be the best on the web for this topic. After following your tutorial during a complete day, i finally get my apk ! But during the installation on device an error stoped the process… Disapointing, so i run the sketch on device from processing IDE to watch if the error was also here during this build method. I didn’t find nothing about my error but i find this :

    Running zip align on final apk…
    [echo] Debug Package: C:\Users\martin\AppData\Local\Temp\android6345855283848743886sketch\bin\sketch_141213a-debug.apk

    I copy and paste “sketch_141213a-debug.apk” on another phone to install it and it worked !
    I just want to share some experimental app on a small network, don’t need to be on the play store, so it looks to be a really easy way to do it. What’s the difference with the manual method, it’s too easy to be a good way to sign application ? do you think it can cause error or instability ?

    Thanks again

    Martin

  19. You know, it’s been so long since I’ve worked on that tech I really don’t have a good answer for you. But maybe someone else reading this will. I never put any of my stuff on the play store, just copied it directly.

    • behnood
    • December 29th, 2014 2:04am

    To anyone who has problem with jarsigner/keytool/keystore: http://stackoverflow.com/questions/13535424/android-keystore-stopped-working?lq=1
    btw, I wanted to thank Eric for his help and guidance. Happy Holidays from 4 years later! 😉

  20. Wow, it has been four years for this post. Blog: The gift that keeps on giving 😛

    • Rocio
    • May 30th, 2016 11:47am

    Thanks from Argentina! this tutorial help me to get the .apk file correctly, I´m planning to translate your tutorial to Spanish so it´s reaches more people, obviously i´ll link your page.
    Thanks, for your blog!.
    :)

Comment are closed.