Skip to content

Commit 688245d

Browse files
[trimming] remove our usage of [Preserve] (#9126)
Fixes: #5197 `[Preserve]` still works in some form, as the `ApplyPreserveAttribute` trimmer step will "mark" types it encounters. It unfortuantely, *does not* work if an assembly is deemed to be fully removed, as the `ApplyPreserveAttribute` will not be called for these types. To better align with the future of .NET, we should remove *our* usage of `[Preserve]`. The only failing result is `Java.Lang.Object.SetHandleOnDeserialized` which can be preserved in `Mono.Android.xml` instead. In a future PR, we can consider emitting warnings in customer code with `[Preserve]` usage.
1 parent 5feae81 commit 688245d

File tree

6 files changed

+1
-11
lines changed

6 files changed

+1
-11
lines changed

src/Microsoft.Android.Sdk.ILLink/PreserveLists/Mono.Android.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<field name="handle_type" />
4141
<field name="refs_added" />
4242
<field name="weak_handle" />
43+
<method name="SetHandleOnDeserialized" />
4344
</type>
4445
<type fullname="Java.Lang.Throwable">
4546
<field name="handle" />

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public AsyncTask ()
6464
JNIEnv.FinishCreateInstance (Handle, class_ref, id_ctor);
6565
}
6666

67-
[Preserve (Conditional = true)]
6867
protected override Java.Lang.Object? DoInBackground (params Java.Lang.Object[]? native_parms)
6968
{
7069
TParams[] parms = new TParams[native_parms?.Length ?? 0];

src/Mono.Android/Android.Runtime/XmlPullParserReader.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public override void Close ()
2525
source.Close ();
2626
}
2727

28-
[Preserve (Conditional=true)]
2928
public static XmlResourceParserReader? FromJniHandle (IntPtr handle, JniHandleOwnership transfer)
3029
{
3130
return FromNative (handle, transfer);
@@ -387,7 +386,6 @@ public override string? Value {
387386
}
388387
}
389388

390-
[Preserve (Conditional=true)]
391389
public static XmlReader? FromJniHandle (IntPtr handle, JniHandleOwnership transfer)
392390
{
393391
return FromNative (handle, transfer);

src/Mono.Android/Android.Runtime/XmlReaderPullParser.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace Android.Runtime
99
{
1010
public class XmlReaderResourceParser : XmlReaderPullParser, IXmlResourceParser
1111
{
12-
[Preserve (Conditional=true)]
1312
public static IntPtr ToLocalJniHandle (XmlReader? value)
1413
{
1514
if (value == null)
@@ -127,7 +126,6 @@ public int StyleAttribute {
127126

128127
public class XmlReaderPullParser : Java.Lang.Object, IXmlPullParser
129128
{
130-
[Preserve (Conditional=true)]
131129
public static IntPtr ToLocalJniHandle (XmlReader? value)
132130
{
133131
if (value == null)

src/Mono.Android/Java.Lang/Object.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public Object (IntPtr handle, JniHandleOwnership transfer)
8282

8383
// Note: must be internal so that e.g. DataContractJsonSerializer will find it
8484
[OnDeserialized]
85-
[Preserve]
8685
internal void SetHandleOnDeserialized (StreamingContext context)
8786
{
8887
if (Handle != IntPtr.Zero)

src/Mono.Android/metadata

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -852,11 +852,6 @@
852852
<remove-node path="/api/package[@name='junit.framework']/class[@name='TestSuite']/method[parameter[@type='java.lang.Class']]" api-since="16" />
853853
-->
854854

855-
<!-- Preserve Mono.Android.App.IntentService::.ctor if the
856-
type is used anywhere. -->
857-
858-
<attr path="/api/package[@name='mono.android.app']/class[@name='IntentService']/constructor[count(parameter) = 0]" name="customAttributes">[Preserve(Conditional = true)]</attr>
859-
860855
<!-- Asyncification -->
861856

862857
<!-- Interfaces -->

0 commit comments

Comments
 (0)