In this post, I’ll show you how to add support for Scala to an existing project in Eclipse. And so, let's go!
On Habré were already posts on this topic. For example:
thisBut the soul always asks for the simplest possible solution. Unlike most Ant-based or other gemmore methods that need to be done to add Scala support, this method is very simple and fast. And help us in this wonderful plugin for Eclipse -
TreeshakerAnd so what needs to be done?For those who do not want to read the dock I will describe the installation and one nuance that arose in me.
That's all! We have installed everything you need.
')
Now let's see how to use it.
- And so, create a new Android project.
- By clicking the right mouse button on the project, in the Configure item, select the Add Scala Nature sub-item. And, lo and behold! Scala - libraries added to the project
- Then, right-click on the project again and see Add / Remove TreeShaker in the list.
Next, 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 we add a Button and TextView with id-shniki specified in the code (there is a markup on the wiki on the Google code, and I’m afraid of a habrahparser). All is ready!
All is good, but there is one BUT. Namely, the code
button.setOnClickListener((v: View) => textView.setText("Hello Scala"))
produces a compilation error. We fix it like this, but this is a bad way:
button.setOnClickListener(func2OnClickListener((v: View) => textView.setText("Hello Scala")))
And the correct one is, this code is left as is:
button.setOnClickListener((v: View) => textView.setText("Hello Scala"))
and we 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
ombik for this hint.
Although I don't know Scala at a good level yet, but 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 have obviously forgotten something.
It is important to ensure that the following items are in the property of the Builders project:
- Android Resource Manager
- Android Pre Compiler
- Scala builder
- Treeshaker
- Android Package Builder
Otherwise, after compiling and running the project on the emulator will be a ClassNotFound exercise.
The demo project is
here.