编译遇到的错误
初次编译Avalonia源码也许会遇到很多各种各样的问题。有一些问题根据错误提示能够比较容易的解决。但还有一些问题,根据错误提示也不太容易推测出来如何解决。在这里记录一下我遇到的一些问题,供遇到类似问题的朋友参考。
Namespace could not be found
bash
megatron@localhost:~/SourceCode/Avalonia$ ./build.sh --target Compile --configuration Release
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
Microsoft (R) .NET SDK version 7.0.404
/home/megatron/SourceCode/Avalonia/nukebuild/BuildTasksPatcher.cs(5,7): error CS0246: The type or namespace name 'ILRepacking' could not be found (are you missing a using directive or an assembly reference?) [/home/megatron/SourceCode/Avalonia/nukebuild/_build.csproj]
/home/megatron/SourceCode/Avalonia/nukebuild/Shims.cs(8,7): error CS0246: The type or namespace name 'Numerge' could not be found (are you missing a using directive or an assembly reference?) [/home/megatron/SourceCode/Avalonia/nukebuild/_build.csproj]
/home/megatron/SourceCode/Avalonia/nukebuild/Shims.cs(96,31): error CS0246: The type or namespace name 'INumergeLogger' could not be found (are you missing a using directive or an assembly reference?) [/home/megatron/SourceCode/Avalonia/nukebuild/_build.csproj]
/home/megatron/SourceCode/Avalonia/nukebuild/Shims.cs(98,25): error CS0246: The type or namespace name 'NumergeLogLevel' could not be found (are you missing a using directive or an assembly reference?) [/home/megatron/SourceCode/Avalonia/nukebuild/_build.csproj]
这是因为clone Avalonia代码库后,没有拉取submodule导致的。使用git submodule update --init
拉取submodule后重新编译源码即可。
File being used by another process
ruby
[ERR] Compile: /home/megatron/dotnet/sdk/7.0.404/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(211,5): error MSB4018: The "GenerateDepsFile" task failed unexpectedly. [/home/megatron/SourceCode/Avalonia/src/tools/DevAnalyzers/DevAnalyzers.csproj]
[ERR] Compile: /home/megatron/dotnet/sdk/7.0.404/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(211,5): error MSB4018: System.IO.IOException: The process cannot access the file '/home/megatron/SourceCode/Avalonia/src/tools/DevAnalyzers/bin/Release/netstandard2.0/DevAnalyzers.deps.json' because it is being used by another process. [/home/megatron/SourceCode/Avalonia/src/tools/DevAnalyzers/DevAnalyzers.csproj]
[ERR] Compile: /home/megatron/dotnet/sdk/7.0.404/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(211,5): error MSB4018: at Microsoft.Win32.SafeHandles.SafeFileHandle.Init(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Int64& fileLength, UnixFileMode& filePermissions) [/home/megatron/SourceCode/Avalonia/src/tools/DevAnalyzers/DevAnalyzers.csproj]
[ERR] Compile: /home/megatron/dotnet/sdk/7.0.404/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(211,5): error MSB4018: at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, UnixFileMode openPermissions, Int64& fileLength, UnixFileMode& filePermissions, Func`4 createOpenException) [/home/megatron/SourceCode/Avalonia/src/tools/DevAnalyzers/DevAnalyzers.csproj]
[ERR] Compile: /home/megatron/dotnet/sdk/7.0.404/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(211,5): error MSB4018: at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode) [/home/megatron/SourceCode/Avalonia/src/tools/DevAnalyzers/DevAnalyzers.csproj]
[ERR] Compile: /home/megatron/dotnet/sdk/7.0.404/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(211,5): error MSB4018: at System.IO.File.Create(String path) [/home/megatron/SourceCode/Avalonia/src/tools/DevAnalyzers/DevAnalyzers.csproj]
[ERR] Compile: /home/megatron/dotnet/sdk/7.0.404/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(211,5): error MSB4018: at Microsoft.NET.Build.Tasks.GenerateDepsFile.WriteDepsFile(String depsFilePath) [/home/megatron/SourceCode/Avalonia/src/tools/DevAnalyzers/DevAnalyzers.csproj]
[ERR] Compile: /home/megatron/dotnet/sdk/7.0.404/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(211,5): error MSB4018: at Microsoft.NET.Build.Tasks.TaskBase.Execute() [/home/megatron/SourceCode/Avalonia/src/tools/DevAnalyzers/DevAnalyzers.csproj]
[ERR] Compile: /home/megatron/dotnet/sdk/7.0.404/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(211,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [/home/megatron/SourceCode/Avalonia/src/tools/DevAnalyzers/DevAnalyzers.csproj]
[ERR] Compile: /home/megatron/dotnet/sdk/7.0.404/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(211,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [/home/megatron/SourceCode/Avalonia/src/tools/DevAnalyzers/DevAnalyzers.csproj]
使用fuser查看DevAnalyzers.deps.json并没有被别的进程使用。怀疑是并行编译导致的问题,不作任何修改,重新执行编译发现问题消失了。
The Android SDK directory could not be found
vbnet
[ERR] Compile: C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\33.0.95\tools\Xamarin.Android.Tooling.targets(70,5): error XA5300: The Android SDK directory could not be found. Install the Android SDK by following the instructions at: https://aka.ms/dotnet-android-install-sdk [C:\Users\megatron\SourceCode\Avalonia\src\Android\Avalonia.Android\Avalonia.Android.csproj]
[ERR] Compile: C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\33.0.95\tools\Xamarin.Android.Tooling.targets(70,5): error XA5300: To use a custom SDK path for a command line build, set the 'AndroidSdkDirectory' MSBuild property to the custom path. [C:\Users\megatron\SourceCode\Avalonia\src\Android\Avalonia.Android\Avalonia.Android.csproj]
[ERR] Compile: Target Compile has thrown an exception
这是在Windows平台编译Avalonia时遇到的问题。机器确实安装了Android SDK但还是报错。推测由于Android SDK安装到了一个编译脚本无法检测到的位置导致的。把Android SDK的安装路径移动到C:\Program files(x86)\Android\android-sdk
后问题得到了解决。
The Java SDK directory could not be found
vbnet
[ERR] Compile: C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\33.0.95\tools\Xamarin.Android.Tooling.targets(70,5): error XA5300: The Java SDK directory could not be found. Install the Java SDK by following the instructions at: https://aka.ms/dotnet-android-install-sdk [C:\Users\megatron\SourceCode\Avalonia\src\Android\Avalonia.Android\Avalonia.Android.csproj]
[ERR] Compile: C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\33.0.95\tools\Xamarin.Android.Tooling.targets(70,5): error XA5300: To use a custom JDK path for a command line build, set the 'JavaSdkDirectory' MSBuild property to the custom path. [C:\Users\megatron\SourceCode\Avalonia\src\Android\Avalonia.Android\Avalonia.Android.csproj]
[ERR] Compile: Target Compile has thrown an exception
去 learn.microsoft.com/en-us/java/... 下载了微软的OpenJDK安装后问题得到解决。
执行workload-install安装Tizen workload无效
这是在安装Tizen workload时遇到的问题。执行workload-install时立即结束,没有任何错误提示,但看起来什么也没有发生,去dotnet workload的目录会发现Tizen workload并没有完成安装。使用strace分析了一下workload-install的执行过程,发现是dotnet命令无法被workload-install脚本导致的。使用export DOTNET_ROOT=/home/megatron/dotnet
设置DOTNET_ROOT环境变量后,workload-install脚本就能正常执行了。
源码编译得到的nupkg
以下nupkg在Windows/Linux/macOS都能够编译出来的nupkg。
Package Name |
---|
Avalonia |
Avalonia.Browser |
Avalonia.Browser.Blazor |
Avalonia.Controls.ColorPicker |
Avalonia.Controls.DataGrid |
Avalonia.Controls.ItemsRepeater |
Avalonia.Desktop |
Avalonia.Diagnostics |
Avalonia.Direct2D1 |
Avalonia.Fonts.Inter |
Avalonia.FreeDesktop |
Avalonia.Headless |
Avalonia.Headless.NUnit |
Avalonia.Headless.Vnc |
Avalonia.Headless.XUnit |
Avalonia.LinuxFramebuffer |
Avalonia.Markup.Xaml.Loader |
Avalonia.ReactiveUI |
Avalonia.Remote.Protocol |
Avalonia.Skia |
Avalonia.Themes.Fluent |
Avalonia.Themes.Simple |
Avalonia.Win32 |
Avalonia.X11 |
还有几个nupkg只能在特定平台编译得到。
Package Name | Windows | Linux | macOS |
---|---|---|---|
Avalonia.Andriod | Yes | No | No |
Avalonia.iOS | Yes | No | No |
Avalonia.Win32.Interoperability | Yes | No | No |
Avalonia.Native | No | No | Yes |
可以看到如果想要通过编译得到所有的Avalonia nupkg,那么需要至少准备Windows和macOS两台机器来做编译。这里有个我还不太明白的地方:官方编译还需要Linux机器,既然Windows和macOS能编译出Avalonia所有的nupkg,那为什么还要再额外找一台Linux机器做编译呢?