Skip to content

Commit

Permalink
[One .NET] Don't preserve all of Android.Runtime.JavaObject (#5367)
Browse files Browse the repository at this point in the history
Context: #5167

The `Android.Runtime.JavaObject` type is not accessed by reflection,
and does not need to be fully preserved by the linker.

After allowing `JavaObject` to be linked, we discovered that the
presence of the `JavaObject.Clone()` method pulled in a lot of API,
through the `System.ICloneable` interface.

Removing the `JavaObject.Clone()` method allows us to save more than
900KB in compressed assembly size:

	Summary:
	  -     942,460 Assemblies -43.72% (of 2,155,792)
	  -     947,763 Package size difference -10.50% (of 9,024,291)

Which raises an important question: do we *need* `JavaObject.Clone()`?

We believe the answer is *no*: `JavaObject` is a sealed type; no
*managed* code can call `JavaObject.Clone()`.

This leaves the Java Callable Wrapper, which [*oddly*][0] is `public`,
*and* overrides `java.lang.Object.clone()` as a public method.

Thus, *in theory*, `JavaObject.Clone()` *is* invocable from Java code.

However, *in practice*, to do so the Java code would need to take
a compile-time dependency on `mono.android.jar`, which feels *highly*
unlikely and unusual.  (`java.lang.Object.clone()` is `protected` by
default, and is thus not invocable normally.)

We thus deem it safe to entirely remove `JavaObject.Clone()`.

In the end we are now smaller in assembly size comparing simple XA app
"legacy" and net6:

	Summary:
	  -     356,408 Assemblies -27.69% (of 1,287,165)
	  +   2,646,694 Package size difference 51.43% (of 5,146,413)

And still a bit larger comparing XA XForms app:

	Summary:
	  +   1,001,148 Assemblies 21.01% (of 4,765,097)
	  +   4,013,739 Package size difference 37.75% (of 10,633,264)

[0]: dotnet/java-interop#754
  • Loading branch information
radekdoulik authored Dec 11, 2020
1 parent f495778 commit d0df243
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<type fullname="Android.Runtime.JavaDictionary`2" />
<type fullname="Android.Runtime.JavaList" />
<type fullname="Android.Runtime.JavaList`1" />
<type fullname="Android.Runtime.JavaObject" />
<type fullname="Android.Runtime.JavaSet" />
<type fullname="Android.Runtime.JavaSet`1" />
<type fullname="Android.Runtime.JavaTypeConverter`1" />
Expand Down
8 changes: 0 additions & 8 deletions src/Mono.Android/Android.Runtime/JavaObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ public object Instance {
get { return inst; }
}

protected override Java.Lang.Object Clone ()
{
if (inst is ICloneable c)
return new JavaObject (c.Clone ());
else
return new JavaObject (inst);
}

public override bool Equals (Java.Lang.Object? obj)
{
if (obj is JavaObject jobj) {
Expand Down

0 comments on commit d0df243

Please sign in to comment.