Personal tools
You are here: Home Developer Zone Documentation MUSIC in mobile devices MUSIC on Android (phase I)
Document Actions

MUSIC on Android (phase I)

by Jorge Lorenzo last modified 2009-12-22 11:42

Android has become into an attractive mobile operating system, especially in the Java and open source world. The MUSIC consortium started an initiative to port the MUSIC middleware into Android OS which would provide mutual benefits for both projects. This article describes this experience.

Android

Android is an open source software stack for mobile devices that includes an operating system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.

The following diagram shows the major components of Android:

Android architecture
  • Linux kernel. Android relies on Linux 2.6. The kernel acts as an abstraction layer between the hardware and the rest of the software stack and provides core system services: security, memory management, process management, network stack, and driver model.
  • Android runtime. Android includes a set of core libraries that provides most of the functionality available in the core libraries of the Java programming language.Every Android application runs in its own process, with its own instance of the Dalvik virtual machine. Dalvik has been written so that a device can run multiple VMs efficiently. The Dalvik VM executes files in the Dalvik Executable (.dex) format which is optimized for minimal memory footprint.
  • Libraries. Android includes a set of C/C++ libraries used by various components of the Android system. These capabilities are exposed to developers through the Android application framework.
  • Application framework. Developers have full access to the same framework APIs used by the core applications. The application architecture is designed to simplify the reuse of components.
  • Applications. Android ships with a set of core applications including an email client, SMS program, calendar, maps, browser, contacts, and others.

Android has turned into a real alternative in the mobile operating systems world. Google expects at least 18 Android devices by the end of 2009. Some Android devices are already in the market.

HTC dream


OSGi and Android

OSGi and Android define a development platform for component-based development and provide an implementation for the service-oriented architecture, within the mobile device. There are a number of similarities as well as some key differences.

OSGi vs Android

A detailed comparison between OSGi and Android is available here. Some relevant differences are:

  • OSGi is based on Java VM instead of Dalvik VM. Each Android application is executed in a separate instance of the Dalvik VM, while all the OSGi applications are executed in the same Java VM.
  • OSGi simplifies the reuse of code by the export/import of Java packages (class sharing) and by the OSGi service model. The performance of the service invocation in OSGi is really convenient in comparison with the complex and heavy inter-process communication supported by Android. Versioning is supported by OSGi which is extremely important for maintenance of applications.
  • Some features are not available. For example, OSGi supports remote management. On the other hand, Android offers an application model.


MUSIC and Android

MUSIC would offer some advantages to Android which makes this integration be convenient. These advantages would be complemented with those ones provided by OSGi as well. The most relevant benefits provided by MUSIC are:
  • Self-adaptive applications. The advantage of reconfigure the behaviour of our applications according to the execution context: device resources, environment, user profile and so on which is a key feature to improve the user experience in changing environments.
  • Context-awareness. MUSIC offers an extensible efficient context-awareness middleware where context providers and context consumers can be plugged in runtime. This feature is enriched with the context distribution mechanism based on SIP.
  • Ubiquitous computing. The MUSIC communication services support the discovery of services and devices available in the surroundings. It permits the binding with these services/devices in a transparent smart way.
  • SOA and SLA. MUSIC offers support for the incorporation of services into the application compositions. In the short term, it will be enhanced by hosting of services and by a Service Level Agreement framework.
  • Portability. The MUSIC applications could be executed in Android devices without source code modifications. However, the GUI should be updated to use the Android API.

Preparing the development environment for Android

Some development tools are required (or convenient) to start working with Android:

To test the correct installation of these development tools, it is recommendable to create an Android Virtual Device (AVD) which will be used in the future steps. The creation of the AVD requires the following steps:
  • List the available Android targets and select the id related to Android 1.5 (probably id will be 2):
android list targets

Available Android targets:
id: 1
Name: Android 1.1
Type: Platform
API level: 2
Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
id: 2
Name: Android 1.5
Type: Platform
API level: 3
Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
id: 3
Name: Google APIs
Type: Add-On
Vendor: Google Inc.
Description: Android + Google APIs
Based on Android 1.5 (API level 3)
Libraries:
* com.google.android.maps (maps.jar)
API for Google Maps
Skins: QVGA-P, HVGA-L, HVGA (default), QVGA-L, HVGA-P
  • Create the AVD (in this case, with the name music_avd and the target 2 which corresponds to Android 1.5):
android create avd --name music_avd --target 2
Now it is possible to launch the Android emulator:
emulator -avd music_avd
Android is built over a Linux kernel and main Linux commands are available. To open a shell:
adb shell

Porting of OSGi to Android

Most of the OSGi projects have noticed the relevance of porting their OSGi framework to Android. Although Android is relatively a recent product, the OSGi frameworks have updated their source code to adapt it to Android. Some changes in the source code have been required to achieve this compatibility.

The MUSIC project has evaluated 3 relevant OSGi frameworks (Equinox, Felix and Knopflerfish) which comply with the MUSIC requirements:
  • Open source and compatible with our license: LGPL v2.1.
  • OSGi v4 enabled. The MUSIC middleware requires 2 OSGi standard services: Configuration Admin (to configure dinamically the OSGi bundles which conform the middleware) and the declarative services (which simplify greatly the development with OSGi services by managing the service dependencies).

The porting of OSGi requires the dexification of the OSGi bundles. This is a process which makes any JAR file (compliant with Java VM) compatible with Dalvik VM. It basically consists on adding a new file, classes.dex, into the JAR file. To dexify each OSGi bundle, a two-step process is required by using the Android SDK tools. In windows, the process would be:
  • Create the classes.dex file associated to the JAR file:
[ANDROID_SDK]\platforms\android-1.5\tools\dx.bat --dex --output=%CD%\classes.dex my_bundle.jar
  • Incorporate the classes.dex file into the JAR file:
[ANDROID_SDK]\platforms\android-1.5\tools\aapt.exe add my_bundle.jar classes.dex
Now, the dexified bundles work on both VMs: Java VM and Dalvik VM.

The result of this evaluation is presented in the following table.


OSGi core
OSGi Configuration Admin
OSGi declarative services
Equinox v3.4.2
ko ko ko
Felix v1.8.0
ok ok ko
Knopflerfish v2.3.1
ok ok ok

It was not possible to dexify the Equinox framework (even the core). Although Felix is really advanced with the porting to Android, offering very good documentation about this process, it was not possible to port one of the OSGi standard services required by the MUSIC middleware. Finally, knopflerfish offered the same situation than Felix but we could find a workaround to fix the porting of the OSGi declarative services. We notified a bug to Knopflerfish but it has not been solved yet. The reason of this bug is that Dalvik VM does not permit to include JAR files inside another JAR file, and in the case of the declarative services bundle, there was a JAR library to support the XML parsing.

According to this evaluation, we decided to use Knopflerfish v2.3.1 as the reference OSGi framework in Android.

Porting of MUSIC to Android

The porting of the MUSIC middleware required:
  • To dexify every MUSIC bundle according to the previous two-step process. We did not find any problem during this process (although only a subset of the MUSIC bundles were transformed).
  • To adapt the MUSIC GUI, as well as the GUI for each MUSIC application, in order to use the Android API. The reference GUI API is eSWT and it is not compatible with Android.

The following picture represents the architecture of the Android application which launches the MUSIC middleware:
  • The Android application, music.apk, which will offer and share the Android activity and will launch the OSGi framework. The OSGi framework is managed by an Android service, OSGiService, so that the OSGi framework is not launched/shutdown whenever the Android application is started/stopped.
  • The OSGi framework which is deployed directly in the /data/osgi directory of the Android device. Knopflerfish is the OSGi framework selected, according to the compatibility reasons claimed before. The only difference with other Knopflerfish distribution is that the OSGi bundles have been dexified to be executed on the Dalvik machine.

Architecture of MUSIC in Android

The main design decision is that all the GUI screens of the MUSIC middleware and applications, which are called GUI pages in MUSIC terminology, will share the same Android activity. The reason is that OSGi bundles cannot define the GUI by the Android standard way (with an XML file). The same activity will be reused and the OSGi bundles will have the chance to update the activity with a new interface.

The following figure represents the sequence diagram for launching the MUSIC middleware. When a user starts MUSIC in an Android device, it will create the “Music Activity” which initially will display a progress screen (because the process will take some time) and will start the “Knopflerfish service”.
This Android service will launch the OSGi framework if it was not previously launched, and will notify to the MUSIC activity when the framework has been launched. Meanwhile, the MUSIC OSGi bundles will be started. When the “Music Activity” discovers that the OSGi framework has been launched, it will register an OSGi service to trigger the activation of the MUSIC GUI. The “MUSIC GUI for Android” is an OSGi bundle which will obtain the Android activity to update the screen for any of the MUSIC GUI pages.

Launch of the MUSIC middleware

The source code which was implemented to migrate MUSIC to Android is available here. It consists on 2 different projects:
  1. android. It corresponds to the Android application which provides the Music Activity and the Knopflerfish service. It is an Eclipse project which requires the Eclipse ADT plugin.
  2. gui.android. It is an alternative realization of the MUSIC GUI bundle for Android (other alternatives are gui.eswt and gui.linux). It is an Eclipse and Maven project with the same structure than other bundles of the MUSIC middleware. To compile this project (with Maven), it is required to install a Maven dependency to the Android API in the local Maven repository; unfortunately, the Android API is not available in any well-known remote Maven repository yet. To install it, you can follow the instructions available here or just execute:
mvn install:install-file -DgroupId=com.google.android -DartifactId=android.sdk -Dversion=1.0 -Dpackaging=jar -Dfile=[ANDROID_SDK]\platforms\android-1.5\android.jar

Demonstration

The objective of this activity was to demonstrate the feasibility of porting OSGi and MUSIC to Android. As a consequence, the effort was constrained to achieve this objective but minimizing the effort. The core components of the MUSIC middleware and 2 tutorials have been ported to Android. Likewise, the MUSIC GUI for Android does not offer all the capabilities provided by the reference implementation based on eSWT.

The MUSIC GUI for Android supports the installation/uninstallation of MUSIC bundles, and the launch/shutdown of MUSIC applications.

The demonstration is realised by the following steps:
  • Launch the Android emulator (if no real Android device is available):
emulator -avd music_avd
  • Download the software for the demonstration. This software is available here. Uncompress the file and open a console in the directory which has been uncompressed:
cd [demo_music_android]
  • Upload the OSGi framework to the /data/osgi directory of the Android device.
adb push knopflerfish /data/osgi
  • Installation of the Android application for MUSIC. This Android application includes the MUSIC Android Activity, which will be shared by the MUSIC GUI and by the GUI of the MUSIC applications, and the Knopflerfish Android Service, which is a background task which will launch the OSGi framework installed in the /data/osgi directory of the Android device.
adb install android-osgi.apk
  • Open a shell to give write permission to every one in the directory /data/osgi and /data/dalvik-cache:
adb shell
$ chmod 777 /data/osgi
$ chmod 777 /data/dalvik-cache

  • Launch the MUSIC application, from the list of applications, in the Android device/emulator.
Demonstration
  • Access to the Bundle Management and install a bundle (this option is available in the menu provided by pressing the MENU key of the Android device). The location of this bundle is: /data/osgi/music/test.jar. This bundle corresponds to the MUSIC application: "Tutorial 2: Alternative realizations" which will be executed.
  • Go back and access to the Application Management. Launch the application: "Tutorial 2: Alternative realizations" by pressing (2 or 3 seconds) this application in the list.
Demo: Launch the application
  • Switch the orientation (it is simulated because the Android emulator does not support the change of the orientation). After changing the orientation, which it corresponds to a context change, the adaptation middleware will reconfigure the application according to the (simulated) screen orientation: portrait or landscape.
Demo: Adapt the application

This demonstration of the MUSIC middleware in Android has been validated in:
  1. The Android emulator provided by the Android SDK.
  2. A Nokia N810 Internet Tablet with NITdroid.

Open issues

Although the porting has been very successful, there are some open issues which is convenient to be tackled:
  • Only the most basic part of the middleware has been ported to Android to validate it with a simple application. We do not foresee any problem to port the rest of the MUSIC middleware but there are some considerations to face:
    • The communication services which needs access to the network for publishing/discovery services and devices. These bundles will require a minor modification in the Android application for MUSIC in order to habilitate the Internet access of the application/MUSIC. Anyway, it should be carefully tested.
    • The resource plugins, or any other bundle with native libraries, will require some adaptation for integrating it into Android because the native libraries will not be valid anymore (unless they were linux libraries).
  • The write access to the directories: /data/osgi and /data/dalvik-cache is not desirable. Although the write access to /data/osgi could be permitted, it is not acceptable the write access to /data/dalvik-cache because it would be a security hole (by enabling any Android application to install and remove any other Android application). It looks like the Android ClassLoading API, which was opened recently, will overcome this problem but it requires some modifications in the source code of the OSGi frameworks. Apache Felix member reported that they have already fixed it into the trunk of their framework.
  • Find any mechanism to use the Android application model in the MUSIC applications. The GUI could be realised with the Android application model, by a normal Android application, and the application model and variability concerns would be separated into a MUSIC OSGi bundle.

Conclusions

The porting of OSGi and MUSIC to Android has been successful. It was not difficult to port OSGi bundles to Android although we faced some problems. The most time consuming activity was the development to the MUSIC GUI for Android so that it would be possible to launch the MUSIC middleware from the Android GUI instead of Android console.

The good performance of the MUSIC middleware and applications, as well as the benefits provided by the Android high-quality user interface, make Android devices as a good alternative to deploy our middleware.

Some open issues have been brought up which could permit future activities to consolidate the migration of MUSIC to Android.

Related material

  1. Source code. This ZIP file includes the source code of 2 different Eclipse projects: a) android, which is an Eclipse project the Android application for MUSIC, and b) gui.android, which is and Eclipse/Maven project with the MUSIC GUI bundle for Android.
  2. Demonstration software. This ZIP file includes the Android application for MUSIC and the MUSIC distribution based on Knopflerfish and with all the bundles already dexified.
  3. Presentation. Presentation with our experience about the integration of OSGi and MUSIC in Android.

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: