Skip to content

Commit

Permalink
[One .NET] remove in-tree DynamicDependencyAttribute
Browse files Browse the repository at this point in the history
Context: dotnet#5652

In earlier development of .NET 6, we had a copy of
`DynamicDependencyAttribute` because we were compiling
`Mono.Android.dll` for `netcoreapp3.1`. Now we are compiling for
`net6.0`, so we can remove these files. We also can condition their
usage behind `#if NET`, to mean ".NET 5 or higher", where the
attributes were introduced.

I also fixed the linker warning:

    src\Mono.Android\Android.Runtime\AndroidRuntime.cs(296,5): warning IL2026: Android.Runtime.AndroidTypeManager.CreateDynamicCallback(MethodInfo): Using method 'System.Reflection.Assembly.GetType(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.

By introducing:

    [DynamicDependency ("Create", "Java.Interop.DynamicCallbackCodeGenerator", "Mono.Android.Export")]

However, we still need to suppress the warning:

    [UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "DynamicDependency should preserve the Create() method.")]
  • Loading branch information
jonathanpeppers committed Aug 20, 2021
1 parent 1aac4a0 commit 941f4a6
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 225 deletions.
2 changes: 2 additions & 0 deletions src/Mono.Android.Export/CallbackCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ public CodeExpression FromNative (CodeExpression arg)
throw new InvalidOperationException ();
}

#if NET
// Ignore ToNative() overload that takes generic instancing mapping. The reflected method should have nothing to do with it.
[DynamicDependency ("ToLocalJniHandle", "Android.Runtime.JavaArray`1", "Mono.Android")]
[DynamicDependency ("ToLocalJniHandle", "Android.Runtime.JavaCollection", "Mono.Android")]
Expand All @@ -416,6 +417,7 @@ public CodeExpression FromNative (CodeExpression arg)
[DynamicDependency ("ToLocalJniHandle", "Android.Runtime.JavaList`1", "Mono.Android")]
[DynamicDependency ("ToLocalJniHandle", "Android.Runtime.JavaSet", "Mono.Android")]
[DynamicDependency ("ToLocalJniHandle", "Android.Runtime.JavaSet`1", "Mono.Android")]
#endif
public CodeExpression ToNative (CodeExpression arg)
{
switch (GetKind (type)) {
Expand Down
5 changes: 5 additions & 0 deletions src/Mono.Android/Android.Runtime/AndroidRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -285,6 +286,10 @@ protected override IEnumerable<string> GetSimpleReferences (Type type)

static MethodInfo? dynamic_callback_gen;

#if NET
[DynamicDependency ("Create", "Java.Interop.DynamicCallbackCodeGenerator", "Mono.Android.Export")]
[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "DynamicDependency should preserve the Create() method.")]
#endif
static Delegate CreateDynamicCallback (MethodInfo method)
{
if (dynamic_callback_gen == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ namespace Java.Interop {
#endif // !JCW_ONLY_TYPE_NAMES
partial class ExportAttribute : Attribute {

#if NET
[DynamicDependency (DynamicallyAccessedMemberTypes.All, "Java.Interop.DynamicCallbackCodeGenerator", "Mono.Android.Export")]
#endif
public ExportAttribute ()
{
}


#if NET
[DynamicDependency (DynamicallyAccessedMemberTypes.All, "Java.Interop.DynamicCallbackCodeGenerator", "Mono.Android.Export")]
#endif
public ExportAttribute (string name)
{
Name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace Java.Interop {
#endif // !JCW_ONLY_TYPE_NAMES
partial class ExportFieldAttribute : Attribute {

#if NET
[DynamicDependency (DynamicallyAccessedMemberTypes.All, "Java.Interop.DynamicCallbackCodeGenerator", "Mono.Android.Export")]
#endif
public ExportFieldAttribute (string name)
{
Name = name;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,4 @@
<Compile Include="$(MSBuildThisFileDirectory)Android.App\LayoutAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\IJniNameProviderAttribute.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'net6.0' ">
<Compile Include="$(MSBuildThisFileDirectory)System.Diagnostics.CodeAnalysis/DynamicallyAccessedMemberTypes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System.Diagnostics.CodeAnalysis/DynamicDependencyAttribute.cs" />
</ItemGroup>
</Project>

0 comments on commit 941f4a6

Please sign in to comment.