Share
Embed
Transcript
27 slides
-
Agenda Xamarin.Android Xamarin.Android "SDK" Build Xamarin.Android SDK Build Apps using
Xamarin.Android SDK Xamarin.Android SDK internals, if time permits -
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) -
API Availability • .NET System.* API • PCLs (x-plat libs)
• Android API • Java Bindings • ... -
Dual VMs • XA runs .NET code as is. ◦
no transpiler • provides Android API ◦ not just lang. runtime ◦ even Java.* API • How? will discuss later -
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 -
What is Xamarin.Android SDK for? You don't need it. No,
you don't. Use it if you need OSS-ed SDK. -
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! -
Building Android App using .NET Android App build is complicated
... Xamarin.Android needs more. XA MSBuild tasks deal with them. -
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(!) -
Requirements, sorta • Mac or Linux ◦ My Dockerfile (Ubuntu
16.04) • broadband network for gigabytes of downloads • 12GB~ local storage -
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(!) -
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 -
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 -
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 -
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 -
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) -
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 -
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 -
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> -
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