Cross-Compiling Android
Applications to the iPhone

             Arno Puder
     San Francisco State University
          ...
The Team

               Core team:
                – Arno Puder, SFSU
                – Sascha Haeberling, Google Inc
  ...
Background

                Smart phones have entered the mainstream.
                Most phones have similar capabilit...
Android vs. iPhone vs. Palm Pre
                                       HTC G1            iPhone 3GS                     Pa...
Proposed Solution

                Android is used as development platform:
                 – Developer only needs to kn...
iPhone “Hello World” in Objective-C
               @interface HelloWorld : UIApplication
               -(void) applicatio...
iPhone “Hello World” in Java
               public class HelloWorld extends UIApplication
               {
               ...
Tool Chain Overview

                 “Hello World”                             Cocoa Java
                   Java app.   ...
Challenges
                Design goals:
                 – If possible, Java API mimics 1:1 the Objective-C API.
       ...
Tool Chain Overview

                       // Java
                       class Calc {
                           static ...
XMLVM of Class Calc

                  <?xml version="1.0" encoding="UTF-8"?>
                  <xmlvm xmlns:dex="http://x...
Cross-Compiling XMLVM to Objective-C
                  Simply map DEX registers to variables of the target language!
    ...
Class Calc in Objective-C

                 // Objective-C generated via stylesheet
                 @implementation Calc
...
Android Compatibility Library

              Application         Application          Application
                        ...
Example 1: Button to UIButton Mapping

              package android.widget;

              public class Button extends Vi...
Example 2: R-class
                   // Generated by Android
                   public final class R {
                  ...
Example 3: Layout Manager

                Android uses declarative UI descriptions.
                UI descriptions are...
Xokoban
                Sokoban version developed for Android and cross-compiled to
                 the iPhone and webOS...
Legions


                Implementation of the Stratego board
                 game.
                Developed for Andr...
Scope

                Android API offers good support for different screen
                 resolutions.
               ...
XMLVM Tool Chain
                            .NET .exe         Java Class File           Ruby YARV
                       ...
XMLVM Tool Chain
                            .NET .exe         Java Class File           Ruby YARV
                       ...
XMLVM Tool Chain
                            .NET .exe         Java Class File           Ruby YARV
                       ...
XMLVM Tool Chain
                            .NET .exe         Java Class File           Ruby YARV
                       ...
XMLVM Tool Chain
                            .NET .exe         Java Class File           Ruby YARV
                       ...
Conclusions

  Summary:
   – Java for the iPhone.
   – Android Compatibility Classes.
  Benefits:
   – Leverage existing...
Upcoming SlideShare
Loading in...5
×

Cross-Compiling Android Applications to the iPhone

1,473

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,473
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
36
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Cross-Compiling Android Applications to the iPhone

  1. 1. Cross-Compiling Android Applications to the iPhone Arno Puder San Francisco State University arno@sfsu.edu
  2. 2. The Team  Core team: – Arno Puder, SFSU – Sascha Haeberling, Google Inc – Wolfgang Korn, blueCarat AG  Contributors: – Kevin Glass – Panayotis Katsaloulis – Gergely Kis – Jessica Lee – Joshua Melcon – Alexander Seewald  Sponsors: – blueCarat AG, Cologne, http://www.bluecarat.de Slide 2 © XMLVM.org
  3. 3. Background  Smart phones have entered the mainstream.  Most phones have similar capabilities.  Common subset useful for certain class of applications (e.g. games).  Developers are interested to push their applications to different platforms.  Problem: Porting an application to different phones requires significant effort. Slide 3 © XMLVM.org
  4. 4. Android vs. iPhone vs. Palm Pre HTC G1 iPhone 3GS Palm Pre OS Linux Mac OS Linux CPU ARMv6, 528 MHz ARMv6, 600 MHz TI-OMAP, 600 MHz RAM 192 MB 256 MB 256 MB Accelerometer, GPS, Accelerometer, Accelerometer, GPS, Sensors proximity, ambient light, GPS, compass. proximity, ambient light. compass. IDE Eclipse Xcode Eclipse Dev-Language Java Objective-C JavaScript GUI Android Cocoa Touch webOS Virtual Machines Allowed Not allowed Allowed License Open Source Proprietary Proprietary Slide 4 © XMLVM.org
  5. 5. Proposed Solution  Android is used as development platform: – Developer only needs to know Android SDK.  Make use of well-documented SDK.  Powerful tools (debugging).  Support for arbitrary screen resolutions using layout managers (Nexus One, iPhone, iPad, Netbooks). – Android application is cross-compiled to Objective-C or JavaScript. – Does not require a JVM on the iPhone!  Approach for Android to iPhone cross-compilation: – Create Java API for Cocoa Touch. – Cross-compile Java to Objective-C. – Write Android compatibility library on top of Cocoa Touch Java API. Slide 5 © XMLVM.org
  6. 6. iPhone “Hello World” in Objective-C @interface HelloWorld : UIApplication -(void) applicationDidFinishLaunching: (NSNotification*) n; @end @implementation HelloWorld -(void) applicationDidFinishLaunching: (NSNotification*) n { UIScreen* screen = [UIScreen mainScreen]; CGRect rect = [screen applicationFrame]; UIWindow* window = [[UIWindow alloc] initWithFrame: rect]; rect.origin.x = rect.origin.y = 0; UIView* mainView = [[UIView alloc] initWithFrame: rect]; [window addSubview: mainView]; UILabel *title = [[UILabel alloc] initWithFrame: rect]; [title setText: @"Hello World!"]; [title setTextAlignment: UITextAlignmentCenter]; [mainView addSubview: title]; [window makeKeyAndVisible]; } @end Slide 6 © XMLVM.org
  7. 7. iPhone “Hello World” in Java public class HelloWorld extends UIApplication { public void applicationDidFinishLaunching(NSNotification n) { UIScreen screen = UIScreen.mainScreen(); CGRect rect = screen.applicationFrame(); UIWindow window = new UIWindow(rect); rect.origin.x = rect.origin.y = 0; UIView mainView = new UIView(rect); window.addSubview(mainView); UILabel title = new UILabel(rect); title.setText("Hello World!"); title.setTextAlignment( UITextAlignment.UITextAlignmentCenter); mainView.addSubview(title); window.makeKeyAndVisible(); } } Slide 7 © XMLVM.org
  8. 8. Tool Chain Overview “Hello World” Cocoa Java Java app. Compat Lib Cross-Compilation Desktop application ( iPhone emulator) “Hello World” Objective-C Objective-C Wrapper Classes. Native iPhone application Slide 8 © XMLVM.org
  9. 9. Challenges  Design goals: – If possible, Java API mimics 1:1 the Objective-C API. – Make use of strongly typed interfaces.  Objective-C challenges: – Namespaces, method overloading: use name-mangling. – Garbage collection: use Objective-C reference counting. – No static member variables: use global variables.  Cocoa Touch challenges: – Makes use of C-functions (e.g., CGColorCreate) – Uses value types (e.g., CGRect) – Uses pointers for output parameters (e.g., NSURLConnection) – Makes heavy use of delegation (e.g., data source for UITableView) Slide 9 © XMLVM.org
  10. 10. Tool Chain Overview // Java class Calc { static int add(int x, int y) { return x + y; } } Calc.java Calc.class javac XMLVM Calc.xmlvm Slide 10 © XMLVM.org
  11. 11. XMLVM of Class Calc <?xml version="1.0" encoding="UTF-8"?> <xmlvm xmlns:dex="http://xmlvm.org/dex"> <class name="Calc"> <method name="add" isStatic="true"> <signature> <parameter type="int" /> <parameter type="int" /> <return type="int" /> </signature> <dex:code register-size="3"> <dex:add-int vx="0" vy="1" vz="2" /> <dex:return vx="0" vx-type="int" /> </dex:code> </method> </class> </xmlvm> Slide 11 © XMLVM.org
  12. 12. Cross-Compiling XMLVM to Objective-C  Simply map DEX registers to variables of the target language!  These translations are done using XSLT.  Mappings exist for different languages.  The XSLT excerpt below demonstrates the translation of <dex:add-int/> (Integer add) to Objective-C. // Objective-C typedef union { id o; int i; _r0.i = _r1.i + _r2.i; float f; double d; } XMLVMElem; <!– XSL template --> <xsl:template match="dex:add-int"> <xsl:text>_r</xsl:text> <xsl:value-of select="@vx"/> <xsl:text>.i = _r</xsl:text> <xsl:value-of select="@vy"/> <xsl:text>.i + _r</xsl:text> <xsl:value-of select="@vz"/> <xsl:text>.i;</xsl:text> Slide 12 © XMLVM.org </xsl:template>
  13. 13. Class Calc in Objective-C // Objective-C generated via stylesheet @implementation Calc + (int) add___int_int :(int)n1 :(int)n2 { XMLVMElem _r0; XMLVMElem _r1; XMLVMElem _r2; _r1.i = n1; _r2.i = n2; _r0.i = _r1.i + _r2.i; return _r0.i; } @end Slide 13 © XMLVM.org
  14. 14. Android Compatibility Library Application Application Application Android API Android Android Android Compat-Lib Compat-Lib Common Layer API Cocoa Touch webOS Layer Layer Device API iPhone Palm Pre  Android Compatibility Library: – Offers Android API. – Device-independent portions of Android. – Builds upon a Common Layer API. – Written in Java. – Is cross-compiled to Objective-C. Slide 14 © XMLVM.org
  15. 15. Example 1: Button to UIButton Mapping package android.widget; public class Button extends View { protected UIButton button; // ... public void setOnClickListener(OnClickListener listener) { final OnClickListener theListener = listener; button.addTarget(new UIControlDelegate() { @Override public void raiseEvent() { theListener.onClick(Button.this); } }, UIControl.UIControlEventTouchUpInside); } } Slide 15 © XMLVM.org
  16. 16. Example 2: R-class // Generated by Android public final class R { public static final class drawable { public static final int ball=0x7f020001; // ... }  Resources in Android are referenced through R-class: ImageView image = …; image.setImageResource(R.drawable.ball);  Problem: map integer (0x7f020001) to file name (ball.png)  Solution: use Java reflection at runtime for the reverse mapping. String rClassName = "R$drawable"; Class<?> rClass = Class.forName(rClassName); Field[] fields = rClass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { String fieldName = fields[i].getName(); int id = fields[i].getInt(rClass); // ... } Slide 16 © XMLVM.org
  17. 17. Example 3: Layout Manager  Android uses declarative UI descriptions.  UI descriptions are saved in XML files.  Those files are copied to the iPhone.  At runtime, XML files are parsed and UI is built on the iPhone.  XMLVM cross-compiles Android’s implementation of the layout managers. <LinearLayout orientation="vertical” layout_width="fill_parent" layout_height="fill_parent"> <EditText ... /> <TextView ... /> <Button layout_width="wrap_content" layout_height="wrap_content” id="@+id/sayHello" text="Say Hello" ... /> <LinearLayout background="@drawable/xmlvm_logo" ... /> </LinearLayout> Slide 17 © XMLVM.org
  18. 18. Xokoban  Sokoban version developed for Android and cross-compiled to the iPhone and webOS.  Available in the Android Market and iPhone AppStore.  Features: – 2D animation. – Dialog boxes. – Accelerometer and swipe interface. – Landscape full-screen mode. – Saving of settings. Slide 18 © XMLVM.org
  19. 19. Legions  Implementation of the Stratego board game.  Developed for Android.  AI engine implemented by Vincent De Boer (3rd time Stratego world champion).  Android version of Legions uses: – Activity lifecycle. – Sound. – Animation. – Custom dialogs/toasts. – Declarative layouts using different layout managers. – File I/O. Slide 19 © XMLVM.org
  20. 20. Scope  Android API offers good support for different screen resolutions.  Cross-compilation of games is feasible.  Problem: widget-driven applications.  Solution: partial cross-compilation.  Benefits: – Code-reuse of Model/Controller. – Custom View for native look-and-feel. – Custom View can still be implemented in Java.  Idea: use iPhone widgets in conjunction with Android layout managers. Slide 20 © XMLVM.org
  21. 21. XMLVM Tool Chain .NET .exe Java Class File Ruby YARV MBEL BCEL Ruby XMLVMCLR XSL XMLVMCLR-DFA XSL XMLVMJVM DX BCEL XSL XMLVMDEX Java Class File XMLVMCLR XSL XSL XSL XSL MBEL Objective-C C++ JavaScript Python .NET .exe Slide 21 © XMLVM.org
  22. 22. XMLVM Tool Chain .NET .exe Java Class File Ruby YARV MBEL BCEL Ruby XMLVMCLR XSL XMLVMCLR-DFA XSL XMLVMJVM DX BCEL XSL XMLVMDEX Java Class File XMLVMCLR XSL XSL XSL XSL MBEL Objective-C C++ JavaScript Python .NET .exe Slide 22 © XMLVM.org
  23. 23. XMLVM Tool Chain .NET .exe Java Class File Ruby YARV MBEL BCEL Ruby XMLVMCLR XSL XMLVMCLR-DFA XSL XMLVMJVM DX BCEL XSL XMLVMDEX Java Class File XMLVMCLR XSL XSL XSL XSL MBEL Objective-C C++ JavaScript Python .NET .exe Slide 23 © XMLVM.org
  24. 24. XMLVM Tool Chain .NET .exe Java Class File Ruby YARV MBEL BCEL Ruby XMLVMCLR XSL XMLVMCLR-DFA XSL XMLVMJVM DX BCEL XSL XMLVMDEX Java Class File XMLVMCLR XSL XSL XSL XSL MBEL Objective-C C++ JavaScript Python .NET .exe Slide 24 © XMLVM.org
  25. 25. XMLVM Tool Chain .NET .exe Java Class File Ruby YARV MBEL BCEL Ruby XMLVMCLR XSL XMLVMCLR-DFA XSL XMLVMJVM DX BCEL XSL XMLVMDEX Java Class File XMLVMCLR XSL XSL XSL XSL MBEL Objective-C C++ JavaScript Python .NET .exe Slide 25 © XMLVM.org
  26. 26. Conclusions  Summary: – Java for the iPhone. – Android Compatibility Classes.  Benefits: – Leverage existing Android/Java skills. – Only knowledge of a single platform is required. – Reduces development costs. – Shortens time-to-market.  Commercial support at http://xmlvm.org/support/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×