Skip to content

Commit dcff35c

Browse files
[Mono.Android] fix $DOTNET_STARTUP_HOOKS on CoreCLR (#10755)
Context: #10699 Bringing useful changes from #10699 to main, such as fixing CoreCLR behavior and updating tests to verify setting `$DOTNET_STARTUP_HOOKS` works as intended for CoreCLR and NativeAOT. I will send a future PR to dotnet/runtime to fix Mono.
1 parent 7e9c52c commit dcff35c

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

src/Mono.Android/Android.Runtime/JNIEnvInit.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,11 @@ static void RunStartupHooks ()
204204
return;
205205
}
206206

207-
// Pass empty string for diagnosticStartupHooks parameter
208-
// The method will read STARTUP_HOOKS from AppContext internally
209-
method.Invoke (null, [ "" ]);
207+
// ProcessStartupHooks accepts startup hooks directly via parameter.
208+
// It will also read STARTUP_HOOKS from AppContext internally.
209+
// Pass DOTNET_STARTUP_HOOKS env var value so it works without needing AppContext setup.
210+
string? startupHooks = Environment.GetEnvironmentVariable ("DOTNET_STARTUP_HOOKS");
211+
method.Invoke (null, [ startupHooks ?? "" ]);
210212
}
211213

212214
static void SetSynchronizationContext () =>

tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@
5252
<StartupHookSupport>true</StartupHookSupport>
5353
</PropertyGroup>
5454

55-
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
55+
<ItemGroup>
5656
<!-- trying to track:
5757
JNI ERROR (app bug): accessed deleted Global 0x3056
5858
-->
59-
<AndroidEnvironment Include="env.txt" />
59+
<AndroidEnvironment Include="env.txt" Condition=" '$(Configuration)' == 'Debug' " />
60+
<AndroidEnvironment Include="hotreload.env" />
6061
</ItemGroup>
6162

6263
<ItemGroup>
@@ -231,7 +232,8 @@
231232
<RuntimeHostConfigurationOption Include="test_bool" Value="true" />
232233
<RuntimeHostConfigurationOption Include="test_integer" Value="42" />
233234
<RuntimeHostConfigurationOption Include="test_string" Value="foo" />
234-
<RuntimeHostConfigurationOption Include="STARTUP_HOOKS" Value="StartupHook" />
235+
<!-- Set STARTUP_HOOKS via RuntimeHostConfigurationOption for MonoVM (read via AppContext.GetData) -->
236+
<RuntimeHostConfigurationOption Include="STARTUP_HOOKS" Value="StartupHook" Condition=" '$(UseMonoRuntime)' == 'true' " />
235237
</ItemGroup>
236238

237239
<ItemGroup Condition=" '$(AndroidPackageFormat)' != 'aab' ">

tests/Mono.Android-Tests/Mono.Android-Tests/System/StartupHookTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ namespace SystemTests
77
[TestFixture]
88
public class StartupHookTest
99
{
10+
[Test]
11+
public void FeatureFlagIsEnabled ()
12+
{
13+
// NOTE: this is set to true in tests\Mono.Android-Tests\Mono.Android-Tests\Mono.Android.NET-Tests.csproj
14+
Assert.IsTrue (Microsoft.Android.Runtime.RuntimeFeature.StartupHookSupport, "RuntimeFeature.StartupHookSupport should be true");
15+
}
16+
17+
[Test]
18+
public void EnvironmentVariableIsSet ()
19+
{
20+
var value = Environment.GetEnvironmentVariable ("DOTNET_STARTUP_HOOKS");
21+
Assert.AreEqual ("StartupHook", value, "DOTNET_STARTUP_HOOKS should be set to 'StartupHook'");
22+
}
23+
1024
[Test]
1125
public void IsInitialized ()
1226
{
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
# Environment Variables and system properties
22
# debug.mono.log=gref,default
33
debug.mono.debug=1
4-
DOTNET_STARTUP_HOOKS=StartupHook
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DOTNET_STARTUP_HOOKS=StartupHook

0 commit comments

Comments
 (0)