使用VSCode构建.NET 8.0 MAUI iOS应用时google-services.json路径错误
构建iOS 18模拟器时出现以下错误:
Platforms/Android/google-services.json : error : The path '../../../../../../../../../Users/user/MyMauiApp/Platforms/Android/google-services.json' would result in a file outside of the app bundle and cannot be used. [/Users/user/MyMauiApp/MyMauiApp.csproj::TargetFramework=net8.0-ios]
即使在Android条件组中引用该文件,并尝试在iOS组中排除,问题仍存在。可尝试以下解决步骤:
统一路径分隔符:MSBuild在跨平台环境下对路径分隔符敏感,将csproj中所有路径的反斜杠
\替换为斜杠/,例如:<!-- Android组内的引用 --> <GoogleServicesJson Include="Platforms/Android/google-services.json" /> <!-- iOS组内的资源引用 --> <BundleResource Include="Platforms/iOS/GoogleService-Info.plist" Link="GoogleService-Info.plist" />全局排除+条件包含:先在全局范围内排除Android的google-services.json,再仅在Android构建时包含它,避免iOS构建时解析该文件:
<!-- 全局排除,确保默认不处理该文件 --> <ItemGroup> <None Remove="Platforms/Android/google-services.json" /> </ItemGroup> <!-- Android构建专属引用 --> <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'"> <PackageReference Include="Xamarin.AndroidX.Core" Version="1.12.0.4" /> <PackageReference Include="Xamarin.AndroidX.Collection" Version="1.4.0.2" /> <PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.4.0.1" /> <PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.8.2.1" /> <GoogleServicesJson Include="Platforms/Android/google-services.json" /> </ItemGroup>检查项目自动引用:打开csproj文件,确认是否存在无条件的
<ItemGroup>包含了Platforms/Android/google-services.json,如果有直接删除该条目。清理构建缓存:
- 关闭VSCode,删除项目根目录下的
bin和obj文件夹 - 执行终端命令:
dotnet clean,接着运行dotnet restore - 重启VSCode后重新构建iOS项目
- 关闭VSCode,删除项目根目录下的
验证NuGet包兼容性:确保iOS端的Firebase包(Xamarin.Firebase.iOS.Core)与.NET 8 MAUI兼容,尝试更新至最新的兼容版本。
内容的提问来源于stack exchange,提问作者Reid

