dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.93k stars 532 forks source link

[.NET8] SkipCompilerExecution=true causes the android sample project of AvaloniaUI main library to fail to run #9499

Closed Jamlee closed 1 week ago

Jamlee commented 1 week ago

The Android Example of the famous project AvaloniaUI cannot be run through dotnet run. After my efforts, I found that it was caused by SkipCompilerExecution=true in dotnet8.0.204/packs/Microsoft.Android.Sdk.Darwin/34.0.143/targets/Microsoft.Android.Sdk.AssemblyResolution.targets.

<Target Name="_ResolveAssemblies">...
    <PropertyGroup>
      <_AdditionalProperties>
        _ComputeFilesToPublishForRuntimeIdentifiers=true
        ;SelfContained=true
        ;AppendRuntimeIdentifierToOutputPath=true
        ;ResolveAssemblyReferencesFindRelatedSatellites=false
        ;SkipCompilerExecution=true   <!-- Hi There -->
        ;_OuterIntermediateAssembly=@(IntermediateAssembly)
        ;_OuterOutputPath=$(OutputPath)
        ;_OuterIntermediateOutputPath=$(IntermediateOutputPath)
      </_AdditionalProperties>
      <_AndroidBuildRuntimeIdentifiersInParallel Condition=" '$(_AndroidBuildRuntimeIdentifiersInParallel)' == '' ">true</_AndroidBuildRuntimeIdentifiersInParallel>
    </PropertyGroup>

cd samples/ControlCatalog.Android; dotnet run The following error will be reported:

/Users/jamlee/Installed/dotnet8.0.204/sdk/8.0.204/Microsoft.Common.CurrentVersion.targets(4793,5): 
error MSB3030: Could not copy the file "obj/Debug/net8.0/Avalonia.Dialogs.dll" because it was not found. 
[/Users/jamlee/Tmp/Avalonia/src/Avalonia.Dialogs/Avalonia.Dialogs.csproj::TargetFramework=net8.0]

Although this Android Example can be run on VisualStudio 2022, it cannot be run on dotnet run. After further exploration I was able to reproduce this problem simply:

git clone https://github.com/AvaloniaUI/Avalonia.git
cd src/Avalonia/src/Avalonia.Dialogs

# Compile successfully
dotnet build

# This compilation will fail
dotnet build /p:android-arm64 /p:_ComputeFilesToPublishForRuntimeIdentifiers=true /p:SelfContained=true /p:AppendRuntimeIdentifierToOutputPath=true /p:ResolveAssemblyReferencesFindRelatedSatellites=false /p:SkipCompilerExecution=true /p:_OuterIntermediateAssembly=obj/Debug/net8.0-android34.0/android-arm64/ControlCatalog.Android.dll /p:_OuterOutputPath=bin/Debug/net8.0-android34.0/android-arm64/ /p:_OuterIntermediateOutputPath=obj/Debug/net8.0-android34.0/android-arm64/

Failure error message

.....
Build FAILED.
/Users/jamlee/Installed/dotnet8.0.204/sdk/8.0.204/Microsoft.Common.CurrentVersion.targets(4793,5): error MSB3030: Could not copy the file "obj/Debug/net6.0/Avalonia.Dialogs.dll" because it was not found. [/Users/jamlee/Tmp/Avalonia/src/Avalonia.Dialogs/Avalonia.Dialogs.csproj::TargetFramework=net6.0]
0 Warning(s)
1 Error(s)

To reproduce this problem directly, you can execute it, refer to AvaloniaUI Compilation Document

git clone https://github.com/AvaloniaUI/Avalonia.git

dotnet workload install android ios wasm-tools
curl -sSL https://raw.githubusercontent.com/Samsung/Tizen.NET/main/workload/scripts/workload-install.sh | sudo bash
dotnet tool install --global Nuke.GlobalTool --version 6.2.1

nuke

cd samples/ControlCatalog.Android
dotnet run

To sum up, there are 2 questions?

  1. Why does skipping compilation delete the existing obj/Debug/net6.0/Avalonia.Dialogs.dll?
  2. Why did I not encounter an error like dotnet run when I right-clicked and ran the project in VisualStudio 2022? Is it because VisualStudio skipped compiling the compiled project?

PS: The first language I learned twelve years ago was C#. Because it was not open source, there seemed to be too much witchcraft in it to confuse people. Now that it is open source, I want to figure out the problem. It took me five nights to find that SkipCompilerExecution=true was the root cause of the problem. If it is convenient, cloud you help me?

dellis1972 commented 1 week ago

dotnet run is not yet implemented you need to use dotnet <project> -t:Run -f net8.0-android

The dotnet team have only just added the support we needed to allow dotnet run to work (see https://github.com/dotnet/android/pull/9470). So this will not be implemented until .net 10.

The chances are the default targets running when you run dotnet run are running a design time build which is why things behave the way they do.

Anyway this should be resolved via https://github.com/dotnet/android/pull/9470

Jamlee commented 1 week ago

@dellis1972 Hi, Thanks for your help.

It seems that dotnet <project> -t:Run -f net8.0-android didn't work properly either.

➜  ControlCatalog.Android git:(master) ✗ dotnet ControlCatalog.Android.csproj -t:Run -f net8.0-android
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET program, but dotnet-ControlCatalog.Android.csproj does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

dotnet run hasn't been implemented yet. But why is it that dotnet run can start the simplest Android program? This is rather puzzling

dotnet new android
dotnet run
dellis1972 commented 1 week ago

sorry

dotnet build <project> -t:Run -f net8.0-android
dellis1972 commented 1 week ago

The current dotnet run is a hack, it does not work 100% of the time in all projects. PR https://github.com/dotnet/android/pull/9470 has the actual correct support. This was because of the way the dotnet team first implemented dotnet run it made some assumptions which make android support a bit unstable/difficult.