Slide 1

Slide 1 text

How Xamarin.Android works MOPCON 2014 @atsushi_eno atsushi在

Slide 2

Slide 2 text

one of Xamarin.Android dev. ex-Mono hacker Japanese, kind of living in Taiwan @atsushieno

Slide 3

Slide 3 text

Slide 4

Slide 4 text

◯ 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)

Slide 5

Slide 5 text

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!

Slide 6

Slide 6 text


Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

.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)

Slide 9

Slide 9 text

.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

Slide 10

Slide 10 text .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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

X.Android Architecture

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

VMs run (compiled) dex Dalvik - JIT compiler based VM for odex ART - AOT compiler based VM for ELF (image:wikipedia) Android Runtimes

Slide 16

Slide 16 text

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.

Slide 17

Slide 17 text

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 " WTF, native app works like Java app...

Slide 18

Slide 18 text

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)

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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)

Slide 21

Slide 21 text

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...

Slide 22

Slide 22 text

Java Integration

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

ACW: invoke Mono from Java public class MainActivity extends implements { 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" ; (type, MainActivity.class, __md_methods); } // Register new instance to libmonodroid public MainActivity () throws java.lang.Throwable { super (); if (getClass () == MainActivity.class) (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. ...

Slide 27

Slide 27 text

(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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

AndroidManifest.xml in XA app XA has its own app class Activity element etc. is automatically generated content provider that initializes mono app killer signal (for debugger)

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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)

Slide 32

Slide 32 text

(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:

Slide 33

Slide 33 text

Packaging & Deployment

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Android Resources FindViewById(int) - expects int Resource ID so Xamarin apps need Resource IDs in C# int too res/layout/* -(aapt)-> -(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

Slide 36

Slide 36 text

Build Process Xamarin app build is complicated aapt, Android resgen ( -> 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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Dev Tools Breakdown

Slide 42

Slide 42 text

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)

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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")

Slide 45

Slide 45 text