Scala on Android
The Scala Programming Language:
The Scala programming Language is an object oriented functional programming language designed to run on the JVM. It is ideal for programming on the Dalvik Platform as code written with the Scala language is more compact, readable and could possibly have performance benefits (which will be analysed at a later date) and it also supports Java and Android APIs.
You can easily build DSL with Scala and the trait (e.g. mixin) aspect of Scala is very attractive in order to build intricate activities which mixes different logic.
Requirements:
- Eclipse IDE – Galileo Classic v3.6
- Android ADT plugin v0.93
- Android SDK v1.6r1
- Scala eclipse plug-in latest
- Scala-android library latest
Step 1: Setting up the environment
The Scala project is rather heavy on resources therefore we would need to increase the heap size of the eclipse VM and of the dx tool which is present in the SDK.
Editing the eclipse memory size:
In order to increase the maximum permissible memory of the IDE we need to edit the exlipse.ini file that resides in the root folder of the IDE (in my case C:/Program Files/eclipse or /opt/eclipse). Open the file with your favourite text editor and edit the files as shown in the figure below and add the following lines to the file:
256m
-vmargs
-Xms256m
-Xmx1024m
-XX:PermSize=64m
Editing dx.bat/dx.sh:
The file dx.bat/dx.sh is located in the <ANDROID_HOME>\platforms\android-1.6\tools. Open dx.bat/dx.sh with your favourite text editor and uncomment the javaOpts line while changing the value to 512M:
Step 2: Install Scala
I installed both the stand alone Scala installer as well as the Eclipse IDE plugin, which I recommend to get the system working stable.
The Scala Installer:
On writing this post the latest version of Scala available for download was the 2.7.6 version, which can be downloaded from: http://www.scala-lang.org/downloads/distrib/files/scala-2.7.6.final-installer.jar . The installation procedure is quite trivial and further information can be found on the Scala website. The installer consists of a sequence of simple steps which on completion result in the installation of Scala on your system.
Eclipse Plugin:
In order to install the eclipse plugin we have to goto the Help->Install New Software->Add and then enter the following:
Name: Scala
Location: http://www.scala-lang.org/scala-eclipse-plugin
You should then see “Scala Eclipse Plugin” offered for installation. Click on the “Install” button and follow the instructions from there.
Step 3: Download scala-android.jar
Open a new shell prompt window in administrator mode. Navigage to the <SCALA_HOME>/bin directory defined in the previous Scala installation step. Then enter the following commands
sbaz install scala-android
The scala-android library will be installed in <SCALA_HOME>/lib directory.
Step 4: Creating a Scala Project
In order to create our first Scala project we go about creating first an Android Project in the Eclipse IDE (File->New->Android Project).
Then right mouse click on the project in the package workspace and navigate down to the Scala menu, then click on Add Scala Nature, once this is completed the project would look like:
Once this is complete, right mouse click on the project again and click on the Properties->Builders tab, and make sure the Scala Builder is in the position as shown in the figure (you will have to move it down two places)
Then open the Java Build Path->Libraries tab and remove the Scala Library Version 2.6 final , then add the scala-android.jar file by click on Add External Jar. You will then have the menu resembling:
Note: The build process needs to compile the scala files into classes which then compiles them into dex files. If you receive errors upon compilation or classpath, try to clean and rebuild the project.
Step 5: Creating ScalaTest.scala
Now goto the ScalaTest->src->com.scala package and delete the the file ScalaTest.java. Right mouse click on the package and then click on New->Other-Scala Class , call this class ScalaTest.
Open the file ScalaTest.scala and enter the following code and save it:
package test.scala
import android.app.Activity
import android.os.Bundle
import android.widget.TextView
class ScalaTest extends Activity {
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
val tv = new TextView(this)
tv.setText("Hello Android, it's me, Scala!")
setContentView(tv)
}}
Step 6: Running the application
In order to run the application goto Run->Run Configuration->Android Application and run set it up as shown in the figure below, and then click on Run.
If everything was set up right you’d then see the emulator load up and run your application
:
References:
- http://www.scala-lang.org/node/160
- http://www.ibm.com/developerworks/opensource/library/os-eclipse-scala/index.html?ca=dgr-jw64Android-Scaladth-o&S_TACT=105AGY46&S_CMP=grjw64#
- http://www.scala-lang.org/node/1403
