I found a workaround to create both library project and main application in scala for Android, working with Eclipse, and I would like to share it with you. However, the obscure cleaning process from Eclipse (see problem at the end) is preventing me to implement this feature in my bigger project, where this workaround currently does not work completely.
I assume you have installed a configuration like Eclipse 3.6, the plugin for Scala 2.8.3, the latest Android SDK (18 for me), the treeshaker plugin
Creating the library
- File > New > Project > Android Project
- Project Name:
TestScalaLibrary
, create new, use default location, next - Target 2.3.3 API 10, next
- Application name:
TestScalaLibrary
, package namecom.exampleLibrary
, create activity namedTestScalaLibraryActivity
, finish
Configuring the project TestScalaLibrary as a scala library
- Right-click on the project, configure, add scala nature
- Right-click on the project, add/remove Treeshaker.
- Right-click on the project, properties ... android, check "Is Library", ok
Create a new scala class named
TestScalaLibraryActivity2
in packagecom.exampleLibrary
with the following content:package com.exampleLibrary import android.app.Activity import android.os.Bundle class TestScalaLibraryActivity2 extends Activity { override def onCreate(savedInstanceState: Bundle) { super.onCreate(savedInstanceState) setContentView(R.layout.main) } }
Remove the file
TestScalaLibraryActivity.java
- Update the
AndroidManifest.xml
by replacingandroid:name=".TestScalaLibraryActivity"
byandroid:name="com.exampleLibrary.TestScalaLibraryActivity2"
- Clean the project, let it be built automatically. If at some point you get build errors because
testscalalibrary.jar
cannot be deleted, do it manually outside of eclipse by temporary closing the projects referencing this library.
Creating the test application
- File > New > Project > Android Project
- Project Name:
TestScala
, create new, use default location, next - Target 2.3.3 API 10, next
- Application name: TestScala, package name
com.exampleApplication
, create activity namedTestScalaActivity
Configuring the project as a scala project
- Right-click on the project, configure, add scala nature
- Right-click on the project, add/remove Treeshaker.
- Right-click on the project, properties ... android, library, add... add the project
TestScalaLibrary
as a library project, ok - Edit the
AndroidManifest.xml
and replace the relative nameandroid:name=".TestScalaActivity"
by its absolute equivalentandroid:name="com.exampleLibrary.TestScalaLibraryActivity2"
Launch it in debug mode (create a new configuration yourself if necessary), it does not work.
You get an Exception RunTimeException
.
Looking at LogCat, it eventually tells you where the problem comes from.
Caused by: java.lang.ClassNotFoundException: com.exampleLibrary.TestScalaLibraryActivity2 in loader dalvik.system.PathClassLoader
To fix it, I have the following workaround that i derived from this blog.
In the project TestScala, create the class
TestScalaActivityDummy
with the following code:package com.exampleApplication class TestScalaActivityDummy {}
Launch it in debug mode... it works !
Now the weird part.
- Remove the file
TestScalaActivityDummy.scala
(yes, delete the file), clean the project, let it be rebuild automatically. Launch it. It still works ! - Clean the project again, maybe several times. Then if you launch it, you get again a RunTimeException.
Why are these errors appearing after one clean, one launch and one clean?