Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How Xamarin.Android works [MOPCON 2014]

How Xamarin.Android works [MOPCON 2014]

Atsushi Eno

October 24, 2014

More Decks by Atsushi Eno

Other Decks in Programming


  1. one of Xamarin.Android dev. ex-Mono hacker Japanese, kind of living

    in Taiwan https://github.com/atsushieno @atsushieno http://goo.gl/ZvNBxN
  2. ◯ what Xamarin.Android is (briefly) ✕ how to use Xamarin

    You can ask ThinkPower (Xamarin partner in Taiwan) ◦ how Xamarin.Android works ✕ how Xamarin IDEs work (except for UI designer) What to discuss here (and what not) http://goo.gl/ZvNBxN
  3. About These Slides ... has more topics than I actually

    talk(!) coz life is too short ... You're welcome to catch me around and shoot questions! http://goo.gl/ZvNBxN
  4. Xamarin C# / .NET on mobiles (and Mac OS X)

    Write C# code to use native platform API C# code sharing across platforms (iOS, Android, Windows Phone) brings .NET oriented technology like MVVM architecture Use Xamarin Studio or Visual Studio as IDE. http://goo.gl/ZvNBxN
  5. .NET Framework VM technology based on ECMA 335 CLI -

    "CLR" CIL (MSIL) bytecode - .exe or .dll, also "managed code" VES (virtual execution system) - JIT, AOT Garbage Collection Platform Invoke - more platform friendly than Java Compilers: C# (ECMA 334), F#, ... Class Libraries (mscorlib.dll, System*.dll) http://goo.gl/ZvNBxN
  6. .NET and mobile .NET Framework - for Windows Desktop Silverlight

    - Browser RIA platform CoreCLR: subset of .NET Framework for small footprint Windows Phone Silverlight based API, extended for mobile (sensors, contacts etc.) .NET Framework Windows Phone Silverlight
  7. https://github.com/mono/mono .NET (ECMA C# / CLI) on Linux, Mac, Windows

    etc. Software stack: runtime "mono" (C) - VES (JIT/AOT), GC, P/Invoke compiler "mcs" (C#) - complete C# 5.0 (and almost 6) class libraries (C#) - .NET 4.5 core + various libs Mono: .NET everywhere http://goo.gl/ZvNBxN
  8. Mono Ecosystem GUI: GTK# (GTK+), MonoMac, Xwt (xplat UI) MonoDevelop

    IDE on top of GTK# Linux desktop apps Mono also embraces .NET ecosystem ASP.NET, NuGet, OSSes ... .NET Foundation COSCUP 2014: Hacking Mono and .NET http://goo.gl/ZvNBxN
  9. Xamarin mobile products Xamarin.iOS ("MonoTouch") Xamarin.Android ("MonoDroid" or "Mono for

    Android") platform API in C# / .NET (as well as .NET System.* API) Xamarin Studio (MonoDevelop + Xamarin addin) or Visual Studio addin http://goo.gl/ZvNBxN
  10. Android Java development Android SDK - Java API and build

    tools use Java SDK IDE: Eclipse ADT, Android Studio (or Ant/Gradle) write Java code, compile to "Dalvik VM" bytecode (dex) package as .apk http://goo.gl/ZvNBxN
  11. VMs run (compiled) dex Dalvik - JIT compiler based VM

    for odex ART - AOT compiler based VM for ELF (image:wikipedia) Android Runtimes
  12. Android native development Android NDK - build libs and/or apps

    in C++ toolchains - gcc / clang bionic libc Not all of Android API are supported NativeActivity - C++ Android API to build apps All Android apps conform to Android framework. ... which is Java based. Native apps too. http://goo.gl/ZvNBxN
  13. NativeActivity So, is it useful? "NativeActivity allows you to write

    a completely native activity" "It is possible to write a completely native application" It still runs on Dalvik/ART VM. "You should still create your project with Eclipse or <snip>" WTF, native app works like Java app... http://goo.gl/ZvNBxN
  14. Xamarin.Android: basic architecture 1) run mono VM for managed code,

    while JVM also runs no "transpiler" to dex CIL natively runs on mono 2) Use Java API [xamarin:architecuture] Mono.Android.dll - .NET friendly version of android.jar API (no NativeActivity / native API)
  15. Mono Runtime on Android libmono - mono has its "embedded

    API" mono runtime built with NDK and run on Android OS entirely written in C depends only on libc Android: Linux kernel + bionic libc http://goo.gl/ZvNBxN
  16. Mono.Android API .NET-ism: PascalCase, System.String etc., properties, events, enums Java.Lang.Object;

    every Java object is based on this class. Binding is not only for android.jar; any Java library can be bound. Though bindings have limitation. (e.g. on generics) http://goo.gl/ZvNBxN
  17. Development Environment use Xamarin Studio = MonoDevelop + Xamarin addin

    or Visual Studio (addin) on Windows (requires Business+ license) xbuild (mono MSBuild) or MSBuild is usable only with Business+ license... creating app is doable only if you can manually create .csproj... http://goo.gl/ZvNBxN
  18. Mono = native library "libmono" libmonodroid - glue C code

    that invokes Java methods throuh JNI funcs Need two way operations: MCW (Mono Callable Wrappers) - .NET code to invoke Java ACW (Android Callable Wrappers) - Java code to invoke .NET also called as JCW (Java Callable Wrapper) (1) Java-Mono Interop http://goo.gl/ZvNBxN
  19. apps are in C# using Mono.Android API Mono.Android API (ultimately)

    invokes JNI JNIEnv class - contains JNI C API equivalents MCW: invoke Java from Mono
  20. ACW: invoke Mono from Java also "JCW", "Java Activation" handle

    callbacks by Android framework e.g. user resumes app -> Activity.onResume() -> our Activity.OnResume() generates Java source for each Java-based object http://goo.gl/ZvNBxN
  21. ACW: invoke Mono from Java public class MainActivity extends android.app.Activity

    implements mono.android.IGCUserPeer { static final String type, __md_methods; // Register type when referenced by code static { type = "MyApp.MainActivity, MyApp, Version=, Culture=neutral, PublicKeyToken=null"; __md_methods = "n_onCreate:(Landroid/os/Bundle;)V:GetOnCreate_Landroid_os_Bundle_Handler\n" ; mono.android.Runtime.register (type, MainActivity.class, __md_methods); } // Register new instance to libmonodroid public MainActivity () throws java.lang.Throwable { super (); if (getClass () == MainActivity.class) mono.android.TypeManager.Activate (type, "", this, new java.lang.Object[] { }); } // implement onCreate() to invoke managed code via JNI public void onCreate (android.os.Bundle p0) { n_onCreate (p0); } // native method private native void n_onCreate (android.os.Bundle p0); // non-existent, registered by libmonodroid. ...
  22. (2) Application bootstrap How does .NET apps run? - Console

    Application - GUI Applications (winforms, Gtk#, Mac, Silverlight) : hook into message loop (win32 / glib / CFRunLoop / agcore) - ASP.NET Application - ISAPI / xsp / mod_mono / KRuntime - iOS - similar to Mac - Android - Zygote ActivityThread message loop
  23. Android Java bootstrap (rephrase: All android apps conform to Android

    framework) Zygote: Java app process manager App process is fork()-ed from Zygote. Application instance is created (referencing AndroidManifest.xml). ActivityManagerService handles app lifecycle. 6.深入了解ActivityManagerService
  24. AndroidManifest.xml in XA app <application a:name="mono.android.app.Application" ...> <activity a:name="myapp.MainActivity" ...>

    <provider a:name="mono.MonoRuntimeProvider" ...> <receiver a:name="mono.android.Seppuku" ...> XA has its own app class Activity element etc. is automatically generated content provider that initializes mono app killer signal (for debugger)
  25. Android app bootstrap details ActivityManagerService binds a new ActivityThread to

    the app, with content provider list from manifest. ActivityThread.handleBindApplication() creates custom Instrumetation if required Then Instrumentation.callApplicationOnCreate(app) Application.onCreate() initializes app's content providers. and thus initializes Mono
  26. XA bootstrap briefly (rephrase: All android apps conform to Android

    framework) Android Java framework launches app starts Application, initializes Provider, launch Activity. All described in auto-generated AndroidManifest.xml. mono.RuntimeProvider initializes monodroid runtime. (rephrase this too: Java classes are generated, and we use them)
  27. (3) Garbage Collection Interop Java.Lang.Object - has reference in both

    JVM and mono VM. What if JVM destroys an object that mono references? What if Mono destroys an object that Java references? ... no, we don't want either. Too advanced to discuss here, see: http://goo.gl/FgirvY http://goo.gl/ZvNBxN
  28. Android App packaging Apk is required for any Android app.

    Xamarin app too. mobile apps - better as small as possible Packaging is heavyweight process esp. for Xamarin Debug build: faster is better Release build: smaller is better
  29. Android Resources FindViewById(int) - expects int Resource ID so Xamarin

    apps need Resource IDs in C# int too res/layout/* -(aapt)-> R.java -(msbuild)-> Resource.designer.cs Res IDs should be const (switch-case requires consts) except for library project (static readonly int in Java too) Res IDs are regenerated by IDEs whenever layout XML is saved
  30. Build Process Xamarin app build is complicated aapt, Android resgen

    (R.java -> Resource.designer.cs) compile C# / F# generate ACW (dll -> java), compile java (ACW, additional Java sources) zip (* -> .apk) zipalign, sign XA uses MSBuild (console, XS, VS) skip build steps as much as possible to speed up build
  31. App Deployment Models "adb install" is slow. We want to

    skip it as long as we can. fast deployment - build and install apk only when required *.dll are not part of *.apk IDE/SDK needs to copy them to app storage before running app app requests "Shared Runtime" or "Platform" apk to give dlls at run time. embedded deployment - normal packaging, everything into *.apk *.dll are assets
  32. Packages for fast deployment run 1. Mono Shared Runtime apk

    App / API Level agnostic files: libmonodroid, System.*.dll 2. Platform apk (per API Level) per-API-Level files: Mono.Android.dll etc. 3. Application apk (user app) app files: minimum native code, classes.dex, resources
  33. Minimizing Xamarin App code Assembly Linker - can remove unused

    managed code like proguard shrinking code (upcoming) proguard support - can remove unused Java code complicated for XA; some config files auto-generated Heavyweight task - we don't want to run it every time → usually run only in Release build
  34. Debug and Release builds Debug - faster build is better

    / Release - small apk is better Debug: APK build is slow, so... we build apk for debugging only once: fast deployment "Debug Runtime apk" and "Platform apk" offer framework code (dlls) Release: Mono runtime and assemblies are big, so... we have "Linker" to shrink assemblies (too advanced to discuss details) Packaging and Deployment, briefly http://goo.gl/ZvNBxN
  35. Xamarin Android Player The default best emulator for Xamarin People

    don't know there were better emus and complained about them(!) accelerated x86 emulator based on VirtualBox Everyone knew it's doable, configuring it was not easy. we can still use Google emulators (x86/HAXM too) http://goo.gl/ZvNBxN
  36. Android UI Designer Xamarin Studio and VS addin support UI

    Designer implemented on Xwt (x-plat UI toolkit for Gtk#/Mac/WPF) AOSP layoutlib UI view drawing reimplemented using Java2D used by Eclipse ADT and Android Studio too, as well as Xamarin http://www.slideshare.net/peterbuck/developing-for-android http://goo.gl/ZvNBxN
  37. Xamarin Sketches Interactive shell for iOS and Android Bret Victor

    - Inventing on Principle (2012) then, Making Instant C# Viable (2012) then, Xcode Playground (2014) C# / F# REPL mono had C# since 2008 (especially "gsharp") http://goo.gl/ZvNBxN