Friday, June 14, 2013

how to distribute android apps created into processing (or how to create an .apk from a .pde)

I just finished diving into the apps of the students of the class (PxS personal interaction studio) that I teached this semester and I got curious to see if I can export an app developed with processing into an .apk.

Well the short answer is yes. Then it takes a while.
Following the how:

void setup() {
Make sure you have keytool, jarsigner, ant etc. commands working from your command prompt. 
If not then install them, just google for those packages. 

This reminded a bit of CERN period and GRID projects because that was when I dealt with OpenSSL. Now it's useful to generate your secret key for the application. So if you don't have OpenSSL install it.
}

Now let's create the .apk.

void draw(){
  1. Push Export from Processing's IDE
  2. Open command prompt and get to your /android folder and you will see bin, lib, res etc. 
  3. Type the following command to generate your secret key that will be required for signing your application:
$keytool -genkey -v -keystore <sktechname>-release-key.keystore -alias <anything> -keyalg RSA -keysize 2048 -validity 10000

It will then ask you the following questions:

Enter keystore password:
Re-enter new password:
What is your first and last name?
  [Unknown]:  
What is the name of your organizational unit?
  [Unknown]:  
What is the name of your organization?
  [Unknown]:  
What is the name of your City or Locality?
  [Unknown]:  
What is the name of your State or Province?
  [Unknown]:  
What is the two-letter country code for this unit?
  [Unknown]:  
Is CN=name , OU=name, O=name, L=blah, ST=PA, C=US correct?
  [no]:  yes
Generating 2,048 bit RSA key pair and self-signed certificate (SHA1withRSA) with a validity of 10,000 days
        for: CN=name, OU=name, O=name, L=blah, ST=PA, C=US
Enter key password for <your alias>
        (RETURN if same as keystore password):
Re-enter new password:
[Storing <appname>-release-key.keystore]

This creates a 
<appname>-release-key.keystore file in your /android folder.

5.Now we create the .apk file which is unsigned. So type in the following:
$ant release
........
........


BUILD SUCCESSFUL
Total time: 12 seconds
-----------------------------------------

You should get this in your console if the build is successful. 

-----------------------------------------------------------------------------------------------------------------------

6.You can now see in your bin folder the .apk file named as <appname>-unsigned.apk


7. Now you will need to sign this with your secret key in order to release it on the android market. So here goes, (you can choose to copy the keystore file into your bin folder and execute the following command (too lazy to type too many characters) or keep it where it is.


$jarsigner -verbose -keystore <appname>-release-key.keystore  <appname>-unsigned.apk <your alias>


Then you will get something like:

Enter Passphrase for keystore:
   adding: META-INF/MANIFEST.MF
   adding: META-INF/ALIAS.SF
   adding: META-INF/ALIAS.RSA

...............
.............
  signing: assets/ComicSansMS-25.vlw
  signing: assets/CurlzMT-150.vlw
  signing: assets/SegoePrint-60.vlw
  signing: res/drawable/icon.png
  signing: res/layout/main.xml
  signing: AndroidManifest.xml
  signing: resources.arsc
  signing: res/drawable-hdpi/icon.png
  signing: classes.dex

7. We need to make sure that jarsigner has signed the app correctly. Execute:

$jarsigner -verify <appname>-unsigned.apk

jar verified.

You should get JAR VERIFIED else something maybe wrong.

8.To create your signed and distributable .apk file (note that the zipalign is probably in the tools folder of the android SDK)
$zipalign -v 4 <appname>-unsigned.apk <new appname>.apk


Verifying alignment of <appname>.apk (4)...
      50 META-INF/MANIFEST.MF (OK - compressed)
     973 META-INF/ALIAS.SF (OK - compressed)
    1958 META-INF/ALIAS.RSA (OK - compressed)
    3094 assets/ComicSansMS-25.vlw (OK - compressed)
   23456 assets/CurlzMT-150.vlw (OK - compressed)
  205025 assets/SegoePrint-60.vlw (OK - compressed)
 .........
..........
 7002884 res/drawable/icon.png (OK)
 7005452 res/layout/main.xml (OK - compressed)
 7005745 AndroidManifest.xml (OK - compressed)
 7006548 resources.arsc (OK)
 7007504 res/drawable-hdpi/icon.png (OK)
 7011152 classes.dex (OK - compressed)
Verification succesful


Now you can happily distribute you android app. 
You may not able to run it on Samsung Captivate unrooted phone as it does not allow non-market app installation 

So for the next class you know how to do :) 

}

ciao Alex 

No comments:

Post a Comment