Slide 1

Slide 1 text

Xamarin.Android SDK Atsushi Eno

Slide 2

Slide 2 text

Xamarin.Android ● Write Android apps using Android API on .NET platform in C# ○ not for running .NET Fx apps on Android: No WPF, no Metro, no UWP ● Reuse existing platform- independent .NET code ● Take full advantage of IDE integration (out of scope for today)

Slide 3

Slide 3 text

Xamarin.Android SDK ● Essential command line tools to build Xamarin.Android apps for releases. ○ No IDE support (no debugger UI, no UI designer, no profiler UI) ● Part of "Open Sourced" Xamarin Platforms Mono Xamarin.Android Components MonoDevelop Xamarin Studio (MD Addins) XamarinVS Visual Studio

Slide 4

Slide 4 text

What does it look like? Let's see... ● create simple Xamarin.Android app from scratch ● build it with xamarin-android ● run it on Android emulator or device

Slide 5

Slide 5 text

What is Xamarin.Android SDK for? You don't need it. No, you don't. Use it if you need OSS-ed SDK. Or you're on Linux.

Slide 6

Slide 6 text

Xamarin.Android Build fundamentals

Slide 7

Slide 7 text

Xamarin.Android: Dual VMs ● XA runs .NET code as is. ○ no transpiler ● provides Android API ○ not just lang. runtime ○ even Java.* API => we build and bundle mono (Not going to explain "how" within the session)

Slide 8

Slide 8 text

API Availability (and who builds them) ● .NET System.* API (Mono, part of XA) ● PCLs (x-plat libs) (we can already build them) ● Android API (part of XA) ● Java Bindings ○ build with XA ● .NET Libs for XA ○ build with XA

Slide 9

Slide 9 text

Basic XA Application Structure ● A Xamarin.Android app is a standard Android application ○ Has AndroidManifest.xml, classes.dex, packaged resources... ○ Launches within standard Android application loop (Zygote, ActivityThread, ...) ● initializes mono runtime at startup, makes it possible to run MSIL code Everything is packaged within APK in Android manner.

Slide 10

Slide 10 text

Build Systems Android SDK (Java): ● IntelliJ IDEA supports Gradle. ● Android Gradle tasks extends it. ● Android Studio integrates Gradle. Xamarin.Android SDK (.NET) ● .NET uses MSBuild everywhere. ● Xamarin.Android extends it. ● Xamarin Studio and Visual Studio integrate MSBuild. ...so, you are supposed to understand How Android App Builds and Works... .., if you are really stuck at what's going on during builds.

Slide 11

Slide 11 text

Building Android App using .NET Android App build is complicated ... Xamarin.Android needs more. XA MSBuild tasks deal with them. (not going to explain the build task details within the session - ask me later!)

Slide 12

Slide 12 text

Off-Topic: Special Android Builds for debugging Java - Instant Run Xamarin.Android - Fast Deployment Build (non-OSS) ● builds APK, but it does not run by itself ● needs separate dex uploads ● reuses build cache as much as possible (Not for this session - it is NOT part of Xamarin.Android SDK)

Slide 13

Slide 13 text

Building Xamarin.Android SDK

Slide 14

Slide 14 text

Setting up Xamarin.Android SDK Requirements, kind of ● Mac or Linux ● broadband network for gigabytes of downloads ● 13GB~ local storage There is no installable package for Linux yet. (There will be, depending on how Xamarin is serious about Linux...) You are supposed to build it by yourself. And there may be often broken builds yet (still early dev. stage)

Slide 15

Slide 15 text

Checkout and Build ● git clone https://github.com/xamarin/xamarin-android.git ● cd xamarin-android ● make prepare ● make Checks out mono, fsharp, and all submodules Automatically downloads Android SDK & NDK Builds mono runtime * supported ABIs i.e. it takes forever to build(!)

Slide 16

Slide 16 text

xamarin-android build outputs in (xamarin-android)/bin/Debug: ● bin, include - scripts and C includes. Skip them, not very important ● lib ○ mandroid - individual build tools and their deps used by MSBuild extensions. ○ xbuild/Xamarin/Android - contains MSBuild extensions. ■ lib/{arch} - contains NDK-built and host(Linux/Mac)-built mono and monodroid native libs. ○ xbuild-frameworks/MonoAndroid ■ v1.0 - System.* and co. ● Facades, RedistList - PCL facades for XA ■ v7.0 etc. - Mono.Android.dll etc., per API Level

Slide 17

Slide 17 text

Build and Run your app with xamarin-android Remeber that xamarin-android has no IDE support... create project elsewhere (!) You need xabuild: xbuild (mono MSbuild) with required environment variables. $ xabuild [app.csproj] /t:SignAndroidPackage /p:Configuration=Release $ adb install bin/Release/{yourapp}.apk

Slide 18

Slide 18 text

What else you can build with Xamarin.Android SDK ● Xamarin.Android Library (i.e. dll that targets Xamarin.Android framework) ● Java Binding Library Just build .csproj (or .fsproj for non-bindings) using xabuild.

Slide 19

Slide 19 text

Summary so far xamarin-android SDK is essential to open sourcing. You could (had better) use Visual Studio or Xamarin Studio to develop apps. Building it is easy (yet early stage). Using it... 加油!

Slide 20

Slide 20 text

XA Internals / XA SDK Internals

Slide 21

Slide 21 text

Mono<->Java Interop Mono->Java ● used by app, to call Android API ● JNI via P/Invoke Java->Mono ● Android Framework only calls Java objects, so we need them ● MSBuild tasks generate and package Java stubs in apk ○ native methods invoke java-interop functions libmonodroid: App Bootstrap ● mono in AndroidManifest.xml

Slide 22

Slide 22 text

Mono<->Java Interop

Slide 23

Slide 23 text

Java Binding generator ● a core part of the product ● also generates Mono.Android.dll ● Can bind arbitrary Android libs (jar/aar) ○ XamarinComponents ● generated code makes use of JNIEnv class (Java Interop) generate Java API XML description use jar2xml (old) or class-parse (new) ↓ modify API by metadata fixup ↓ generator: parse API XML and generate C# ↓ compile C# (with jars embedded in DLL)

Slide 24

Slide 24 text

Resource.Designer.cs (R.java) ● Resource/*/* => res/*/* (rename files) ○ to conform to Android standard ○ remember original filenames ● res/*/* => R.java ○ using aapt (resource compiler in Android SDK) ● R.java => Resource.Designer.cs (or .fs) ○ Parse Java source with simple regex and get resource IDs ○ restore original filenames to generate C#(F#) field identifiers ● Then your code can use Resource.Id.YourButton

Slide 25

Slide 25 text

Assembly Linking ● It's like Proguard in Java land. ● Release build assemblies should be as small as possible. ● Remove unused code from DLLs using mark and sweep technique ● /p:AndroidLinkMode=[SdkOnly|All] ● We support proguard too ○ xbuild /p:AndroidEnableProguard=True ○ To shrink jars from binding AND shrink DLLs more. (proguard cfg generator as part of linker feature) Mono.Android.dll original 30MB App.dll uses these Mono.Android.dll shrunk, 3MB

Slide 26

Slide 26 text

How we extend MSBuild Tasks ● Typical MSBuild extensions ○ create .targets file ○ it in .csproj / .fsproj ○ write custom Task classes ● Aapt task, Javac task, CompileToDalvik task (dx), Proguard task ... ○ unify everything Xamarin.Android.Common.targets (tasks before standard C# builds) $(BuildDependsOn); (tasks after standard C# builds)