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

Xamarin.Android SDK解説 @ YAP*C Tokyo 2016-07-02

Xamarin.Android SDK解説 @ YAP*C Tokyo 2016-07-02

Atsushi Eno

July 02, 2016
Tweet

More Decks by Atsushi Eno

Other Decks in Technology

Transcript

  1. Agenda Xamarin.Android Xamarin.Android "SDK" Build Xamarin.Android SDK Build Apps using

    Xamarin.Android SDK Xamarin.Android SDK internals, if time permits
  2. Xamarin.Android • .NET on Android ◦ Uses Mono ◦ Runtime

    is written in C, NDK-ready • Android API in .NET manner ◦ offers "native" Android experience ◦ properties, events, enums • IDE support (out of scope for today)
  3. API Availability • .NET System.* API • PCLs (x-plat libs)

    • Android API • Java Bindings • ...
  4. Dual VMs • XA runs .NET code as is. ◦

    no transpiler • provides Android API ◦ not just lang. runtime ◦ even Java.* API • How? will discuss later
  5. 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
  6. What is Xamarin.Android SDK for? You don't need it. No,

    you don't. Use it if you need OSS-ed SDK.
  7. Build System for Android and .NET • IntelliJ IDEA supports

    Gradle. • Android Gradle tasks extends it. • .NET uses MSBuild everywhere. • Xamarin.Android extends it. Xamarin.Android is NOT for "writing .NET Apps and Run in on Android" in a sense It is rather for "writing Android Apps, using .NET and C#/F#" You are supposed to understand How Android App Builds and Works!
  8. Building Android App using .NET Android App build is complicated

    ... Xamarin.Android needs more. XA MSBuild tasks deal with them.
  9. Setting up Xamarin.Android SDK There is no installable (binary) package

    yet. You are supposed to build it by yourself. And it won't work yet(!)
  10. Requirements, sorta • Mac or Linux ◦ My Dockerfile (Ubuntu

    16.04) • broadband network for gigabytes of downloads • 12GB~ local storage
  11. 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(!)
  12. Xamarin.Android project structures A Xamarin.Android project is a standard MSBuild

    project that can be loaded by [XS|VS]. (YourApp.sln) YourApp.csproj MainActivity.cs, ... Resources/*/*.(a)xml Properties/AndroidManifest.xml
  13. Minimum Sources <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <OutputType>Library</OutputType> <Configuration Condition="'$(Configuratin)' == ''">Debug</Configuration>

    <OutputPath>bin\$(Configuration)</OutputPath> <TargetFrameworkVersion>v6.0</TargetFrameworkVersion> </PropertyGroup> <ItemGroup> <Compile Include="MainActivity.cs" /> <Reference Include="System" /> <Reference Include="Mono.Android" /> </ItemGroup> <Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin. Android.CSharp.targets" /> </Project> using Android.App; using Android.OS; using Android.Widget; namespace YAPCApp1 { [Activity (MainLauncher = true)] public class MainActivity : Activity { protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); Toast.MakeText (this, "!!!", ToastLength.Long).Show (); } } } App.csproj MainActivity.cs
  14. Build and Run your app xabuild = xbuild (mono MSbuild)

    with required environment variables. $ xabuild [app.csproj] /t:SignAndroidPackage /p:Configuration=Release $ adb install bin/Release/{yourapp}.apk
  15. 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
  16. 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)
  17. 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
  18. 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
  19. Extending MSBuild Tasks • Typical MSBuild extensions ◦ create .targets

    file ◦ <Import> it in .*proj ◦ 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>
  20. References • Links ◦ Architecuture - Xamarin ◦ YAPF#31: How

    Xamarin.Android works [ja] ◦ オープンソース化された Xamarin.Androidの概要 ◦ Xamarin.Androidプロジェクトをテキストエディタで作っ てみよう ◦ JXUG#4: Binding Java Library ◦ Android Studio 2.0のInstant Runの仕組みを解読する (java Android build/run analysis) ◦ Xamarin.Androidのproguardサポートについて grimoire of Android @ TechBooster