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

Xamarin.Android SDK @ community open camp 20160827 @ 中研院

Atsushi Eno
August 21, 2016

Xamarin.Android SDK @ community open camp 20160827 @ 中研院

Atsushi Eno

August 21, 2016


  1. Xamarin.Android SDK Atsushi Eno

  2. 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)
  3. 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
  4. 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
  5. 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.
  6. Xamarin.Android Build fundamentals

  7. 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)
  8. 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
  9. 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.
  10. 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.
  11. 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!)
  12. 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)
  13. Building Xamarin.Android SDK

  14. 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)
  15. 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(!)
  16. 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
  17. 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
  18. 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.
  19. 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... 加油!
  20. XA Internals / XA SDK Internals

  21. 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 <Provider> in AndroidManifest.xml
  22. Mono<->Java Interop

  23. 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)
  24. 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
  25. 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
  26. How we extend MSBuild Tasks • Typical MSBuild extensions ◦

    create .targets file ◦ <Import> it in .csproj / .fsproj ◦ write custom Task classes • Aapt task, Javac task, CompileToDalvik task (dx), Proguard task ... ◦ unify everything Xamarin.Android.Common.targets <PropertyGroup> <BuildDependsOn> (tasks before standard C# builds) $(BuildDependsOn); (tasks after standard C# builds) </BuildDependsOn> </PropertyGroup>