Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[One .NET] fix more Mono.Android.dll linker warnings #6270

Merged

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Sep 7, 2021

Context: #5652

There are two remaining linker warnings when building with:

> dotnet build -c Release -p:SuppressTrimAnalysisWarnings=false
...
src\Mono.Android\Android.Runtime\ResourceIdManager.cs(37,6): warning IL2026: Android.Runtime.ResourceIdManager.GetResourceTypeFromAssembly(Assembly): Using method 'System.Reflection.Assembly.GetType(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.
src\Mono.Android\Java.Interop\JavaObjectExtensions.cs(136,4): warning IL2026: Java.Interop.JavaObjectExtensions.GetHelperType(Type,String): Using method 'System.Reflection.Assembly.GetType(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.
src\Mono.Android\Java.Interop\JavaObjectExtensions.cs(131,5): warning IL2026: Java.Interop.JavaObjectExtensions.GetHelperType(Type,String): Using method 'System.Reflection.Assembly.GetType(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.

The two issues here:

  1. ResourceIdManager uses reflection for the implementation of
    Resource.designer.cs. Specify UnconditionalSuppressMessage to
    ignore the warning, because the assembly hitting the warning is the
    "root assembly". The methods in question will not be removed by the
    linker.

  2. JavaObjectExtensions.GetHelperType dynamically looks up Invoker
    types. Specify UnconditionalSuppressMessage to ignore the
    warning, because the MarkJavaObjects linker step preserves them.
    Additionally, I renamed the method to GetInvokerType() and
    hardcoded "Invoker" within the method. This should prevent us
    from messing this up if a new "helper" type is added in addition to
    Invoker.

Unfortunately, even after all the issues are solved. ILLink still
appears to emit warnings:

Java.Interop.dll warning IL2104: Assembly 'Java.Interop' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
Mono.Android.dll warning IL2104: Assembly 'Mono.Android' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries

Adding -p:TrimmerSingleWarn=false shows an additional 30 warnings.

There is potentially an issue with how these warnings are presented,
filed at:

dotnet/linker#2265

@jonathanpeppers
Copy link
Member Author

As discovered in dotnet/linker#2265, adding -p:TrimmerSingleWarn=false shows a whole new list of warnings to fix here...

external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniMarshalMemberBuilder.cs(57,4): warning IL2072: Java.Interop.JniRuntime.SetMarshalMemberBuilder(JniRuntime.CreationOptions): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Object System.Activator::CreateInstance(System.Type)'. The return value of method 'System.Reflection.Assembly.GetType(String)' 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.
external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(320,32): warning IL2067: Java.Interop.JniRuntime.JniTypeManager.FindAndCallRegisterMethod(Type,JniNativeMethodRegistrationArguments): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeMethods(Type)'. The parameter 'marshalType' of method 'Java.Interop.JniRuntime.JniTypeManager.FindAndCallRegisterMethod(Type,JniNativeMethodRegistrationArguments)' 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.
external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(270,5): warning IL2070: Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType,Type,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicNestedTypes' in call to 'System.Type.GetNestedType(String,BindingFlags)'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType,Type,String)' 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.
external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniTypeManager.cs(274,5): warning IL2072: Java.Interop.JniRuntime.JniTypeManager.TryLoadJniMarshalMethods(JniType,Type,String): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Reflection.MethodInfo System.Reflection.RuntimeReflectionExtensions::GetRuntimeMethod(System.Type,System.String,System.Type[])'. The return value of method 'System.Type.GetNestedType(String,BindingFlags)' 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.
external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(338,5): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetActivationConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetActivationConstructor(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.
external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(577,5): warning IL2060: Java.Interop.JniRuntime.JniValueManager.GetObjectArrayMarshaler(Type): Call to 'System.Reflection.MethodInfo System.Reflection.MethodInfo::MakeGenericMethod(System.Type[])' can not be statically analyzed. It's not possible to guarantee the availability of requirements of the generic method.
external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(523,6): warning IL2072: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Object System.Activator::CreateInstance(System.Type)'. The return value of method 'Java.Interop.JniValueMarshalerAttribute.MarshalerType.get' 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.
external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(537,5): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(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.
external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(559,27): warning IL2070: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(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.
external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(569,6): warning IL2072: Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type): '#0' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Object System.Activator::CreateInstance(System.Type)'. The return value of method 'Java.Interop.JniValueMarshalerAttribute.MarshalerType.get' 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.
external\Java.Interop\src\Java.Interop\Java.Interop\ManagedPeer.cs(93,5): warning IL2057: Java.Interop.ManagedPeer.Construct(IntPtr,IntPtr,IntPtr,IntPtr,IntPtr,IntPtr): 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.
external\Java.Interop\src\Java.Interop\Java.Interop\ManagedPeer.cs(102,5): warning IL2075: Java.Interop.ManagedPeer.Construct(IntPtr,IntPtr,IntPtr,IntPtr,IntPtr,IntPtr): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Type.GetConstructor(Type[])'. The return value of method 'System.Type.GetType(String,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.
external\Java.Interop\src\Java.Interop\Java.Interop\ManagedPeer.cs(156,5): warning IL2057: Java.Interop.ManagedPeer.GetParameterTypes(String): 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.
external\Java.Interop\src\Java.Interop\Java.Interop\ManagedPeer.cs(200,5): warning IL2057: Java.Interop.ManagedPeer.RegisterNativeMembers(IntPtr,IntPtr,IntPtr,IntPtr,IntPtr): 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\AndroidRuntime.cs(300,5): warning IL2075: Android.Runtime.AndroidTypeManager.CreateDynamicCallback(MethodInfo): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'System.Reflection.Assembly.GetType(String)' 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\AndroidRuntime.cs(407,25): warning IL2070: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType,Type,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'type' of method 'Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType,Type,String)' 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\AndroidRuntime.cs(419,7): warning IL2057: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType,Type,String): 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\AndroidRuntime.cs(424,6): warning IL2067: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType,Type,String): 'target' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.Delegate.CreateDelegate(Type,Type,String)'. The parameter 'type' of method 'Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType,Type,String)' 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\AndroidRuntime.cs(424,6): warning IL2072: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType,Type,String): 'target' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.Delegate.CreateDelegate(Type,Type,String)'. The return value of method 'System.Type System.Type::get_BaseType()' 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\AndroidRuntime.cs(424,6): warning IL2072: Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType,Type,String): 'target' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.Delegate.CreateDelegate(Type,Type,String)'. The return value of method 'System.Type.GetType(String,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\Android.Runtime\ConstructorBuilder.cs(10,3): warning IL2111: Android.Runtime.ConstructorBuilder..cctor(): Method 'System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(Type)' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements of the method.
external\Java.Interop\src\Java.Interop.Tools.TypeNameMappings\Java.Interop.Tools.TypeNameMappings\JavaNativeTypeManager.cs(174,4): warning IL2070: Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToJniName(Type,ExportParameterKind): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter 'type' of method 'Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.ToJniName(Type,ExportParameterKind)' 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\JavaObjectExtensions.cs(142,4): warning IL2055: Java.Interop.JavaObjectExtensions.GetInvokerType(Type): Call to 'System.Type.MakeGenericType(Type[])' can not be statically analyzed. It's not possible to guarantee the availability of requirements of the generic type.
src\Mono.Android\Android.Runtime\JNIEnv.cs(125,4): warning IL2057: Android.Runtime.JNIEnv.RegisterJniNatives(IntPtr,Int32,IntPtr,IntPtr,Int32): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(String)'. It's not possible to guarantee the availability of the target type.
src\Mono.Android\Android.Runtime\ResourceIdManager.cs(26,5): warning IL2075: Android.Runtime.ResourceIdManager.UpdateIdValues(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'Android.Runtime.ResourceIdManager.GetResourceTypeFromAssembly(Assembly)' 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\TypeManager.cs(329,4): warning IL2070: Java.Interop.TypeManager.CreateProxy(Type,IntPtr,JniHandleOwnership): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructor(BindingFlags,Binder,Type[],ParameterModifier[])'. The parameter 'type' of method 'Java.Interop.TypeManager.CreateProxy(Type,IntPtr,JniHandleOwnership)' 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\TypeManager.cs(333,4): warning IL2070: Java.Interop.TypeManager.CreateProxy(Type,IntPtr,JniHandleOwnership): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructor(BindingFlags,Binder,Type[],ParameterModifier[])'. The parameter 'type' of method 'Java.Interop.TypeManager.CreateProxy(Type,IntPtr,JniHandleOwnership)' 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\TypeManager.cs(130,5): warning IL2057: Java.Interop.TypeManager.GetParameterTypes(String): 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\Java.Interop\TypeManager.cs(153,4): warning IL2057: Java.Interop.TypeManager.n_Activate(IntPtr,IntPtr,IntPtr,IntPtr,IntPtr,IntPtr): 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\Java.Interop\TypeManager.cs(161,4): warning IL2075: Java.Interop.TypeManager.n_Activate(IntPtr,IntPtr,IntPtr,IntPtr,IntPtr,IntPtr): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Type.GetConstructor(Type[])'. The return value of method 'System.Type.GetType(String,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.

Context: dotnet#5652

There are two remaining linker warnings when building with:

    > dotnet build -c Release -p:SuppressTrimAnalysisWarnings=false
    ...
    src\Mono.Android\Android.Runtime\ResourceIdManager.cs(37,6): warning IL2026: Android.Runtime.ResourceIdManager.GetResourceTypeFromAssembly(Assembly): Using method 'System.Reflection.Assembly.GetType(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.
    src\Mono.Android\Java.Interop\JavaObjectExtensions.cs(136,4): warning IL2026: Java.Interop.JavaObjectExtensions.GetHelperType(Type,String): Using method 'System.Reflection.Assembly.GetType(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.
    src\Mono.Android\Java.Interop\JavaObjectExtensions.cs(131,5): warning IL2026: Java.Interop.JavaObjectExtensions.GetHelperType(Type,String): Using method 'System.Reflection.Assembly.GetType(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.

The two issues here:

1. `ResourceIdManager` uses reflection for the implementation of
   `Resource.designer.cs`. Specify `UnconditionalSuppressMessage` to
   ignore the warning, because the assembly hitting the warning is the
   "root assembly". The methods in question will not be removed by the
   linker.

2. `JavaObjectExtensions.GetHelperType` dynamically looks up `Invoker`
   types. Specify `UnconditionalSuppressMessage` to ignore the
   warning, because the `MarkJavaObjects` linker step preserves them.
   Additionally, I renamed the method to `GetInvokerType()` and
   hardcoded `"Invoker"` within the method. This should prevent us
   from messing this up if a new "helper" type is added in addition to
   `Invoker`.

Unfortunately, even after all the issues are solved. `ILLink` still
appears to emit warnings:

    Java.Interop.dll warning IL2104: Assembly 'Java.Interop' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
    Mono.Android.dll warning IL2104: Assembly 'Mono.Android' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries

Adding `-p:TrimmerSingleWarn=false` shows an additional 30 warnings.

There is potentially an issue with how these warnings are presented,
filed at:

dotnet/linker#2265
@jonathanpeppers jonathanpeppers changed the title [One .NET] fix remaining Mono.Android.dll linker warnings [One .NET] fix more Mono.Android.dll linker warnings Sep 8, 2021
@jonathanpeppers jonathanpeppers marked this pull request as ready for review September 8, 2021 21:56
Comment on lines -127 to +128
internal static Type? GetHelperType (Type type, string suffix)
// typeof(Foo) -> FooInvoker
// typeof(Foo<>) -> FooInvoker`1
[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "*Invoker types are preserved by the MarkJavaObjects linker step.")]
internal static Type? GetInvokerType (Type type)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonpryor this was the part, I wanted to check it's OK to rename this method.

I thought allowing any string suffix through here could introduce a future linker issue, because MarkJavaObjects only preserves *Invoker and *Adapter right now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just raises a related question: what's a *Adapter type, and why is it being preserved? I forget. :-(

I think for "consistency" we should consider updating src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MarkJavaObjects.cs to likewise rename GetHelperType() to, or otherwise introduce, GetInvokerType(). At least then our internal terminology would be consistent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be the commit which introduce the Adapter suffix; it was apparently for interfaces: https://github.com/xamarin/monodroid/commit/ff043c0281fbe1fefb3e83b7ccda1eca791c3fb0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect what happened is we "unified" the Adapter and Invoker suffixes into Invoker, but I can't easily find such a commit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like Adapter was removed in generator output, with Invoker as the replacement, as part of https://github.com/xamarin/monodroid/commit/efeff037dfc4d4c60ad87fd6d6b22d9af45d7583

So it looks like there's no need to preserve Adapter-suffixed types, not since 2011.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the MarkJavaObjects step. I wonder if we'll see any .apk size differences?

@jonathanpeppers
Copy link
Member Author

We can probably ignore the test failure here:

InstallApkSet
    ...
    Errors
        [BT Error retrieving device SDK version. Please try again. 
        Xamarin.Android.Common.targets(2543,3): java.lang.IllegalStateException :  Error retrieving device SDK version. Please try again.
    java.lang.IllegalStateException: Error retrieving device SDK version. Please try again.
    	at com.google.common.base.Preconditions.checkState(Preconditions.java:459)
    	at com.android.tools.build.bundletool.device.DeviceAnalyzer.getDeviceSpec(DeviceAnalyzer.java:57)
    	at com.android.tools.build.bundletool.commands.InstallApksCommand.execute(InstallApksCommand.java:154)
    	at com.android.tools.build.bundletool.BundleToolMain.main(BundleToolMain.java:89)
    	at com.android.tools.build.bundletool.BundleToolMain.main(BundleToolMain.java:47)

Can keep an eye on this, if it keeps happening.

@jonpryor
Copy link
Member

jonpryor commented Sep 15, 2021

Context: https://github.com/dotnet/linker/issues/2265
Context: https://github.com/xamarin/xamarin-android/issues/5652

There are two remaining linker warnings when building with
`$(SuppressTrimAnalysisWarnings)`=false:

	> dotnet build -c Release -p:SuppressTrimAnalysisWarnings=false
	
	src\Mono.Android\Android.Runtime\ResourceIdManager.cs(37,6): warning IL2026: Android.Runtime.ResourceIdManager.GetResourceTypeFromAssembly(Assembly): Using method 'System.Reflection.Assembly.GetType(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.
	src\Mono.Android\Java.Interop\JavaObjectExtensions.cs(136,4): warning IL2026: Java.Interop.JavaObjectExtensions.GetHelperType(Type,String): Using method 'System.Reflection.Assembly.GetType(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.
	src\Mono.Android\Java.Interop\JavaObjectExtensions.cs(131,5): warning IL2026: Java.Interop.JavaObjectExtensions.GetHelperType(Type,String): Using method 'System.Reflection.Assembly.GetType(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.

The two issues here:

 1. `ResourceIdManager` uses reflection for the implementation of
    `Resource.designer.cs`.  Use `[UnconditionalSuppressMessage]` to
    ignore the warning, because the assembly hitting the warning is
    the "root assembly", which is *not linked*.  The methods in
    question will not be removed by the linker.

 2. `JavaObjectExtensions.GetHelperType()` dynamically looks up
    `*Invoker` types.  Use `[UnconditionalSuppressMessage]` to ignore
    the warning, because the `MarkJavaObjects()` linker step preserves
    them.  Additionally, I renamed the method to `GetInvokerType()`
    and hardcoded `"Invoker"` within the method.  This should prevent
    us from messing this up if a new "helper" type is added in
    addition to `Invoker`.

Unfortunately, even after all the issues are solved. `ILLink` still
appears to emit warnings:

	Java.Interop.dll warning IL2104: Assembly 'Java.Interop' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
	Mono.Android.dll warning IL2104: Assembly 'Mono.Android' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries

Adding `-p:TrimmerSingleWarn=false` shows an additional 30 warnings.

@jonathanpeppers jonathanpeppers merged commit 141da85 into dotnet:main Sep 17, 2021
@jonathanpeppers jonathanpeppers deleted the mono.android-illink-warnings branch September 17, 2021 13:41
@github-actions github-actions bot locked and limited conversation to collaborators Jan 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants