Skip to content

Commit f5afaaf

Browse files
Fix Android 16 startup crashes and dependency injection error
1 parent 01a7b47 commit f5afaaf

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

osu.Android/OsuGameActivity.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,30 @@ protected override Framework.Game CreateGame()
6161
throw new InvalidOperationException("Framework tried to create a game twice.");
6262

6363
if (game == null)
64-
throw new InvalidOperationException("Game was not initialised in OnCreate.");
64+
throw new InvalidOperationException("Game was not initialised.");
6565

6666
gameCreated = true;
6767
return game;
6868
}
6969

7070
public OsuGameActivity()
7171
{
72+
game = new OsuGameAndroid(this);
7273
}
7374

7475
protected override void OnCreate(Bundle? savedInstanceState)
7576
{
7677
base.OnCreate(savedInstanceState);
7778

78-
game = new OsuGameAndroid(this);
79-
8079
// Initialise MAUI Essentials so that Battery, Connectivity and other platform
8180
// APIs can resolve the current Activity/context. Without this call the
8281
// BroadcastReceivers registered in the merged manifest (BatteryBroadcastReceiver,
8382
// EnergySaverBroadcastReceiver, ConnectivityBroadcastReceiver) will crash on
8483
// first use because the internal Platform.CurrentActivity is null.
8584
Microsoft.Maui.ApplicationModel.Platform.Init(this, savedInstanceState);
8685

86+
87+
8788
// OnNewIntent() only fires for an activity if it's *re-launched* while it's on top of the activity stack.
8889
// on first launch we still have to fire manually.
8990
// reference: https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent)

osu.Android/OsuGameAndroid.cs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,34 @@ public partial class OsuGameAndroid : OsuGame
2727
[Cached]
2828
private readonly OsuGameActivity gameActivity;
2929

30+
private readonly object packageInfoLock = new object();
3031
private PackageInfo? packageInfo;
3132
private bool packageInfoChecked;
3233

3334
private PackageInfo? getPackageInfo()
3435
{
35-
if (packageInfoChecked)
36-
return packageInfo;
37-
38-
try
39-
{
40-
packageInfo = gameActivity.PackageManager?.GetPackageInfo(gameActivity.PackageName!, 0);
41-
}
42-
catch (Exception e)
36+
lock (packageInfoLock)
4337
{
44-
Debug.WriteLine($"[osu!] Failed to retrieve package info: {e.Message}");
45-
}
46-
finally
47-
{
48-
packageInfoChecked = true;
49-
}
38+
if (packageInfoChecked)
39+
return packageInfo;
5040

51-
return packageInfo;
41+
try
42+
{
43+
// Use the activity instance directly instead of Application.Context to ensure
44+
// the PackageManager is accessible even on newer/stricter Android versions.
45+
packageInfo = gameActivity.PackageManager?.GetPackageInfo(gameActivity.PackageName!, 0);
46+
}
47+
catch (Exception e)
48+
{
49+
Debug.WriteLine($"[osu!] Failed to retrieve package info: {e.Message}");
50+
}
51+
finally
52+
{
53+
packageInfoChecked = true;
54+
}
55+
56+
return packageInfo;
57+
}
5258
}
5359

5460
public override Vector2 ScalingContainerTargetDrawSize => DrawWidth > 0 && DrawHeight > 0
@@ -109,12 +115,12 @@ public override Version AssemblyVersion
109115
}
110116

111117
[BackgroundDependencyLoader]
112-
private void load(OsuConfigManager config)
118+
private void load()
113119
{
114-
config.BindWith(OsuSetting.AndroidPerformanceMode, performanceMode);
115-
config.BindWith(OsuSetting.AndroidLowLatencyAudio, lowLatencyAudio);
116-
config.BindWith(OsuSetting.AndroidVulkanProbe, vulkanProbeEnabled);
117-
config.BindWith(OsuSetting.AudioOffset, audioOffset);
120+
LocalConfig.BindWith(OsuSetting.AndroidPerformanceMode, performanceMode);
121+
LocalConfig.BindWith(OsuSetting.AndroidLowLatencyAudio, lowLatencyAudio);
122+
LocalConfig.BindWith(OsuSetting.AndroidVulkanProbe, vulkanProbeEnabled);
123+
LocalConfig.BindWith(OsuSetting.AudioOffset, audioOffset);
118124
}
119125

120126
protected override void LoadComplete()

0 commit comments

Comments
 (0)