I recently followed a way of programming for Android using Scala and Eclipse, which reduces the code and the compile time without using Proguard or Treeshake.
Following this article, I should be able use the last Eclipse build (3.7), almost the last version of Scala (2.8.1) updated on an emulator version 10, the version 2.8.3 within Eclipse with the provided plug-in.
The presented way is to provide a specific ramdisk image version, where we can upload scala libraries, which drastically shrinks the size of the code to upload to the emulator.
I followed the steps, created a hello world, added scala nature, added a dummy scala class, moved the Scala builder before the Android Package Installer, everything builds perfectly, but when I launch the apk on a emulator from Eclipse, the application crashes and I get the following error, which looks like the
same as presented here (at the end of the document) :
03-29 10:29:38.505: E/AndroidRuntime(839): java.lang.NoClassDefFoundError: upg.TestSinceInstallation.ComputeSum
If I remove the scala reference in the activity file, it runs well.
Here is the TestSinceInstallation.java file:
package upg.TestSinceInstallation;
import android.app.Activity;
import android.os.Bundle;
import upg.TestSinceInstallation.ComputeSum;
public class TestSinceInstallationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int a = 1;
int b = 5;
ComputeSum cs = new ComputeSum(a, b);
if(cs.getResut() == 6) {
setContentView(R.layout.main);
}
}
}
and here is the ComputeSum.scala file
package upg.TestSinceInstallation
class ComputeSum(a: Int, b: Int) {
def getResut() : Int = a + b
}
What do you think I should do to make this work ? I feel so close to the goal.
Here is the solution, to use Android with Eclipse 3.7 and with Scala 3.0.0 without any problems.
Now, to create a scala project,
Configure,Add Scala natureAdd AndroidProguardScala natureYou’re done.
Scalafying android code
Now good things happen.
First, you can scalafy any activity, and you will get access to scala unique features, such as:
Here is an example of some of them.
Now after all the previous boilerplate, it is very useful to write concise activities. Note how we can directly operate on ids as if they were views. Disambiguation is needed as
(view: TextView).requestFocus()if the methods can be inferred from various structures.Make sure that the name of the scala file matches the main activity contained in it, in this case it should be
MyActivity.scala.Second, to set up a scala project as a library project, to use is as a base for applications having different resources, follow the regular way of setting up a library project. Right-click on the scala project that you want as a base library project,
Properties,Android, and checkisLibrary.To create derivated project using this library and for which you can generate an APK, create a new android project, and without adding any scala or androidproguardscala nature, just right-click,
Properties,Android, and add the previous scala project as a library.UPDATE With the new version of the Android Plug-in, you should go to
Project Properties > Build Päth > Order and Exportand checkAndroid Private Libraries. This will allow to export the scala library, both in the library project and the main project, even if the main project is not assigned Scala.TESTING Using the plug-in Robolectric makes it easy to test your android scala project. Just follow the steps for creating a test project, add Scala nature to it. You can even use the new scala debugger, and by adding the scalatest library, you can use should matchers and many other features Scala has.