Skip to content

Commit 67bf7d8

Browse files
[Mono.Android] fix crash on startup with EnableLLVM
`dotnet new android` apps would crash on startup when built with `-c Release -p:EnableLLVM=true`: 07-20 08:55:44.642 2983 2983 F monodroid-assembly: Internal p/invoke symbol 'java-interop @ java_interop_jvm_list' (hash: 0x58c48fc8b89cb484) not found in compile-time map. ... 07-20 08:55:44.834 3004 3004 F DEBUG : signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr -------- 07-20 08:55:44.834 3004 3004 F DEBUG : Abort message: 'Internal p/invoke symbol 'java-interop @ java_interop_jvm_list' (hash: 0x58c48fc8b89cb484) not found in compile-time map.' `java_interop_jvm_list` should *never* be called on Android, it is the result of `new AndroidRuntime()` having not been called yet? The problem being that the static `Android.App.Application.cctor` was somehow running *before* `JNIEnv.Initialize()` was complete? And this only happens with LLVM? Reviewing the code: [UnmanagedCallersOnly] internal static unsafe void Initialize (JnienvInitializeArgs* args) { //... SynchronizationContext.SetSynchronizationContext (Android.App.Application.SynchronizationContext); } We do indeed access `Android.App.Application`... To fix this, we can move this to a new method decorated with `MethodImplOptions.NoInlining`. Apps built with LLVM now launch for me. We can also enable the LLVM `Mono.Android-Tests` again.
1 parent 3e4686c commit 67bf7d8

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

build-tools/automation/azure-pipelines.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,13 +691,11 @@ stages:
691691

692692
- template: yaml-templates/apk-instrumentation.yaml
693693
parameters:
694-
# TODO: disable LLVM test, see: https://github.com/dotnet/runtime/issues/68914
695-
condition: false
696694
configuration: $(XA.Build.Configuration)
697695
testName: Mono.Android.NET_Tests-AotLlvm
698696
project: tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj
699697
testResultsFiles: TestResult-Mono.Android.NET_Tests-$(XA.Build.Configuration)AotLlvm.xml
700-
extraBuildArgs: -p:TestsFlavor=AotLlvm -p:EnableLlvm=true
698+
extraBuildArgs: -p:TestsFlavor=AotLlvm -p:EnableLlvm=true -p:AndroidEnableProfiledAot=false
701699
artifactSource: bin/Test$(XA.Build.Configuration)/$(DotNetTargetFramework)-android/Mono.Android.NET_Tests-Signed.aab
702700
artifactFolder: $(DotNetTargetFramework)-AotLlvm
703701
useDotNet: true

src/Mono.Android/Android.Runtime/JNIEnv.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,17 @@ internal static unsafe void Initialize (JnienvInitializeArgs* args)
196196
}
197197

198198
#if !MONOANDROID1_0
199-
SynchronizationContext.SetSynchronizationContext (Android.App.Application.SynchronizationContext);
199+
SetSynchronizationContext ();
200200
#endif
201201
}
202202

203+
#if !MONOANDROID1_0
204+
// NOTE: prevents Android.App.Application static ctor from running
205+
[MethodImpl (MethodImplOptions.NoInlining)]
206+
static void SetSynchronizationContext () =>
207+
SynchronizationContext.SetSynchronizationContext (Android.App.Application.SynchronizationContext);
208+
#endif
209+
203210
internal static void Exit ()
204211
{
205212
/* Manually dispose surfaced objects and close the current JniEnvironment to

0 commit comments

Comments
 (0)