Skip to content

Commit 93dd85e

Browse files
committed
Fix package manager registration for CoreCLR
1 parent 5574c31 commit 93dd85e

File tree

5 files changed

+72
-9
lines changed

5 files changed

+72
-9
lines changed

CLR-Host-Notes.md

+46
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,49 @@ be shared between all targets, which then can (if needed) translate it to the ta
3737
Runtime currently calls `abort()` in several places. This should probably become part of the host contract instead.
3838
Being part of the contract, the target platform could implement process termination on `abort()` in a uniform way
3939
(includes platform-specific logging, preparation etc)
40+
41+
## Issues and workarounds
42+
43+
### Trimmer issue (as of 14.02.2025)
44+
45+
It appears the trimmer removes a bit too much at this point. In order to make the application run with trimming, one
46+
needs to add the following to their .csproj (solution found by Ivan Povazan):
47+
48+
```xml
49+
<ItemGroup>
50+
<TrimmerRootDescriptor Include="MyRoots.xml" />
51+
</ItemGroup>
52+
```
53+
54+
and put the following in the `MyRoots.xml` file:
55+
56+
```xml
57+
<linker>
58+
<assembly fullname="Java.Interop">
59+
<type fullname="Java.Interop.JavaArray`1">
60+
<method name="get_IsReadOnly" />
61+
<method name="System.Collections.ICollection.get_Count" />
62+
<method name="System.Collections.ICollection.get_IsSynchronized" />
63+
<method name="System.Collections.ICollection.get_SyncRoot" />
64+
<method name="System.Collections.ICollection.CopyTo" />
65+
<method name="System.Collections.IEnumerable.GetEnumerator" />
66+
<method name="System.Collections.IList.get_IsFixedSize" />
67+
<method name="System.Collections.IList.get_Item" />
68+
<method name="System.Collections.IList.set_Item" />
69+
<method name="System.Collections.IList.Add" />
70+
<method name="System.Collections.IList.Contains" />
71+
<method name="System.Collections.IList.IndexOf" />
72+
<method name="System.Collections.IList.Insert" />
73+
<method name="System.Collections.IList.Remove" />
74+
<method name="System.Collections.IList.RemoveAt" />
75+
<method name="System.Collections.Generic.ICollection&lt;T&gt;.Add" />
76+
<method name="System.Collections.Generic.ICollection&lt;T&gt;.Remove" />
77+
<method name="System.Collections.Generic.IList&lt;T&gt;.Insert" />
78+
<method name="System.Collections.Generic.IList&lt;T&gt;.RemoveAt" />
79+
</type>
80+
<type fullname="Java.Interop.JavaObjectArray`1">
81+
<method name="Clear" />
82+
</type>
83+
</assembly>
84+
</linker>
85+
```

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ internal static bool ShouldWrapJavaException (Java.Lang.Throwable? t, [CallerMem
7777
return wrap;
7878
}
7979

80+
static void MonoDroidUnhandledException (Exception ex)
81+
{
82+
RuntimeNativeMethods.monodroid_unhandled_exception (ex);
83+
}
84+
8085
internal static void PropagateUncaughtException (IntPtr env, IntPtr javaThreadPtr, IntPtr javaExceptionPtr)
8186
{
8287
if (!JNIEnvInit.PropagateExceptions)
@@ -95,7 +100,17 @@ internal static void PropagateUncaughtException (IntPtr env, IntPtr javaThreadPt
95100
Logger.Log (LogLevel.Info, "MonoDroid", "UNHANDLED EXCEPTION:");
96101
Logger.Log (LogLevel.Info, "MonoDroid", javaException.ToString ());
97102

98-
RuntimeNativeMethods.monodroid_unhandled_exception (innerException ?? javaException);
103+
switch (JNIEnvInit.RuntimeType) {
104+
case DotNetRuntimeType.MonoVM:
105+
MonoDroidUnhandledException (innerException ?? javaException);
106+
break;
107+
case DotNetRuntimeType.CoreCLR:
108+
// TODO: what to do here?
109+
break;
110+
111+
default:
112+
throw new NotSupportedException ($"Internal error: runtime type {JNIEnvInit.RuntimeType} not supported");
113+
}
99114
} catch (Exception e) {
100115
Logger.Log (LogLevel.Error, "monodroid", "Exception thrown while raising AppDomain.UnhandledException event: " + e.ToString ());
101116
}

src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -673,10 +673,12 @@ XElement CreateApplicationElement (XElement manifest, string applicationClass, L
673673

674674
IList<string> AddMonoRuntimeProviders (XElement app)
675675
{
676-
if (AndroidRuntime != AndroidRuntime.MonoVM && AndroidRuntime != AndroidRuntime.CoreCLR) {
677-
//TODO: implement provider logic for non-Mono runtimes
678-
return [];
679-
}
676+
(string packageName, string className) = AndroidRuntime switch {
677+
AndroidRuntime.MonoVM => ("mono", "MonoRuntimeProvider"),
678+
AndroidRuntime.CoreCLR => ("mono", "MonoRuntimeProvider"),
679+
AndroidRuntime.NativeAOT => ("net.dot.jni.nativeaot", "NativeAotRuntimeProvider"),
680+
_ => throw new NotSupportedException ($"Internal error: unsupported runtime type: {AndroidRuntime}")
681+
};
680682

681683
app.Add (CreateMonoRuntimeProvider ($"{packageName}.{className}", null, --AppInitOrder));
682684

src/java-runtime/java/mono/android/MonoPackageManager.java

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class MonoPackageManager {
2727

2828
public static void LoadApplication (Context context)
2929
{
30+
Log.w ("XAMONO", "MonoPackageManager.LoadApplication: start");
3031
synchronized (lock) {
3132
android.content.pm.ApplicationInfo runtimePackage = context.getApplicationInfo ();
3233
String[] apks = null;

src/java-runtime/java/mono/android/clr/MonoPackageManager.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
import mono.android.Runtime;
1919
import mono.android.DebugRuntime;
2020
import mono.android.BuildConfig;
21+
import net.dot.android.ApplicationRegistration;
2122

2223
public class MonoPackageManager {
2324

2425
static Object lock = new Object ();
2526
static boolean initialized;
2627

27-
static android.content.Context Context;
28-
2928
public static void LoadApplication (Context context)
3029
{
3130
Log.w ("XACLR", "MonoPackageManager.LoadApplication: start");
@@ -42,7 +41,7 @@ public static void LoadApplication (Context context)
4241
}
4342

4443
if (context instanceof android.app.Application) {
45-
Context = context;
44+
ApplicationRegistration.Context = context;
4645
}
4746
if (!initialized) {
4847
Log.w ("XACLR", "MonoPackageManager.LoadApplication: initializing");
@@ -89,7 +88,7 @@ public static void LoadApplication (Context context)
8988
);
9089

9190
Log.w ("XACLR", "MonoPackageManager.LoadApplication: call registerApplications");
92-
net.dot.android.ApplicationRegistration.registerApplications ();
91+
ApplicationRegistration.registerApplications ();
9392

9493
Log.w ("XACLR", "MonoPackageManager.LoadApplication: initialized");
9594
initialized = true;

0 commit comments

Comments
 (0)