Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] libxamarin-app.so generation (#2718)
Browse files Browse the repository at this point in the history
Xamarin.Android needs a lot of information in the runtime in order to
do its tasks.  Among this is are two bits of information that are
used during startup: type mappings (java-to-managed `.jm` files and
managed-to-java `.mj` files) and environment settings (variables and
system properties).  Historically, Xamarin.Android used these bits of
data (generated on the application build time) by writing them into
separate files (`*.mj`, `*.jm`, `environment`), storing them in the
`.apk`, and reading these files dynamically during startup.
an `environment`

However, none of this data is *actually* dynamic - it's known
beforehand and could be stored as static data right in the `.apk`.
The problem with this idea was that we had no way to put that
information into the application native bits at build time: it would
require the presence of a compiler (or native assembler) as well as
the native linker for the target platforms.  This would have required
having the Android NDK installed, which is a hefty requirement.

That said, the Android SDK currently ships a native linker within its
`build-tools` package, and we can also bundle within our packages the
`gas` native assembler for all the architectures we support.  This
allows us to generate simple native assembler code, compile it
and link into a shared library ***when packaging*** the application.

The way it works is that during the Xamarin.Android *SDK* build we
build a "stub" `libxamarin-app.so` library which the Xamarin.Android
`libmono-android.*.so` neé `libmonodroid.so` runtime libraries are
linked against.  The stub `libxamarin-app.so` library is *not*
distributed with the Xamarin.Android SDK; instead, the "real" version
is generated at build/packaging time and packaged with the runtime
within the `.apk`.  Since the "real" version is ABI-compatible with
the stub version, the dynamic loader doesn't care and loads and
resolves symbols just fine.

This allows us to dispense with memory allocation, loading
(potentially large) files from the `.apk` (lots of I/O, slow),
parsing text files to set environment variables (additional memory
allocation), etc.  All of the data is loaded for us by Android and we
simply access global variables from the `libxamarin-app.so` library.

The process, however, can be time consuming (sort of...) and in order
to allow fast deployment to work as quickly as possible, the debug
(and desktop/host - for the benefit of the UI Designer) builds of the
runtime also contain code to load both type maps and environment
variables from files stored inside one of the override directories.

The changes provide a speed-up of ~10ms, reducing the startup time
down to around 233ms for a release build of a Xamarin.Forms app on a
Pixel 3 XL.
  • Loading branch information
grendello authored and jonpryor committed Apr 17, 2019
1 parent 84c97da commit decfbcc
Show file tree
Hide file tree
Showing 50 changed files with 4,117 additions and 292 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ define CREATE_THIRD_PARTY_NOTICES_RULE
prepare-tpn:: $(1)

$(1) $(topdir)/$(1): build-tools/ThirdPartyNotices/ThirdPartyNotices.csproj \
$(wildcard external/*.tpnitems src/*.tpnitems) \
$(wildcard external/*.tpnitems src/*.tpnitems build-tools/*.tpnitems) \
$(TPN_LICENSE_FILES)
$(call CREATE_THIRD_PARTY_NOTICES,$(1),$(2),$(3),$(4))
endef # CREATE_THIRD_PARTY_NOTICES_RULE
Expand Down
6 changes: 6 additions & 0 deletions Xamarin.Android.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "api-merge", "build-tools\ap
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "remap-assembly-ref", "build-tools\remap-assembly-ref\remap-assembly-ref.csproj", "{C876DA71-8573-4CEF-9149-716D72455ED4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "fetch-windows-assemblers", "build-tools\fetch-windows-assemblers\fetch-windows-assemblers.csproj", "{09518DF2-C7B1-4CDB-849A-9F91F4BEA925}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{04E3E11E-B47D-4599-8AFC-50515A95E715}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Java.Interop", "external\Java.Interop\src\Java.Interop\Java.Interop.csproj", "{94BD81F7-B06F-4295-9636-F8A3B6BDC762}"
Expand Down Expand Up @@ -197,6 +199,10 @@ Global
{C876DA71-8573-4CEF-9149-716D72455ED4}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{C876DA71-8573-4CEF-9149-716D72455ED4}.Release|AnyCPU.ActiveCfg = Release|Any CPU
{C876DA71-8573-4CEF-9149-716D72455ED4}.Release|AnyCPU.Build.0 = Release|Any CPU
{09518DF2-C7B1-4CDB-849A-9F91F4BEA925}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU
{09518DF2-C7B1-4CDB-849A-9F91F4BEA925}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{09518DF2-C7B1-4CDB-849A-9F91F4BEA925}.Release|AnyCPU.ActiveCfg = Release|Any CPU
{09518DF2-C7B1-4CDB-849A-9F91F4BEA925}.Release|AnyCPU.Build.0 = Release|Any CPU
{94BD81F7-B06F-4295-9636-F8A3B6BDC762}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU
{94BD81F7-B06F-4295-9636-F8A3B6BDC762}.Debug|AnyCPU.Build.0 = Debug|Any CPU
{94BD81F7-B06F-4295-9636-F8A3B6BDC762}.Release|AnyCPU.ActiveCfg = Release|Any CPU
Expand Down
2 changes: 1 addition & 1 deletion build-tools/android-toolchain/android-toolchain.targets
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<ReplaceFileContents
SourceFile="..\scripts\XABuildConfig.cs.in"
DestinationFile="..\..\bin\Build$(Configuration)\XABuildConfig.cs"
Replacements="@NDK_MINIMUM_API_AVAILABLE@=$(_NDKMinimumApiAvailable);@NDK_RELEASE@=$(AndroidNdkVersion);@NDK_REVISION@=$(_NDKRevision);@NDK_VERSION_MAJOR@=$(_NDKVersionMajor);@NDK_VERSION_MINOR@=$(_NDKVersionMinor);@NDK_VERSION_MICRO@=$(_NDKVersionMicro);@NDK_ARMEABI_V7_API@=$(AndroidNdkApiLevel_ArmV7a);@NDK_ARM64_V8A_API@=$(AndroidNdkApiLevel_ArmV8a);@NDK_X86_API@=$(AndroidNdkApiLevel_X86);@NDK_X86_64_API@=$(AndroidNdkApiLevel_X86_64)">
Replacements="@NDK_MINIMUM_API_AVAILABLE@=$(_NDKMinimumApiAvailable);@NDK_RELEASE@=$(AndroidNdkVersion);@NDK_REVISION@=$(_NDKRevision);@NDK_VERSION_MAJOR@=$(_NDKVersionMajor);@NDK_VERSION_MINOR@=$(_NDKVersionMinor);@NDK_VERSION_MICRO@=$(_NDKVersionMicro);@NDK_ARMEABI_V7_API@=$(AndroidNdkApiLevel_ArmV7a);@NDK_ARM64_V8A_API@=$(AndroidNdkApiLevel_ArmV8a);@NDK_X86_API@=$(AndroidNdkApiLevel_X86);@NDK_X86_64_API@=$(AndroidNdkApiLevel_X86_64);@XA_SUPPORTED_ABIS@=$(AndroidSupportedTargetJitAbis.Replace(':',';'))">
</ReplaceFileContents>
</Target>
</Project>
35 changes: 35 additions & 0 deletions build-tools/fetch-windows-assemblers.tpnitems
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Condition=" '$(TpnIncludeExternalDependencies)' == 'True' ">
<!-- The CRC32 code comes from:
https://github.com/force-net/Crc32.NET/blob/fbc1061b0cb53df2322d5aed33167a2e6335970b/Crc32.NET/SafeProxy.cs
License: MIT (https://github.com/force-net/Crc32.NET/blob/fbc1061b0cb53df2322d5aed33167a2e6335970b/LICENSE)
-->
<ThirdPartyNotice Include="force-net/crc32.net">
<LicenseText>
The MIT License (MIT)

Copyright (c) 2016 force

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
</LicenseText>
<SourceUrl>https://github.com/force-net/Crc32.NET</SourceUrl>
</ThirdPartyNotice>
</ItemGroup>
</Project>
Loading

0 comments on commit decfbcc

Please sign in to comment.