Pages

Monday, July 25, 2011

Adding Scala libraries to the Android emulator

After a couple of weeks' development we recognized that the compilation took vast amounts of time to complete and it really slowed us down. We tried using Android plugin's lazyunpack with rather poor results, as well as tweaking the Proguard, but clean compilation still took us almost 2 minutes.

After a short search we stumbled on a really interesting article about preloading the Scala libraries to the Android emulator. All we needed to do was predex our Scala library and create a custom ramdisk with some extra rules in the init.rc file for loading the Scala libraries during the emulator startup. Jan Berkel also had a really good tutorial about how to preinstall the Scala libaries to the emulator (and also to a rooted phone). With these resources at hand I first cloned the scripts provided by
Berkel from the Github (derived from Stéphane Micheloud's scripts).

git clone git://github.com/jberkel/android-sdk-scala.git
cd android-sdk-scala\bin
createdexlibs.bat

I needed to set SCALA_HOME and ANDROID_SDK_ROOT, to get the scripts to run correctly.
set SCALA_HOME=PATH_TO_SCALA
set ANDROID_SDK_ROOT=PATH_TO_ANDROID_SDK

I also had to manually modify the createdexlib.bat, since Google had changed the location of the dx compiler.

Then I configured the Scala 2.9.0 version into my SCALA_HOME variable and ran the scipts. I also downloaded the custom ramdisk.img (avd.v10 [2.3.3]) from Micheloud's article. I tried to get the emulator to start with the new custom image by giving it as a parameter in the emulator start-up, but without success. So I had to replace the original ramdisk.img with my custom one.

The original image is located in the ANDROID_SDK_ROOT/platforms/android-10/images/ folder.

Then I started the Android emulator and ran the following commands in the
ANDROID_SDK_ROOT/platform-tools/ folder:

adb.exe shell ls / (you should run this command until you get a proper list of files)
adb.exe shell mkdir -p /data/framework

adb.exe push PATH_TO_ANDROID_SDK_SCALA\config\framework\scala-actors.jar /data/framework
adb.exe push
PATH_TO_ANDROID_SDK_SCALA\config\framework\scala-collection.jar /data/framework
adb.exe push
PATH_TO_ANDROID_SDK_SCALA\config\framework\scala-immutable.jar /data/framework
adb.exe push
PATH_TO_ANDROID_SDK_SCALA\config\framework\scala-library.jar /data/framework
adb.exe push
PATH_TO_ANDROID_SDK_SCALA\config\framework\scala-mutable.jar /data/framework

After this I restarted the emulator, but for some reason the emulator couldn't load the scala libraries correctly. So I had to do this again with the Scala 2.8.1 configured into the SCALA_HOME variable. With these libraries the loading went a lot better, but I still got this weird error message: DexOpt: mismatch dep signature for '/data/dalvik-cache/data@framework@scala-library.jar@classes.dex'. Despite the message, the emulator worked alright.

Compilation times

with Proguardwithout Proguard (Scala:Provided)without Proguard and with lazyunpackagewithout Proguard with scala cc (fcs:false) and lazyunpack
mvn clean package 1 min 56 sec 35 sec27 sec 21 sec
mvn package 1 min 32 sec 25 sec24 sec 14 sec


If you want to try this too, I have provided the Scala libraries (2.8.1) and also the custom ramdisk.img here so you don't have to do as much work as I did to get this working.

Big thanks to:
Stéphane Micheloud for this exellent article http://lamp.epfl.ch/~michelou/android/emulator-android-sdk.

Jan Berkel for making a really clear and easy step by step tutorial on how to do the emulator customization http://zegoggl.es/2011/07/how-to-preinstall-scala-on-your-android-phone.html

No comments:

Post a Comment