android + scala + intellij sbt returns warning and app crashes on emulator: bad class file magic (cafebabe) or version (0033.0000)-Collection of common programming errors

I am trying to get a hello world android app work with scala and intellij Whoever tried the same feat must be familiar with the following steps. The problem happens at the end when I am using the sbt

Please note the versions for each tool I use. For starters I am using the java version 1.7.0_07

-Download and setup the android sdk. I have downloaded all versions from 1.5 (API 3) to 4.1 (API 16). I will not go into further details on this. There are many tutorials already.

We are going to need the android home path in an enviroment variable. So let’s set a persistant enviroment variable for the current user

gedit ~/.pam_environment

and now append this: ANDROID_HOME= e.g.

ANDROID_HOME=/home/pligor/android-sdks

Now let’s grab the sbt tool. We would rather have a manual installation of sbt. Find the “Unix” section in the link: https://github.com/harrah/xsbt/wiki/Getting-Started-Setup and download sbt-launch.jar and place it in ~/bin Currently this is version 0.12.0 Then create an sbt file:

gedit ~/bin/sbt

write this line inside:

java -Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M -jar `dirname $0`/sbt-launch.jar "$@"

and make script executable:

chmod u+x ~/bin/sbt

Now you need to install an sbt plugin for the intellij. The sbt-idea version 1.1.0:

gedit ~/.sbt/plugins/build.sbt

add append inside the line (remember each .sbt files seperates each statement by an extra empty line):

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.1.0")

For github templates you need the giter8 tool. So execute:

curl https://raw.github.com/n8han/conscript/master/setup.sh | sh

and then execute:

cs n8han/giter8

Now I use the g8 to get an android app template from jberkel:

g8 jberkel/android-app

There are some questions for you to setup. Currently I leave everything to default. Which means I use scala version 2.9.1 and Android 2.3.3 (API 10) and scalatest_version of 1.8.RC1. I am not accustomed with proguard since I haven’t use it before so I set useProguard to false.

Before you run sbt: The android-plugin is currently compatible with the 0.11.3 version of sbt we need to make an adjustment. So inside the folder of the new android application execute: gedit project/build.properties and insert this line: sbt.version=0.11.3

Inside the same folder execute: sbt ‘gen-idea no-classifiers’ compile

and then go to the sbt interactive mode console: sbt

Inside the console execute: android:package-debug

And I believe these kind of warnings is where the problem starts: trouble processing: bad class file magic (cafebabe) or version (0033.0000) …while parsing my/android/project/R$id.class

Now you can try to start an android emulator: android:emulator-start # use to get a list of avds e.g. android:emulator-start android2_3_3

after the emulator is fully started execute the android app like that: android:start-emulator

AND AFTER ALL THIS THE APP WILL CRASH !!

  1. The problem is with your Java version. Switch back to Java 6 or if you can’t, http://stackoverflow.com/a/7705091/203487 might help.

    “bad class file magic (cafebabe) or version (0033.0000)” means the generated class files has a version of 51 which is Java 7, so it’s clear that neither the dexer nor ProGuard can work with these class files. Here I’m not talking about the latest ProGuard version but the one bundled with the Android SDK.

  2. I can only speak about my setup, which is kind of successful so far.

    • You will need Proguard to create apks of reasonable size.
    • I use Scala 2.10-M6, since 2.8 is old and has its quirks, especially with Android, and 2.9 is very slow to Proguard.
    • The keep class scala.collection.SeqLike { ... is important otherwise the class is left out to the contrary it has a method references (I posted this workaround).
    • The Build.scala uses many fun stuff like scalaz or libgdx, feel free to prune these. Many android stuff is hacked together from various g8 templates, thx for all the people sharing them.

    So here is a dump of my setup:

    project/Build.scala project/plugins.sbt

    I use sbt 0.11.3 and Idea 11.1. I compile using the sbt console, mainly use the android:start-device command, and sometimes android:emulator-start RomName + android:start-emulator. Idea attaches nicely to the running Android process.

    I’m developing a small game with non-intensive graphics, and so far I used idiomatic Scala (and even Scalaz) without slowdowns. So no need to revert to arrays, vars and while loops just for the sake of Android 🙂

    Good luck!