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

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

Closed
jeromelaban opened this issue Aug 23, 2022 · 6 comments · Fixed by #8936
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects.

Comments

@jeromelaban
Copy link

jeromelaban commented Aug 23, 2022

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

@jeromelaban jeromelaban added Area: App+Library Build Issues when building Library projects or Application projects. needs-triage Issues that need to be assigned. labels Aug 23, 2022
@jonathanpeppers jonathanpeppers removed the needs-triage Issues that need to be assigned. label Aug 23, 2022
@jonathanpeppers
Copy link
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?

@jeromelaban
Copy link
Author

jeromelaban commented Aug 24, 2022

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.

@dellis1972
Copy link
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.

@tranb3r
Copy link

tranb3r commented Apr 30, 2024

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?

@jonathanpeppers
Copy link
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)

@tranb3r
Copy link

tranb3r commented Apr 30, 2024

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: App+Library Build Issues when building Library projects or Application projects.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants