android (3)
ant (2)
beautifulsoup (1)
debian (1)
decorators (1)
django (9)
dovecot (1)
encryption (1)
fix (4)
gotcha (2)
hobo (1)
htmlparser (1)
imaplib (2)
java (1)
json (2)
kerberos (2)
linux (7)
lxml (5)
markdown (4)
mechanize (6)
multiprocessing (1)
mysql (2)
nagios (2)
new_features (3)
open_source (5)
optparse (2)
parsing (1)
perl (2)
postgres (1)
preseed (1)
pxe (4)
pyqt4 (1)
python (41)
raid (1)
rails (1)
red_hat (1)
reportlab (4)
request_tracker (2)
rt (2)
ruby (1)
scala (1)
screen_scraping (7)
shell_scripting (8)
soap (1)
solaris (3)
sql (2)
sqlalchemy (2)
tips_and_tricks (1)
twitter (2)
ubuntu (1)
vmware (2)
windows (1)
zimbra (2)

Wouldn't it be nice to write android applications in Scala? And wouldn't it be even better to build these applications with ant just like can be done with Java? Well, it is possible and I've taken out all of the frustration of figuring out how for you! No Eclipse required!

We do this by altering the ant build rules in the android SDK. In the android SDK directory, replace the content of platforms/?/templates/android_rules.xml with the content linked below.

Now we have to copy a few jars to the tools/lib directory of our SDK.

Copy scala-compiler.jar and scala-library.jar from the lib directory of the Scala distribution.

Copy proguard.jar from the lib directory of the Proguard distribution. Proguard is used to create a single jar with only the class files your application uses.

We are all set to build android applications with Scala now. Let's go through one for android-2.0 to test.

Create a new android project:

$ android create project --package com.example.helloscala --activity HelloScala --target android-5 --path HelloScala

In this new project, remove src/com/example/helloscala/ and create src/com/example/helloscala/HelloScala.scala with the following contents:

package com.example.helloscala


class HelloScala extends Activity {
  override def onCreate(savedInstanceState: Bundle) {

From the root of the project run ant debug to build a test apk. Output will look similar to this:

Buildfile: build.xml
    [setup] Project Target: Android 2.0
    [setup] API level: 5
    [setup] WARNING: No minSdkVersion value set. Application will install on all Android versions.


     [echo] Creating output directories if needed...
    [mkdir] Created dir: /home/tlesmann/src/android/HelloScala/gen
    [mkdir] Created dir: /home/tlesmann/src/android/HelloScala/bin/classes

     [echo] Generating / from the resources...

     [echo] Compiling aidl files into Java classes...

    [javac] Compiling 1 source file to /home/tlesmann/src/android/HelloScala/bin/classes
   [scalac] Compiling 1 source file to /home/tlesmann/src/android/HelloScala/bin/classes

 [proguard] ProGuard, version 4.4
 [proguard] Reading program directory [/home/tlesmann/src/android/HelloScala/bin/classes]
 [proguard] Reading program directory [/home/tlesmann/src/android/HelloScala/libs]
 [proguard] Reading program jar [/home/tlesmann/src/android/android-sdk-linux/tools/lib/scala-library.jar] (filtered)
 [proguard] Reading library jar [/home/tlesmann/src/android/android-sdk-linux/platforms/android-2.0/android.jar]
 [proguard] Note: scala.xml.include.sax.Main$ calls '(org.xml.sax.EntityResolver)Class.forName(variable).newInstance()'
 [proguard] Note: there were 1 class casts of dynamically created class instances.
 [proguard]       You might consider explicitly keeping the mentioned classes and/or
 [proguard]       their implementations (using '-keep').
 [proguard] Note: You're ignoring all warnings!
 [proguard] Preparing output jar [/home/tlesmann/src/android/HelloScala/bin/classes.min.jar]
 [proguard]   Copying resources from program directory [/home/tlesmann/src/android/HelloScala/bin/classes]
 [proguard]   Copying resources from program directory [/home/tlesmann/src/android/HelloScala/libs]
 [proguard]   Copying resources from program jar [/home/tlesmann/src/android/android-sdk-linux/tools/lib/scala-library.jar] (filtered)

     [echo] Converting compiled files and external libraries into /home/tlesmann/src/android/HelloScala/bin/classes.dex...

     [echo] Packaging resources
 [aaptexec] Creating full resource package...

[apkbuilder] Creating HelloScala-debug-unaligned.apk and signing it with a debug key...
[apkbuilder] Using keystore: /home/tlesmann/.android/debug.keystore

     [echo] Running zip align on final apk...
     [echo] Debug Package: /home/tlesmann/src/android/HelloScala/bin/HelloScala-debug.apk

Total time: 10 seconds

You should take notice that ant still builds java, the XML generated sources in the case. Let's try out our new apk. If you need to create an avd for the android 2.0 platform, do so with the following command:

$ android create avd --target android-5 --name android-5

And start it up:

$ emulator -avd android-5

Finally, install your apk:

$ adb install bin/HelloScala-debug.apk

You may have to run the preceding command twice, once to connect to the emulator and again to install. After that, you should be able to run the application from the emulator and see the following:

Scala on Android screenshot

Posted by Tyler Lesmann on December 4, 2009 at 10:03 and commented on 1 time
Tagged as: android ant scala