Skip to content

[net6] Enabling ILLink on the root assembly causes the build to fail on Application attribute #7301

Closed

Description

@jeromelaban

Android application type

Android for .NET (net6.0-android, etc.)

Affected platform version

17.4 Preview 1

Description

When building with the root assembly enabled for linking, the build fails with the following:

    Unhandled exception. System.InvalidOperationException: Sequence contains no matching element
       at System.Linq.ThrowHelper.ThrowNoMatchException()
       at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
       at MonoDroid.Tuner.PreserveApplications.PreserveTypeProperty(CustomAttribute attribute, String property) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveApplications.cs:line 99
       at MonoDroid.Tuner.PreserveApplications.ProcessAttributeProvider(ICustomAttributeProvider provider) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveApplications.cs:line 85
       at Mono.Linker.Steps.MarkStep.MarkType(TypeReference reference, DependencyInfo reason, Nullable`1 origin)
       at Mono.Linker.Steps.MarkStep.ProcessMarkedPending()
       at Mono.Linker.Steps.MarkStep.Initialize()
       at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
       at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step)
       at Mono.Linker.Pipeline.Process(LinkContext context)
       at Mono.Linker.Driver.Run(ILogger customLogger)
       at Mono.Linker.Driver.Main(String[] args)

caused by :
https://github.com/xamarin/xamarin-android/blob/10b972063f6253c232bad784d702c22dfb381a91/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveApplications.cs#L99

Which is expecting specific parameters values to be present, even if those are optional.

Steps to Reproduce

	<Target Name="_AdjustILLinkParameters" AfterTargets="PrepareForILLink">
		<ItemGroup>
			<TrimmerRootAssembly Update="@(TrimmerRootAssembly)" RootMode="library" />
		</ItemGroup>
	</Target>

Did you find any workaround?

Add empty parameters to the Application attribute:

    [global::Android.App.ApplicationAttribute(
        ...
        BackupAgent = null,
        ManageSpaceActivity = null
    )]

Update: It's actually not a workaround since it fails later on:

1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001: System.NullReferenceException: Object reference not set to an instance of an object.
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at Xamarin.Android.Manifest.ManifestDocumentElement.ResolveType(String type, ICustomAttributeProvider provider, IAssemblyResolver resolver)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at Android.App.ApplicationAttribute.<>c.<.cctor>b__110_7(ApplicationAttribute self, ICustomAttributeProvider p, IAssemblyResolver r)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at Xamarin.Android.Manifest.ManifestDocumentElement`1.ToAttribute(String name, T value, String packageName, ICustomAttributeProvider provider, IAssemblyResolver resolver, Int32 targetSdkVersion)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at Xamarin.Android.Manifest.ManifestDocumentElement`1.<>c__DisplayClass8_0.<ToElement>b__1(String e)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at Xamarin.Android.Manifest.ManifestDocumentElement`1.ToElement(T value, ICollection`1 specified, String packageName, ICustomAttributeProvider provider, IAssemblyResolver resolver, Int32 targetSdkVersion)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at Xamarin.Android.Tasks.ManifestDocument.CreateApplicationElement(XElement manifest, String applicationClass, List`1 subclasses, TypeDefinitionCache cache)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at Xamarin.Android.Tasks.ManifestDocument.Merge(TaskLoggingHelper log, TypeDefinitionCache cache, List`1 subclasses, String applicationClass, Boolean embed, String bundledWearApplicationName, IEnumerable`1 mergedManifestDocuments)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at Xamarin.Android.Tasks.GenerateJavaStubs.Run(DirectoryAssemblyResolver res)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at Xamarin.Android.Tasks.GenerateJavaStubs.RunTask()
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7001:    at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 17

Relevant log output

No response

Activity

  1. jonathanpeppers commented on Aug 23, 2022

    @jonathanpeppers
    Member

    Trimming the main assembly (root assembly) would probably break in lots of ways. Is there an app size reduction you're going after here? Do you have some numbers you can share?

  2. jeromelaban commented on Aug 24, 2022

    @jeromelaban
    Author

    Trimming used to be supported with Xamarin (afaik) and with all the code generators out there, lots of code is generated without necessarily being used (record is a good example).

    That being said, the feature I'm mostly interested in activating for the root assembly is ILLink "substitutions" and the root assembly mode is "copy" by default, preventing features to be conditionally enabled/disabled as a result.

    In the case of Uno, activating this allows us to cut in half the size of the app on WebAssembly (because we're generating lots of code). By the nature of string-based switches, the ILLinker cannot work properly so we use a combination of substitution and linking to achieve that.

  3. dellis1972 commented on Aug 24, 2022

    @dellis1972
    Contributor

    With Xamarin.Android, we never linked the main assembly since it was the entry point which controlled what was used everywhere else. We would also loose things like BroadcastRecievers etc since they would never be used in code and only from the Manifest.

    At least thats how it used to work.

  4. tranb3r commented on Apr 30, 2024

    @tranb3r

    I'm facing the same issue when testing net9-pre3, while everything works with net8.
    Is there any workaround or any fix for this issue in net9?

  5. jonathanpeppers commented on Apr 30, 2024

    @jonathanpeppers
    Member

    The issue here is filed along with .NET 6. It may no longer be relevant.

    The issue you have might be quite different, if you are opting into TrimMode=full such as:

    Can you file a new issue with a .binlog? https://aka.ms/binlog (or if it is a runtime crash adb logcat)

  6. tranb3r commented on Apr 30, 2024

    @tranb3r

    Yes, I'm using TrimMode=full.
    The issue is really similar, but I agree it's better to file a new issue.

  7. locked and limited conversation to collaborators on Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Area: App+Library BuildIssues when building Library projects or Application projects.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions