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)

Most upvoted comments

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). NGEN output still required the JIT, and had a variety of tradeoffs (NGEN output 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.handle before 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:

doesn’t hybrid AoT just JIT the Android bindings (Mono.Android) and statically compile everything else?

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.

didn’t the ability to statically compile Android apps and strip the IL come as working features come with Mono when it was acquired by Microsoft?

.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.)

@gabriel-kozma alas, not yet. I’ll be looking into this issue this week, hopefully finding a clue or two

@grendello sounds good, thank you!

@gabriel-kozma: please keep in mind that Xamarin.Android end of support is on 2024-May-1, which is less than a year away. .NET Android on .NET 7 and later is getting most of our development resources.

Please migrate your app to .NET Android and let us know if HybridAOT works better for you there.

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