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/HelloScala.java and create src/com/example/helloscala/HelloScala.scala with the following contents:

package com.example.helloscala

import _root_.android.app.Activity
import _root_.android.os.Bundle

class HelloScala extends Activity {
  override def onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main)
  }
}

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.

-compile-tested-if-test:

-dirs:
     [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

-resource-src:
     [echo] Generating R.java / Manifest.java from the resources...

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

compile:
    [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] 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)

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

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

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

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

BUILD SUCCESSFUL
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
Tagged as: android ant scala
Comments
#1 ornameFlere wrote this 2 years, 9 months ago

Man .. Excellent .. Superb .. I will bookmark your blog and take the feeds alsoI'm happy to seek out numerous useful information right here in the put up, we'd like work out more strategies in this regard, thanks for sharing. . . . . .

Post a comment