2

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 name com.exampleLibrary, create activity named TestScalaLibraryActivity, 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 package com.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 replacing android:name=".TestScalaLibraryActivity" by android: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 named TestScalaActivity

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 name android:name=".TestScalaActivity" by its absolute equivalent android: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?

Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
  • Not directly answering your question, but I hacked on maven + scala + android for some days, but finally found resolution in sbt + sbt-android plugin. Plus sbt-idea plugin to generate Idea project. Note that Idea free supports Android and Scala too, maybe worth a try. Or sbteclipse to generate Eclipse project ( https://github.com/typesafehub/sbteclipse ), no experience though. – ron May 24 '12 at 12:03
  • I also can't solve this problem, but I'd like to suggest a different eclipse plugin for scala + android: https://github.com/banshee/AndroidProguardScala (I'm the author) – James Moore Jun 15 '12 at 15:45
  • Excellent plug-in ! I'll write down a different solution which is working now. – Mikaël Mayer Jun 15 '12 at 18:59

1 Answers1

1

The solution is to start all over without treeshaker but with the AndroidProguardScala plug-in instead :

https://stackoverflow.com/a/11084146/1287856

Community
  • 1
  • 1
Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101