Skip to content

NET8 Android binding library generated sources are deleted when app resources are updated #8658

Closed

Description

@uholeschak

Android application type

.NET Android (net7.0-android, net8.0-android, etc.)

Affected platform version

VS2022 17.8.5

Description

The NET8 Android binding library generates it sources files at obj\$(Configuration)\$(TargetFramework)\generated\src.
When the resources of the corresponding app are updated by MSBuild:UpdateGeneratedFiles the msbuild target PrepareResources is called, which deletes the generated sources.
When debugging this causes problems, because the source has changed.
Building the application fails if resources files are open.

Steps to Reproduce

  1. Create NET8 Android binding library for an .AAR file
  2. Use the binding library in a application as project reference
  3. Build the application
  4. Check the binding library generated source at obj\$(Configuration)\$(TargetFramework)\generated\src
  5. Open the app Android resource and change it
  6. Watch obj\$(Configuration)\$(TargetFramework)\generated\src, the source will be deleted after some delay.
  7. While debugging (hitting a break point) the hot reload window is displayed, because the source files have been deleted.

Did you find any workaround?

Create a file Directory.Build.targets in the Android binding library project directory

<Project>
    <Target Name="PrepareResources" DependsOnTargets="$(PrepareResourcesDependsOn)" Condition="'$(DesignTimeBuild)'!='true'">
        <Message Text="PrepareResources modified" Importance="High" />
    </Target>
</Project>

Add to all projects in the solution:

    <DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>

This disables the resource update in the Android binding library

Relevant log output

No response

Activity

  1. dellis1972 commented on Jan 22, 2024

    @dellis1972
    Contributor

    @uholeschak

    Are you able to provide binlogs which demonstrates the issue?
    You can follow the instrucitons at https://github.com/dotnet/msbuild/blob/main/documentation/wiki/Providing-Binary-Logs.md#capturing-binary-logs-through-visual-studio. This will capture ALL data including the design time builds, it will allow us to see what is happening. Note it is important that we do get the design time build logs, as from what you describe the problem is with one of those.

  2. added
    need-infoIssues that need more information from the author.
    and removed
    needs-triageIssues that need to be assigned.
    on Jan 22, 2024
  3. uholeschak commented on Jan 22, 2024

    @uholeschak
    Author

    Ich have created some log files with Project System Tools 2022 in design mode when opening an Android resource:
    VSLogFiles.zip
    The interesting files are the 3 log files from the NetBinding projects.
    The generated sources are deleted in this case.

  4. added
    need-attentionA xamarin-android contributor needs to review
    and removed
    need-infoIssues that need more information from the author.
    on Jan 22, 2024
  5. dellis1972 commented on Jan 22, 2024

    @dellis1972
    Contributor

    @uholeschak

    Cam you share the csproj for one of those files? Usually I'd extract it from the binlog but I cannot open it on the mac as the https://live.msbuildlog.com/ site has not been updated it seems :(.

    Looks like the problem is the _ClearGeneratedManagedBindings target. Its currently using the following condition
    '@(InputJar->Count())' == '0' And '@(EmbeddedJar->Count())' == '0' And '@(LibraryProjectZip->Count())' == '0' And '@(_JavaBindingSource->Count())' == '0'.

    All of which are evaluating to 0. Something is missing from the conditon on this target it seems.

  6. added
    need-infoIssues that need more information from the author.
    and removed
    need-attentionA xamarin-android contributor needs to review
    on Jan 22, 2024
  7. uholeschak commented on Jan 22, 2024

    @uholeschak
    Author

    Here is one of the binding project files.
    UsbSerialNetBinding.zip
    It also contains a pre build step, that is irrelevant in this case. It only tries to force a regeneration of the source if the source is missing ...
    The input file is an .aar that is auto detected by the project.

  8. 2 remaining items

  9. Saratsin commented on Feb 5, 2024

    @Saratsin

    @uholeschak

    Cam you share the csproj for one of those files? Usually I'd extract it from the binlog but I cannot open it on the mac as the live.msbuildlog.com site has not been updated it seems :(.

    Looks like the problem is the _ClearGeneratedManagedBindings target. Its currently using the following condition '@(InputJar->Count())' == '0' And '@(EmbeddedJar->Count())' == '0' And '@(LibraryProjectZip->Count())' == '0' And '@(_JavaBindingSource->Count())' == '0'.

    All of which are evaluating to 0. Something is missing from the conditon on this target it seems.

    Shouldn't we check here for @(AndroidLibrary->Count()) == '0' as well?
    I mean it's the new way to define a native library dependency for binding projects since net6.0.
    I suppose they just forgot to add it to this target.

    P.S. As a workaround, try to use old nodes instead of AndroidLibrary in your csproj file (e.g., LibraryProjectZip or EmbeddedJar). At least it fixed an issue in my project

  10. jonathanpeppers commented on Feb 5, 2024

    @jonathanpeppers
    Member

    Shouldn't we check here for @(AndroidLibrary->Count()) == '0' as well?

    No, the new item group is simply translated into the "old" names:

    https://github.com/xamarin/xamarin-android/blob/06b1d7f82143fb32b1397b3657de5b22fdcfdf7c/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.AvailableItems.targets#L65-L87

    It was done this way for compatibility w/ Xamarin.Android -- and the fact we shared code between .NET 6 and Xamarin.Android.

  11. Saratsin commented on Feb 5, 2024

    @Saratsin

    Shouldn't we check here for @(AndroidLibrary->Count()) == '0' as well?

    No, the new item group is simply translated into the "old" names:

    https://github.com/xamarin/xamarin-android/blob/06b1d7f82143fb32b1397b3657de5b22fdcfdf7c/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.AvailableItems.targets#L65-L87

    It was done this way for compatibility w/ Xamarin.Android -- and the fact we shared code between .NET 6 and Xamarin.Android.

    To be honest it's not quite clear that _CategorizeAndroidLibraries target is called during the same MSBuild process when _ClearGeneratedManagedBindings target is called. I suppose that AndroidLibrary nodes might not be translated into older names due to this reason. I just cannot find the other reason of why changing node to LibraryProjectZip worked out

  12. dellis1972 commented on Feb 6, 2024

    @dellis1972
    Contributor

    @Saratsin

    do you have a diagnostic build log which shows the issue? (via aka.ms/binlog).
    It would help to see which ItemGroups your project is picking up.

  13. dellis1972 commented on Feb 7, 2024

    @dellis1972
    Contributor

    ok good news is I think I repo'd the issue.

    The new FastUpdate check is what is causing it. It runs some targets which do not play well with the .NET android build system. Working on a fix now. in the mean time adding
    <DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
    to the project will work around the issue.

  14. uholeschak commented on Feb 7, 2024

    @uholeschak
    Author

    If you read my first text you see that i have already set <DisableFastUpToDateCheck>true</DisableFastUpToDateCheck> in the project, which was not enough to fix the problem.

  15. removed
    need-attentionA xamarin-android contributor needs to review
    on Mar 20, 2024
  16. Domik234 commented on Mar 25, 2024

    @Domik234

    I'm not sure if it is the same problem as mentioned in description, but even when "obj\Debug\net8.0-android\generated\src" files are not removed, Intellisense and VS cannot see generated content.

    Built binaries contains all the code but when an application project uses dependency on this Android binding project (.NET 8 - ProjectReference or DLL) - it cannot see anything but Android resources.

    Shouldn't we check here for @(AndroidLibrary->Count()) == '0' as well?

    No, the new item group is simply translated into the "old" names:
    https://github.com/xamarin/xamarin-android/blob/06b1d7f82143fb32b1397b3657de5b22fdcfdf7c/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.AvailableItems.targets#L65-L87

    It was done this way for compatibility w/ Xamarin.Android -- and the fact we shared code between .NET 6 and Xamarin.Android.

    To be honest it's not quite clear that _CategorizeAndroidLibraries target is called during the same MSBuild process when _ClearGeneratedManagedBindings target is called. I suppose that AndroidLibrary nodes might not be translated into older names due to this reason. I just cannot find the other reason of why changing node to LibraryProjectZip worked out

    @Saratsin 's idea of using EmbeddedJar Include=".." fixes this. Thanks.

  17. added a commit that references this issue on Mar 25, 2024
  18. uholeschak commented on Mar 25, 2024

    @uholeschak
    Author

    Thanks, I have now also added to fix the problem:
    <LibraryProjectZip Include="..." />

  19. locked and limited conversation to collaborators on Apr 26, 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