Saturday, September 29, 2012

Building an .apk file from your Processing sketch on OSX in order to publish it in Google Play


Building a comercial app for Android OS.
Processing is capable of run the sketch directly on device, however, it doesn't generate the file with a commercial format, that is a signed .apk. In order to get that file, there is a list of procedures to make.

Preparing the sketch
Create icons
It's indicated that you create the icons that will appear when a customer install the application. To get that, you should create three versions of the same icon with different resolutions in png (36x36px, 48x48px and 72x72px), name them as "icon-36.png", "icon-48.pg" and "icon-72.png" and place them inside the sketch folder (not data folder or any other subfolder).

Exporting the sketch
With the sketch opened in Processing in Android Mode, go to File > Export Android Project. It wil create an "android" folder inside the sketch folder with the project.
I had problems exporting with the 2.0 beta version of Processing. I used the version 2.0a.6.

Signing
Installing Tools
The programs needed are Apache Ant, Java Development for OSX, MacPorts, OpenSSL and Android SDK.

Install Apache Ant, that could be download there:  http://ant.apache.org/bindownload.cgi
Extract the archive and rename to "ant". Open Terminal and browse to ant folder. Use the command below to move the folder to ".usr/local":

mv ant /usr/local

Next, type the following command:

cd /usr/local
ln -s ant ant

Open the archive "bashrc"inside "/etc" with the command

sudo pico bashrc

add the following lines in the end of the text

export ANT_HOME=/usr/local/ant
export PATH=${PATH}:${ANT_HOME}/bin

Press control+x to exit and save the file without rename it.
Close Terminal and open it again. Type

ant

If the message “build.xml does not exist” appears, the process is successful.

Install Java Development for OSX https://developer.apple.com/downloads/index.action (mine was Java for OSX 2012-005 Developer Package)

Install MacPorts (http://www.macports.org/install.php).
Use MacPorts to install OpenSSL with the following command:

sudo port sync; sudo port selfupdate; sudo port install openssl


If you don't have the Android SDK installed, Install Android SDK: download this file and extract it into a safe place:  http://developer.android.com/sdk/index.html. Go to the folder "tools" and open the file "android". Next, download the packages. I suggest you download everything you could.


Create the secret key
Open Terminal and browse to the folder in which the Android project were exported.
Create the secret key in order to sign the app.
Type the syntax below replacing the data inside brackets, including the brackets "{}"with the actual names and paths.

keytool -genkey -v -keystore {sketchName}-release-key.keystore -alias {yourName} -keyalg RSA -keysize 2048 -validity 10000

It will be made various questions like your name and last name, city, etc. Fill everithing and type return everytime. When it's done, the following message will be showed [Storing {sketchName}-release-key.keystore], and the file will be stored inside the android folder.

Build an unsigned .apk file
Open Terminal, browse to the android folder and type

ant release

When the build is complete, the message "Build Successful"will be showed and the folde /bin with the unsigned apk file will be created inside the folder "android".
Sign the apk file with the secret key replacing the data inside brackets, and the brackets, with actual data

jarsigner -verbose -keystore {sketchName}-release-key.keystore {complete/path/to/sketchFolder/android/bin/sketchName-unsigned.apk} {Your name (that you defined in the secret key step)}

Hit return

It wil ask you to insert the password that you defined in the secret key step

Next, verify if jarsigner did what it had to with the following command"

jarsigner -verify {complete/path/to/sketchFolder/android/bin/sketchName-unsigned.apk}

The following message will appear:

jar verified.

Finally, build an signed apk ready to publish in Google Play with the command:

zipalign -v 4 {complete/path/to/sketchFolder/android/bin/sketchName-unsigned.apk} {sketchName}.apk

A long list of messages will appear and the message "Verification successful" will be showed
If everything worked, there will be a new file with the name {sketchName}.apk inside the "android" folder.

But, I had a problem. When I called the command "zipalign", the message "command not found" appeared.
What I did (and worked fine) is to open Terminal and browse inside the folder that you installed the Android SDK tools " /android-sdk-macosx/tools" and type the following command:

./zipalign -v 4 {complete/path/to/sketchFolder/android/bin/sketchName-unsigned.apk} {sketchName}.apk

In this case, the file {sketchName}.apk will be builded inside the "tools" folder.
This is the file ready to publish in Google Play.

Thats it! I hope you enjoy!!




No comments:

Post a Comment