android: When building an App with Hybrid aot, the app crashes on startup with
Android application type
Classic Xamarin.Android (MonoAndroid12.0, etc.)
Affected platform version
Xamarin Android 12.2, 12.3 and 13.0
Description
After compiling my app with hybrid aot mode and llvm turned on, the app crashes on startup with the error
Nested exception detected.
Original Exception: at Android.Runtime.JNIEnvInit.Initialize (Android.Runtime.JNIEnvInit/JnienvInitializeArgs*) [0x000a3] in <56b2ba5e3196483aa45d2f5001dd2d37>:0
my release configuration is
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>false</DebugSymbols>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidDexTool>d8</AndroidDexTool>
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
<AndroidManagedSymbols>true</AndroidManagedSymbols>
<AndroidSupportedAbis>arm64-v8a</AndroidSupportedAbis>
<JavaMaximumHeapSize>1G</JavaMaximumHeapSize>
<AotAssemblies>true</AotAssemblies>
<AndroidAotMode>Hybrid</AndroidAotMode>
<AndroidAotAdditionalArguments>hybrid,no-write-symbols,nodebug</AndroidAotAdditionalArguments>
<EnableLLVM>true</EnableLLVM>
<BundleAssemblies>true</BundleAssemblies>
<AndroidKeyStore>True</AndroidKeyStore>
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
<AndroidLinkTool>r8</AndroidLinkTool>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AndroidPackageFormat>apk</AndroidPackageFormat>
<AndroidCreatePackagePerAbi>false</AndroidCreatePackagePerAbi>
<MandroidI18n />
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
<AndroidR8ExtraArguments>--no-tree-shaking</AndroidR8ExtraArguments>
<AndroidAapt2LinkExtraArgs>--no-version-vectors</AndroidAapt2LinkExtraArgs>
</PropertyGroup>
Steps to Reproduce
Archive app to release Install on device Try to run
Did you find any workaround?
Reverting to Xamarin.Android 12.1
Relevant log output
No response
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 27 (9 by maintainers)
A bit of history đ
âIn the beginningâ (.NET Framework 1.0) there was
NGEN.EXE, which would Ahead-of-Time compile an assembly (with various tradeoffs).NGENoutput still required the JIT, and had a variety of tradeoffs (NGENoutput was larger, and âcold startâ was slower than the JIT, etc.)MonoTouch in 2009 required âFullâ AOT â AOT everything â in order to work, as iOS did not support a JIT at all. This required a fair bit of work to get Mono to support Full AOT. However, the way it was implemented was as an âeither/orâ: you had Full AOT and no JIT, or JIT and no Full AOT. (Mono also had ânormalâ AOT as well, which was a different file format than Full AOT.)
Android supported a JIT, so Mono for Android didnât use Full AOT, and instead relied on a JIT, and in fact still requires a JIT for various constructs such as constructor invocation. (When Java instantiates a C# type, we use System.Reflection.Emit to create an âintermediateâ constructor which sets
Java.Lang.Object.handlebefore the C# constructor executes. This required a JIT, before the introduction of the IL interpreter, which {mostly} isnât used on Android.)âHybridâ AOT was a way to use the âFullâ AOT file format + runtime changes and also use a JIT. This was the only way to use Full AOT on Android, short of significant architectural changes to Xamarin.Android to remove the need for a JIT (which still havenât happened, and arenât planned to happen).
Full and Hybrid AOT work across assembly boundaries, requiring that the entire app be known (so you canât download assemblies at runtime and have them work w/o a JIT).
To answer your questions:
Not quite. Hybrid AOT AOTs all assemblies in the app, while allowing the JIT to be used for constructs which require the JIT, which are spread across all assemblies.
.NET Android nĂ©e Xamarin.Android nĂ©e Mono for Android use Mono(VM), and have always used Mono, so this question doesnât quite make sense. The Microsoft acquisition doesnât have a lot to do with Hybrid AOT or the way it works; the same wonderful Mono runtime people were involved both before and after acquisition. (There is also very large overlap before and after acquisition on the Android team.) âStatic compilationâ, while a feature of Mono, had tradeoffs that made it not quite right for use on Android. This is why the Mono team needed to create the âHybridâ variant for Android, with a different set of tradeoffs.
While Hybrid AOT shared many things with Full AOT, it also involved new code, and wasnât widely tested. When we did begin testing, we found various problems, which in turn required that we add various checks such as the XA1025 error (which is likely moot now, but wasnât moot then). This is why it shipped as âExperimentalâ: it works (-ish) for us (in some contexts), but our testing can only go so far. We wanted wider testing to know that it wouldnât break everybody in unexpected ways.
Hybrid AOT also had other âlimitationsâ/requirements, such as that an Android NDK was required to use it (large download) and you had to be a Visual Studio Enterprise customer to even try it. This further reduced the amount of possible external testing that could occur.
Which is where things largely stagnated: our priorities werenât on Hybrid AOT (fewer customers could use it), and we focused on Profiled AOT and reducing startup times and other things which would help more of our customers, while navigating the â.NET Transitionâ (engineering time âspentâ on migrating away from Mono to .NET MonoVM, âfixingâ our build system so that it could work under
dotnet build, etc., etc.) and later MAUI.The benefit of https://github.com/dotnet/runtime/issues/44855 is that it should be available to everyone, thus avoiding the âno widespread testingâ problem, and if it lands early enough, it might be something worth enabling by default. (Maybe. No promises.)
@grendello sounds good, thank you!
Hi @jonpryor, yeah, I intended to migrate as soon as possible, but HybridAOT is not working on .NET Android, thereâs a feature request open for 2.5 years and this has been preventing me and others to migrate
https://github.com/dotnet/runtime/issues/44855