Adding Scala support in a few clicks to your Android project (Eclipse)

Published on January 21, 2012

Adding Scala support in a few clicks to your Android project (Eclipse)

    In this post, I’ll show you how you can add Scala to your existing project in Eclipse. And so, let's go!

    On a habr there were already posts on this subject. For example:
    this one
    But the soul always asks for the simplest solution possible. Unlike most of the methods based on Ant or other hemorrhages that you need to do to add support for Scala, this method is very simple and fast. And the wonderful Eclipse plugin - Treeshaker will help us with this .

    And so, what should be done?
    For those who do not want to read the dock, I will describe the installation and one nuance that has arisen with me.

    • You should already have configured the environment (Eclipse + plugin from Google) for development for Android. You need to install the plugin (Scala IDE). We go to the Eclipse Help-> Install New Software item , add the site to the list: download.scala-ide.org/releases-28/2.0.0-beta and click Next, etc.
    • Further, according to the same scheme as in the paragraph above, install TreeShaker, and add as the site address: treeshaker.googlecode.com/svn/trunk/update_site

    That's all! We have installed everything you need.

    Now let's see how to use it.
    1. And so, create a new Android project
    2. By right-clicking on the project, in the Configure item, select the Add Scala Nature sub-item. And lo! Scala - libraries added to the project
    3. Next, right-click on the project again and see the Add / Remove TreeShaker item in the list


    Next, we remove our Java code, and instead, insert the following:
    import android.os.Bundle
    import android.view.View
    import android.widget.TextView
    import android.widget.Button
    import android.app.Activity
    import R._
    class ThreeShakerTutorialActivity extends Activity {
        lazy val textView = findViewById(R.id.text).asInstanceOf[TextView]
        lazy val button = findViewById(R.id.button).asInstanceOf[Button]
        override def onCreate(savedInstanceState: Bundle) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.main)
            button.setOnClickListener((v: View) => textView.setText("Hello Scala"))
        }
        implicit def func2OnClickListener(func: (View) => Unit) = {
            new View.OnClickListener() {
                override def onClick(v: View) = func(v)
        }   }
    }
    

    In the main.xml layout, in LinearLayout, add Button and TextView with the id-keys specified in the code (the markup is on the wiki on Google code, but I'm afraid of a hub-parser). All is ready!

    Everything would be fine, but there is one BUT. Namely, the code

    button.setOnClickListener((v: View) => textView.setText("Hello Scala"))
    

    throws a compilation error. We fix it this way, but this is a bad way:
    button.setOnClickListener(func2OnClickListener((v: View) => textView.setText("Hello Scala")))
    


    And the correct one, this code is left as is:
    button.setOnClickListener((v: View) => textView.setText("Hello Scala"))
    

    and make an amendment to the definition of the method:
    implicit def func2OnClickListener(func: (View) => Unit): View.OnClickListener = {
            new View.OnClickListener() {
                    override def onClick(v: View) = func(v)
            }
    }
    


    Thanks to the romik habrayuzer for this help.

    Although I don’t know Scala at a good level, intuition is a great thing! It is strange somehow that the authors did not notice this moment!
    Knowledgeable people can explain why there is a mistake, but I think the authors of the example obviously forgot something.

    It is important to ensure that the Builders project property contains all of the following:
    • Android Resource Manager
    • Android Pre Compiler
    • Scala builder
    • Treeshaker
    • Android Package Builder

    Otherwise, after compiling and starting the project, the ClassNotFound will be executed on the emulator.
    Demo project - here