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

Xamarin.Android SDK @ community open camp 20160...

Atsushi Eno
August 21, 2016
2.3k

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

Atsushi Eno

August 21, 2016
Tweet

More Decks by Atsushi Eno

Transcript

  1. 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)
  2. 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
  3. 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
  4. 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.
  5. 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)
  6. 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
  7. 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.
  8. 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.
  9. 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!)
  10. 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)
  11. 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)
  12. 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(!)
  13. 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
  14. 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
  15. 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.
  16. 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... 加油!
  17. 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
  18. 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)
  19. 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
  20. 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
  21. 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>