Skip to content

Commit

Permalink
[trimming] remove our usage of [Preserve]
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jonathanpeppers committed Jul 22, 2024
1 parent baf16f1 commit 094909a
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<field name="handle_type" />
<field name="refs_added" />
<field name="weak_handle" />
<method name="SetHandleOnDeserialized" />
</type>
<type fullname="Java.Lang.Throwable">
<field name="handle" />
Expand Down
1 change: 0 additions & 1 deletion src/Mono.Android/Android.OS/AsyncTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public AsyncTask ()
JNIEnv.FinishCreateInstance (Handle, class_ref, id_ctor);
}

[Preserve (Conditional = true)]
protected override Java.Lang.Object? DoInBackground (params Java.Lang.Object[]? native_parms)
{
TParams[] parms = new TParams[native_parms?.Length ?? 0];
Expand Down
2 changes: 0 additions & 2 deletions src/Mono.Android/Android.Runtime/XmlPullParserReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public override void Close ()
source.Close ();
}

[Preserve (Conditional=true)]
public static XmlResourceParserReader? FromJniHandle (IntPtr handle, JniHandleOwnership transfer)
{
return FromNative (handle, transfer);
Expand Down Expand Up @@ -387,7 +386,6 @@ public override string? Value {
}
}

[Preserve (Conditional=true)]
public static XmlReader? FromJniHandle (IntPtr handle, JniHandleOwnership transfer)
{
return FromNative (handle, transfer);
Expand Down
2 changes: 0 additions & 2 deletions src/Mono.Android/Android.Runtime/XmlReaderPullParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Android.Runtime
{
public class XmlReaderResourceParser : XmlReaderPullParser, IXmlResourceParser
{
[Preserve (Conditional=true)]
public static IntPtr ToLocalJniHandle (XmlReader? value)
{
if (value == null)
Expand Down Expand Up @@ -127,7 +126,6 @@ public int StyleAttribute {

public class XmlReaderPullParser : Java.Lang.Object, IXmlPullParser
{
[Preserve (Conditional=true)]
public static IntPtr ToLocalJniHandle (XmlReader? value)
{
if (value == null)
Expand Down
1 change: 0 additions & 1 deletion src/Mono.Android/Java.Lang/Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public Object (IntPtr handle, JniHandleOwnership transfer)

// Note: must be internal so that e.g. DataContractJsonSerializer will find it
[OnDeserialized]
[Preserve]
internal void SetHandleOnDeserialized (StreamingContext context)
{
if (Handle != IntPtr.Zero)
Expand Down
5 changes: 0 additions & 5 deletions src/Mono.Android/metadata
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,6 @@
<remove-node path="/api/package[@name='junit.framework']/class[@name='TestSuite']/method[parameter[@type='java.lang.Class']]" api-since="16" />
-->

<!-- Preserve Mono.Android.App.IntentService::.ctor if the
type is used anywhere. -->

<attr path="/api/package[@name='mono.android.app']/class[@name='IntentService']/constructor[count(parameter) = 0]" name="customAttributes">[Preserve(Conditional = true)]</attr>

<!-- Asyncification -->

<!-- Interfaces -->
Expand Down
1 change: 0 additions & 1 deletion tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ public LinkerClass () { }
public void WasThisMethodPreserved (string arg1) { }
[Android.Runtime.Preserve]
public void PreserveAttribMethod () { }
}
}",
Expand Down

0 comments on commit 094909a

Please sign in to comment.