This chapter explains the ways to package and distribute scripts using Eclipse and QR codes.
Most Android devices include a native barcode scanner. SL4A supports importing QR codes directly into the editor. A QR code can only encode 4,296 characters of content. Hence, this method of distribution is suitable for short scripts.
There are several websites where you can paste text and create a QR code. However, we shall reference http://zxing.appspot.com/generator to generate the QR code in our illustration.
The steps are explained below −
Step 1 − Open the Contents drop-down and choose Text.
Step 2 − On the first line of the Text Content, enter the name of the script (e.g., greet.py).
Step 3 − Paste the script content below that.
Step 4 − Choose the barcode size from the Size drop-down.
Step 5 − Click Generate.
Step 6 − Embed the resulting barcode image or share it.
Thus, QR code is an effective way to share short scripts through a blog or a website.
Android applications are distributed in a single file/package with an .apk extension. The Android Package (.apk) is similar to a .jar or .zip file. Each .apk contains a number of mandatory files that must be present. The most important file is the AndroidManifest.xml.
The manifest file does a number of things −
Declares application components.
Identify user permissions the application requires.
Declares hardware and software features used or required by the application, such as camera, Bluetooth services etc.
Specifies API libraries the application needs to be linked against, such as the Google Maps Library.
The steps to build a distributable project i.e. .apk in eclipse are illustrated below −
Download and install Hg from mercurial − http://mercurial.selenic.com/wiki/Download. Set the PATH variable to the installation folder.
Create a clone of the latest SL4A repository: Open the terminal and navigate to the directory where the clone must be created. Enter the following Hg clone: https://android-scripting.googlecode.com/hg/android-scripting.
Open Eclipse and click on File/Import/General/Existing Projects into Workspace/Next and Browse to the cloned directory. Click on Android/OK then select all and click Finish.
Include ANDROID_SDK in the Classpath Variable list by clicking Windows/Preferences/Java/BuildPathVariables/New. Put ANDROID_SDK for the name and your SDK directory for the folder (probably c:\ProgramFiles\Android\android-sdk-windows).
Click Project/Build Automatically, then Project/Clean/Clean all Projects/OK.
To turn your script into an APK, make a copy of ScriptForAndroidTemplate (right click/copy then right click/paste into the same area). A new project will appear with the name Copy of ScriptForAndroidTemplate.
To connect this project to your clone of SL4A, double click on it, right click on build.xml /Run As /Ant Build. Rename the project using Refactor/Rename to whatever name you choose for your project and Refresh/Clean/Build the project.
Next, double-click on raw and Refactor/Rename to change the name script.py to your_script_name.py and then double-click src/com.dummy.fooforandroid/Script.java and change R.raw.script to R.raw.your_script_name and save.
Double-click on src and Refactor/Rename to change the package name com.dummy.fooforandroid to your.package.name. Do the same for gen.
Now highlight your project then click on Project/Properties/Android. Select your android target and click OK.
Towards the bottom of your project list, double click on AndroidManifest.xml. Change the package name in the manifest from com.dummy.fooforandroid to your.package.name. Change your minSdkVersion from "4" to suit your android target (this is the API level for your android target shown in Project/Preferences/Android). Uncomment the permissions you require your application to have (take off <!-- at the start and --> at the end). Set Debug to False.
Right click on your_script_name and open with text editor. Delete the default script, replace it with your own and save. Then Clean/Build/Refresh your project and run it. If all goes well click on File/Export to export your application as an apk.
Click Next.
Every Android Application must be digitally signed before it can be installed. Select Create new Keystore, if this is the first time you have been through this process.
Select a file to hold your keystore and it must be password protected.
Click Next. Notice the validity field. You may create a key valid for any number of years, from 1 to 99.
The final dialog box allows you to specify where you want the .apk file to reside. Click Finish.
One may use Eclipse or the ADB tool to test/install the .apk file. To install using ADB, open a terminal window, navigate to the .apk destination directory and type the following −
adb install distributable_file.apk