Skip to content

Commit f6139e7

Browse files
Squashed commit of the following:
commit 79b9908 Author: Jonathan Peppers <jonathan.peppers@microsoft.com> Date: Tue Mar 12 11:51:23 2024 -0500 Update justification message commit db695b4 Author: Jonathan Peppers <jonathan.peppers@microsoft.com> Date: Mon Mar 11 15:04:10 2024 -0500 [DAM] not needed on JavaCollection<T>, JavaList<T>, JavaDictionary<K,V> commit ffd33ed Author: Jonathan Peppers <jonathan.peppers@microsoft.com> Date: Mon Mar 11 15:02:09 2024 -0500 One less IL2068 suppression commit 3e34f0f Author: Jonathan Peppers <jonathan.peppers@microsoft.com> Date: Mon Mar 11 09:02:33 2024 -0500 Set `$(IlcTreatWarningsAsErrors)` commit e4bbe8c Author: Jonathan Peppers <jonathan.peppers@microsoft.com> Date: Mon Mar 11 08:43:49 2024 -0500 Changes to make diff in `AndroidEnvironment` smaller commit 6399051 Author: Jonathan Peppers <jonathan.peppers@microsoft.com> Date: Thu Feb 29 12:33:10 2024 -0600 [Mono.Android] is now "trimming safe" Fixes: #5652 Each trimming problem listed below. ~~ AndroidEnvironment ~~ src\Mono.Android\Android.Runtime\AndroidEnvironment.cs(373,19): error IL2057: Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type. src\Mono.Android\Android.Runtime\AndroidEnvironment.cs(379,22): error IL2057: Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type. src\Mono.Android\Android.Runtime\AndroidEnvironment.cs(342,20): error IL2057: Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String, Boolean)'. It's not possible to guarantee the availability of the target type. src\Mono.Android\Android.Runtime\AndroidEnvironment.cs(352,11): error IL2077: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The field 'Android.Runtime.AndroidEnvironment.httpMessageHandlerType' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. To fix these: * Use constant strings for calls like: Type.GetType ("System.Net.Http.HttpMessageHandler, System.Net.Http", throwOnError: false) Use `Type.IsAssignableFrom()` inline, and the trimmer now understands the code paths. There is a problem here if either of these are used with a custom type: * `$(AndroidHttpClientHandlerType)` * `$XA_HTTP_CLIENT_HANDLER_TYPE` This is a rarely used feature, I found one usage in GitHub: https://github.com/search?q=%3CAndroidHttpClientHandlerType%3E+NOT+Xamarin.Android.Net+NOT+System.Net.Http+NOT+Default&type=code Filed an issue to fix this in the future: * #8797 ~~ JavaConvert ~~ src\Mono.Android\Java.Interop\JavaConvert.cs(223,12): error IL2091: 'TResult' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'Java.Interop.JavaObjectExtensions._JavaCast<TResult>(IJavaObject)'. The generic parameter 'T' of 'Java.Interop.JavaConvert.FromJavaObject<T>(IJavaObject, out Boolean)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src\Mono.Android\Java.Interop\JavaConvert.cs(254,12): error IL2067: 'resultType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'Java.Interop.JavaObjectExtensions.JavaCast(IJavaObject, Type)'. The parameter 'targetType' of method 'Java.Interop.JavaConvert.FromJavaObject(IJavaObject, Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src\Mono.Android\Java.Interop\JavaConvert.cs(67,14): error IL3050: Using member 'System.Type.MakeGenericType(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. src\Mono.Android\Java.Interop\JavaConvert.cs(73,14): error IL3050: Using member 'System.Type.MakeGenericType(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. src\Mono.Android\Java.Interop\JavaConvert.cs(79,14): error IL3050: Using member 'System.Type.MakeGenericType(params Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. Adding attributes fixed some of these. I suppressed warnings for: * `Type.MakeGenericType()`: `JavaDictionary<,>`, `JavaList<>`, and `JavaCollection<>` use DynamicDependency to preserve `FromJniHandle` method. * `Type.GetElementType()`: for calling `MyJavaObject[]` constructors, the `MarkJavaObjects` trimmer step should preserve these constructors. This trickled over to require more attributes for: * `AdapterView` * `ArrayAdapter` * `AsyncTask` * `JavaCollection` * `JavaDictionary` * `JavaList` * `JavaList` * `JavaObjectExtensions` * `JavaSet` * `SparseArray` * `System.Linq\Extensions` ~~ JNIEnv ~~ src\Mono.Android\Android.Runtime\JNIEnv.cs(810,38): error IL3050: Using member 'System.Type.MakeArrayType()' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. src\Mono.Android\Android.Runtime\JNIEnv.cs(953,33): error IL3050: Using member 'System.Type.MakeArrayType()' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. src\Mono.Android\Android.Runtime\JNIEnv.cs(1078,44): error IL3050: Using member 'System.Type.MakeArrayType()' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. src\Mono.Android\Android.Runtime\JNIEnv.cs(1139,15): error IL2091: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'Java.Interop.JavaConvert.FromJavaObject<T>(IJavaObject)'. The generic parameter 'T' of 'Android.Runtime.JNIEnv.GetArray<T>(Object[])' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. src\Mono.Android\Android.Runtime\JNIEnv.cs(1060,14): error IL3050: Using member 'System.Array.CreateInstance(Type, Int32)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. src\Mono.Android\Android.Runtime\JNIEnv.cs(1065,14): error IL3050: Using member 'System.Array.CreateInstance(Type, Int32)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. src\Mono.Android\Android.Runtime\JNIEnv.cs(1257,23): error IL2091: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'Java.Interop.JavaConvert.FromJniHandle<T>(nint, JniHandleOwnership)'. The generic parameter 'T' of 'Android.Runtime.JNIEnv.CopyObjectArray<T>(nint, T[])' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. Adding some attributes fixed two of these. Suppress NativeAOT warnings, in source, for: * `Array.CreateInstance()` * `Type.MakeArrayType()` Link to an issue to fix in the future.
1 parent 4d1fca7 commit f6139e7

File tree

15 files changed

+233
-35
lines changed

15 files changed

+233
-35
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!-- Import this to enable trim warnings of all kinds -->
2+
<Project>
3+
<PropertyGroup>
4+
<!-- Sets assembly metadata, enable analyzers -->
5+
<IsTrimmable>true</IsTrimmable>
6+
<EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
7+
<EnableAotAnalyzer>true</EnableAotAnalyzer>
8+
<!-- In app projects, tells ILLink to emit warnings as errors -->
9+
<ILLinkTreatWarningsAsErrors>true</ILLinkTreatWarningsAsErrors>
10+
<!--
11+
Trim warnings, codes listed here:
12+
* https://github.com/dotnet/runtime/blob/7403a062960d092c73ce3f07d3ff323ffdf7de43/src/tools/illink/src/linker/Resources/Strings.resx
13+
* https://github.com/dotnet/docs/tree/9cb45cf9cd34f3b7259a023c3d92a124a87090d5/docs/core/deploying/trimming/trim-warnings
14+
-->
15+
<WarningsAsErrors>
16+
$(WarningsAsErrors);
17+
IL2000;IL2001;IL2002;IL2003;IL2004;
18+
IL2005;IL2006;IL2007;IL2008;IL2009;
19+
IL2010;IL2011;IL2012;IL2013;IL2014;
20+
IL2015;IL2016;IL2017;IL2018;IL2019;
21+
IL2020;IL2021;IL2022;IL2023;IL2024;
22+
IL2025;IL2026;IL2027;IL2028;IL2029;
23+
IL2030;IL2031;IL2032;IL2033;IL2034;
24+
IL2035;IL2036;IL2037;IL2038;IL2039;
25+
IL2040;IL2041;IL2042;IL2043;IL2044;
26+
IL2045;IL2046;IL2047;IL2048;IL2049;
27+
IL2050;IL2051;IL2052;IL2053;IL2054;
28+
IL2055;IL2056;IL2057;IL2058;IL2059;
29+
IL2060;IL2061;IL2062;IL2063;IL2064;
30+
IL2065;IL2066;IL2067;IL2068;IL2069;
31+
IL2070;IL2071;IL2072;IL2073;IL2074;
32+
IL2075;IL2076;IL2077;IL2078;IL2079;
33+
IL2080;IL2081;IL2082;IL2083;IL2084;
34+
IL2085;IL2086;IL2087;IL2088;IL2089;
35+
IL2090;IL2091;IL2092;IL2093;IL2094;
36+
IL2095;IL2096;IL2097;IL2098;IL2099;
37+
IL2100;IL2101;IL2102;IL2103;IL2104;
38+
IL2105;IL2106;IL2107;IL2108;IL2109;
39+
IL2110;IL2111;IL2112;IL2113;IL2114;
40+
IL2115;IL2116;IL2117;IL2118;IL2119;
41+
IL2120;IL2121;IL2122;IL2123;IL2124;
42+
IL2125;IL2126;IL2127;IL2128;IL2129;
43+
</WarningsAsErrors>
44+
<!-- In NativeAOT app projects, tells Ilc to emit warnings as errors -->
45+
<IlcTreatWarningsAsErrors>true</IlcTreatWarningsAsErrors>
46+
<!--
47+
NativeAOT warnings, codes listed here:
48+
* https://github.com/dotnet/docs/tree/9cb45cf9cd34f3b7259a023c3d92a124a87090d5/docs/core/deploying/native-aot/warnings
49+
-->
50+
<WarningsAsErrors>
51+
$(WarningsAsErrors);
52+
IL3050;IL3051;IL3052;IL3053;IL3054;IL3055;IL3056;
53+
</WarningsAsErrors>
54+
</PropertyGroup>
55+
</Project>

src/Mono.Android/Android.OS/AsyncTask.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ namespace Android.OS {
99

1010
[global::System.Runtime.Versioning.ObsoletedOSPlatform ("android30.0")]
1111
[Register ("android/os/AsyncTask", DoNotGenerateAcw=true)]
12-
public abstract class AsyncTask<TParams, TProgress, TResult> : AsyncTask {
12+
public abstract class AsyncTask<
13+
[DynamicallyAccessedMembers (Constructors)]
14+
TParams,
15+
[DynamicallyAccessedMembers (Constructors)]
16+
TProgress,
17+
[DynamicallyAccessedMembers (Constructors)]
18+
TResult
19+
> : AsyncTask {
20+
21+
const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;
1322

1423
static IntPtr java_class_handle;
1524
internal static IntPtr class_ref {

src/Mono.Android/Android.Runtime/AndroidEnvironment.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static class AndroidEnvironment {
2424
static IX509TrustManager? sslTrustManager;
2525
static KeyStore? certStore;
2626
static object lock_ = new object ();
27+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
2728
static Type? httpMessageHandlerType;
2829

2930
static void SetupTrustManager ()
@@ -335,11 +336,18 @@ static IWebProxy GetDefaultProxy ()
335336
[DynamicDependency (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof (Xamarin.Android.Net.AndroidMessageHandler))]
336337
static object GetHttpMessageHandler ()
337338
{
339+
// FIXME: https://github.com/xamarin/xamarin-android/issues/8797
340+
// Note that this is a problem for custom $(AndroidHttpClientHandlerType) or $XA_HTTP_CLIENT_HANDLER_TYPE
341+
[UnconditionalSuppressMessage ("Trimming", "IL2057", Justification = "DynamicDependency should preserve AndroidMessageHandler.")]
342+
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
343+
static Type TypeGetType (string typeName) =>
344+
Type.GetType (typeName, throwOnError: false);
345+
338346
if (httpMessageHandlerType is null) {
339347
var handlerTypeName = Environment.GetEnvironmentVariable ("XA_HTTP_CLIENT_HANDLER_TYPE")?.Trim ();
340348
Type? handlerType = null;
341349
if (!String.IsNullOrEmpty (handlerTypeName))
342-
handlerType = Type.GetType (handlerTypeName, throwOnError: false);
350+
handlerType = TypeGetType (handlerTypeName);
343351

344352
// We don't do any type checking or casting here to avoid dependency on System.Net.Http in Mono.Android.dll
345353
if (handlerType is null || !IsAcceptableHttpMessageHandlerType (handlerType)) {
@@ -369,13 +377,19 @@ static bool IsAcceptableHttpMessageHandlerType (Type handlerType)
369377
return true;
370378
}
371379

372-
static bool Extends (Type handlerType, string baseTypeName) {
380+
static bool Extends (
381+
Type handlerType,
382+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
383+
string baseTypeName)
384+
{
373385
var baseType = Type.GetType (baseTypeName, throwOnError: false);
374386
return baseType?.IsAssignableFrom (handlerType) ?? false;
375387
}
376388

377-
static Type GetFallbackHttpMessageHandlerType (string typeName = "Xamarin.Android.Net.AndroidMessageHandler, Mono.Android")
389+
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
390+
static Type GetFallbackHttpMessageHandlerType ()
378391
{
392+
const string typeName = "Xamarin.Android.Net.AndroidMessageHandler, Mono.Android";
379393
var handlerType = Type.GetType (typeName, throwOnError: false)
380394
?? throw new InvalidOperationException ($"The {typeName} was not found. The type was probably linked away.");
381395

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,27 @@
1616

1717
namespace Android.Runtime {
1818
public static partial class JNIEnv {
19+
const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;
20+
1921
[ThreadStatic]
2022
static byte[]? mvid_bytes;
2123

2224
public static IntPtr Handle => JniEnvironment.EnvironmentPointer;
2325

26+
static Array ArrayCreateInstance (Type elementType, int length) =>
27+
// FIXME: https://github.com/xamarin/xamarin-android/issues/8724
28+
// IL3050 disabled in source: if someone uses NativeAOT, they will get the warning.
29+
#pragma warning disable IL3050
30+
Array.CreateInstance (elementType, length);
31+
#pragma warning restore IL3050
32+
33+
static Type MakeArrayType (Type type) =>
34+
// FIXME: https://github.com/xamarin/xamarin-android/issues/8724
35+
// IL3050 disabled in source: if someone uses NativeAOT, they will get the warning.
36+
#pragma warning disable IL3050
37+
type.MakeArrayType ();
38+
#pragma warning restore IL3050
39+
2440
internal static IntPtr IdentityHash (IntPtr v)
2541
{
2642
return JNIEnvInit.LocalRefsAreIndirect ? RuntimeNativeMethods._monodroid_get_identity_hash_code (Handle, v) : v;
@@ -807,7 +823,7 @@ public static void CopyArray (IntPtr src, Array dest, Type? elementType = null)
807823
throw new ArgumentNullException ("dest");
808824

809825
if (elementType != null && elementType.IsValueType)
810-
AssertCompatibleArrayTypes (src, elementType.MakeArrayType ());
826+
AssertCompatibleArrayTypes (src, MakeArrayType (elementType));
811827

812828
if (elementType != null && elementType.IsArray) {
813829
for (int i = 0; i < dest.Length; ++i) {
@@ -950,7 +966,7 @@ public static void CopyArray (Array source, Type elementType, IntPtr dest)
950966
throw new ArgumentNullException ("elementType");
951967

952968
if (elementType.IsValueType)
953-
AssertCompatibleArrayTypes (elementType.MakeArrayType (), dest);
969+
AssertCompatibleArrayTypes (MakeArrayType (elementType), dest);
954970

955971
Action<Array, IntPtr> converter = GetConverter (CopyManagedToNativeArray, elementType, dest);
956972

@@ -1057,12 +1073,12 @@ public static void CopyArray<T> (T[] src, IntPtr dest)
10571073
}
10581074
} },
10591075
{ typeof (IJavaObject), (type, source, len) => {
1060-
var r = Array.CreateInstance (type!, len);
1076+
var r = ArrayCreateInstance (type!, len);
10611077
CopyArray (source, r, type);
10621078
return r;
10631079
} },
10641080
{ typeof (Array), (type, source, len) => {
1065-
var r = Array.CreateInstance (type!, len);
1081+
var r = ArrayCreateInstance (type!, len);
10661082
CopyArray (source, r, type);
10671083
return r;
10681084
} },
@@ -1075,7 +1091,7 @@ public static void CopyArray<T> (T[] src, IntPtr dest)
10751091
return null;
10761092

10771093
if (element_type != null && element_type.IsValueType)
1078-
AssertCompatibleArrayTypes (array_ptr, element_type.MakeArrayType ());
1094+
AssertCompatibleArrayTypes (array_ptr, MakeArrayType (element_type));
10791095

10801096
int cnt = _GetArrayLength (array_ptr);
10811097

@@ -1130,7 +1146,10 @@ static int _GetArrayLength (IntPtr array_ptr)
11301146
return ret;
11311147
}
11321148

1133-
public static T[]? GetArray<T> (Java.Lang.Object[] array)
1149+
public static T[]? GetArray<
1150+
[DynamicallyAccessedMembers (Constructors)]
1151+
T
1152+
> (Java.Lang.Object[] array)
11341153
{
11351154
if (array == null)
11361155
return null;
@@ -1244,7 +1263,10 @@ static IntPtr GetArrayElementClass<T>(T[] values)
12441263
return FindClass (elementType);
12451264
}
12461265

1247-
public static void CopyObjectArray<T>(IntPtr source, T[] destination)
1266+
public static void CopyObjectArray<
1267+
[DynamicallyAccessedMembers (Constructors)]
1268+
T
1269+
>(IntPtr source, T[] destination)
12481270
{
12491271
if (source == IntPtr.Zero)
12501272
return;

src/Mono.Android/Android.Runtime/JavaCollection.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace Android.Runtime {
1414
// java.util.Collection allows null values
1515
public class JavaCollection : Java.Lang.Object, System.Collections.ICollection {
1616

17+
internal const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;
18+
1719
internal static IntPtr collection_class = JNIEnv.FindClass ("java/util/Collection");
1820

1921
internal static IntPtr id_add;
@@ -148,6 +150,11 @@ internal Java.Lang.Object[] ToArray ()
148150
//
149151
public void CopyTo (Array array, int array_index)
150152
{
153+
[UnconditionalSuppressMessage ("Trimming", "IL2073", Justification = "JavaCollection<T> constructors are preserved by the MarkJavaObjects trimmer step.")]
154+
[return: DynamicallyAccessedMembers (Constructors)]
155+
static Type GetElementType (Array array) =>
156+
array.GetType ().GetElementType ();
157+
151158
if (array == null)
152159
throw new ArgumentNullException ("array");
153160
if (array_index < 0)
@@ -164,7 +171,7 @@ public void CopyTo (Array array, int array_index)
164171
JavaConvert.FromJniHandle (
165172
JNIEnv.GetObjectArrayElement (lrefArray, i),
166173
JniHandleOwnership.TransferLocalRef,
167-
array.GetType ().GetElementType ()),
174+
GetElementType (array)),
168175
array_index + i);
169176
JNIEnv.DeleteLocalRef (lrefArray);
170177
}
@@ -203,7 +210,10 @@ public static IntPtr ToLocalJniHandle (ICollection? items)
203210
}
204211

205212
[Register ("java/util/Collection", DoNotGenerateAcw=true)]
206-
public sealed class JavaCollection<T> : JavaCollection, ICollection<T> {
213+
public sealed class JavaCollection<
214+
[DynamicallyAccessedMembers (Constructors)]
215+
T
216+
> : JavaCollection, ICollection<T> {
207217

208218
public JavaCollection (IntPtr handle, JniHandleOwnership transfer)
209219
: base (handle, transfer)

src/Mono.Android/Android.Runtime/JavaDictionary.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Android.Runtime {
1212
// java.util.HashMap allows null keys and values
1313
public class JavaDictionary : Java.Lang.Object, System.Collections.IDictionary {
1414

15+
internal const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;
16+
1517
class DictionaryEnumerator : IDictionaryEnumerator {
1618

1719
IEnumerator simple_enumerator;
@@ -396,7 +398,12 @@ public static IntPtr ToLocalJniHandle (IDictionary? dictionary)
396398
// it may throw.
397399
//
398400
[Register ("java/util/HashMap", DoNotGenerateAcw=true)]
399-
public class JavaDictionary<K, V> : JavaDictionary, IDictionary<K, V> {
401+
public class JavaDictionary<
402+
[DynamicallyAccessedMembers (Constructors)]
403+
K,
404+
[DynamicallyAccessedMembers (Constructors)]
405+
V
406+
> : JavaDictionary, IDictionary<K, V> {
400407

401408
[Register (".ctor", "()V", "")]
402409
public JavaDictionary ()

src/Mono.Android/Android.Runtime/JavaList.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Android.Runtime {
1010
// java.util.ArrayList allows null values
1111
public partial class JavaList : Java.Lang.Object, System.Collections.IList {
1212

13+
internal const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;
1314
internal static readonly JniPeerMembers list_members = new XAPeerMembers ("java/util/List", typeof (JavaList), isInterface: true);
1415

1516
//
@@ -23,7 +24,10 @@ public partial class JavaList : Java.Lang.Object, System.Collections.IList {
2324
//
2425
// https://developer.android.com/reference/java/util/List.html?hl=en#get(int)
2526
//
26-
internal unsafe object? InternalGet (int location, Type? targetType = null)
27+
internal unsafe object? InternalGet (
28+
int location,
29+
[DynamicallyAccessedMembers (Constructors)]
30+
Type? targetType = null)
2731
{
2832
const string id = "get.(I)Ljava/lang/Object;";
2933
JniObjectReference obj;
@@ -266,14 +270,19 @@ public unsafe bool Contains (object? item)
266270

267271
public void CopyTo (Array array, int array_index)
268272
{
273+
[UnconditionalSuppressMessage ("Trimming", "IL2073", Justification = "JavaList<T> constructors are preserved by the MarkJavaObjects trimmer step.")]
274+
[return: DynamicallyAccessedMembers (Constructors)]
275+
static Type GetElementType (Array array) =>
276+
array.GetType ().GetElementType ();
277+
269278
if (array == null)
270279
throw new ArgumentNullException ("array");
271280
if (array_index < 0)
272281
throw new ArgumentOutOfRangeException ("array_index");
273282
if (array.Length < array_index + Count)
274283
throw new ArgumentException ("array");
275284

276-
var targetType = array.GetType ().GetElementType ();
285+
var targetType = GetElementType (array);
277286
int c = Count;
278287
for (int i = 0; i < c; i++)
279288
array.SetValue (InternalGet (i, targetType), array_index + i);
@@ -673,7 +682,10 @@ public virtual Java.Lang.Object [] ToArray ()
673682
}
674683

675684
[Register ("java/util/ArrayList", DoNotGenerateAcw=true)]
676-
public class JavaList<T> : JavaList, IList<T> {
685+
public class JavaList<
686+
[DynamicallyAccessedMembers (Constructors)]
687+
T
688+
> : JavaList, IList<T> {
677689

678690
//
679691
// Exception audit:

src/Mono.Android/Android.Runtime/JavaSet.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@ public static IntPtr ToLocalJniHandle (ICollection? items)
268268

269269
[Register ("java/util/HashSet", DoNotGenerateAcw=true)]
270270
// java.util.HashSet allows null
271-
public class JavaSet<T> : JavaSet, ICollection<T> {
271+
public class JavaSet<
272+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
273+
T
274+
> : JavaSet, ICollection<T> {
272275

273276
//
274277
// Exception audit:

src/Mono.Android/Android.Util/SparseArray.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23

34
using Android.Runtime;
45

@@ -7,7 +8,10 @@
78
namespace Android.Util
89
{
910
[Register ("android/util/SparseArray", DoNotGenerateAcw=true)]
10-
public partial class SparseArray<E> : SparseArray
11+
public partial class SparseArray<
12+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
13+
E
14+
> : SparseArray
1115
{
1216
public SparseArray ()
1317
{

src/Mono.Android/Android.Widget/AdapterView.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using Android.Views;
45
using JLO = Java.Lang.Object;
56

@@ -49,7 +50,10 @@ public event EventHandler ItemSelectionCleared {
4950
}
5051

5152
[Register ("android/widget/AdapterView", DoNotGenerateAcw=true)]
52-
public abstract class AdapterView<T> : AdapterView where T : IAdapter {
53+
public abstract class AdapterView<
54+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
55+
T
56+
> : AdapterView where T : IAdapter {
5357

5458
public AdapterView (IntPtr handle, JniHandleOwnership transfer)
5559
: base (handle, transfer)

src/Mono.Android/Android.Widget/ArrayAdapter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
namespace Android.Widget {
99

1010
[Register ("android/widget/ArrayAdapter", DoNotGenerateAcw=true)]
11-
public partial class ArrayAdapter<T> : ArrayAdapter {
11+
public partial class ArrayAdapter<
12+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
13+
T
14+
> : ArrayAdapter {
1215

1316
public ArrayAdapter (IntPtr handle, JniHandleOwnership transfer)
1417
: base (handle, transfer)

0 commit comments

Comments
 (0)