Android development with Gentoo, Scala and SBT

This wiki page details how you can start developing for Android with Gentoo, the Scala programming language, and SBT (Simple-Build-Tool).

It is also likely that this is trivially easy to adopt to work for regular Java, likely the only difference will be that there needs to be a java source code file in a java/ folder instead of a scala one in a scala/ folder, but that has not been confirmed yet...

In the case no Java SDK is installed so far, install the package "sun-jdk" or any sdk for Java 1.6. Set it to be your user VM with java2-config. You will NOT need to install Scala, SBT can and will prefer to manage that by itself.
 * Step 0: Install Java

You need to emerge android-sdk-update-manager and add your user to the android group with gpasswd -a username android, as per the post-install instructions.
 * Step 1: Install the Android SDK and configure it.

Note: most of the andoid folders are set +rw on the andoid group, the only one left out is /opt/android-sdk-update-manager/tools/, if you feel like you need that too, please fill a bug report and change the permissions recursively in the meantime. Next you need to put export ANDROID_SDK_HOME="/opt/android-sdk-update-manager/" into your .bashrc or .zshrc in your user folder. It is important to also put export ANDROID_SWT="/usr/share/swt-3.7/lib/" into your .bashrc or .zshrc file in your user folder so that the download manager can locate swt.jar.

Once that is done, logged your user out and back in. You can now run "android". Download an android distribution ("Available Packages") and set up a Virtual Device (any will do). Run it to see if it works. If the keyboard and device screen shows up, that's good - the virtual device will take a moment to boot, though. Just wait until it is done. Keep the Virtual Device running for now, you'll need it to be running when you deploy your application later.

There is no ebuild for SBT. But since SBT is downloadable as a single .jar file, you can easily and safely install it by saving the jar into /usr/local/bin/sbt-launch.jar.
 * Step 2: Installing SBT

Next, create a file called /usr/local/bin/sbt with the content: java -Xmx512M -jar `dirname $0`/sbt-launch.jar "$@". Chmod a+x it so it is executable. You should now be able to run "sbt" on the console, as user.

At the point of this writing, this is most easily done by running a script used to set up a project which includes the plugin that allows SBT to build / deploy to Android targets. The most recent version of that plugin is maintained here: http://github.com/jberkel/android-plugin, inside the script/ folder. Pay attention to the readme on this page, as well as any possible more up-to-date branches of the plugin ("Network" button on github). You can download the script itself here: http://github.com/jberkel/android-plugin/raw/master/script/create_project.
 * Step 3: Get a project structure set up

After it is saved, chmod u+x it so it becomes executable, then run it as explained in the readme. You'll end up with a full folder structure, including sample code and basic configuration files for Android. This project is almost ready to go, but you will still need to edit /project/build/*.scala to contain the name of the android platform sdk you installed earlier. The androidPlatformName def in that file will need to match the folder name under /opt/android-sdk-update-manager/platforms - in my case, I put "android-8" there. This should take care of all the necessary setup, and you could run "compile" or "install-emulator" in the open sbt shell right now.

If you would like to make use of Scala 2.8 instead of 2.7, you can edit "project/build.properties". At the time of writing, 2.8.0 RC3 was the latest version, so it would need to be "build.scala.versions=2.8.0.RC3". Usually there is no need to change the def.scala.version line, which is only the one used by sbt itself.

You can now just have a peek at "src/main/scala/Activity.scala", which is a sample hello world scala application. Running sbt from the top level project folder will give you an interactive shell in which you can build and run targets as per the android-plugin's readme. You will probably want to run "~reinstall-emulator" from inside that sbt shell while an android virtual device is running - this will automatically reinstall the application into the android emulator, upon source code changes. More detailed use can be found out by reading the sbt wiki.
 * Step 4: Programming