.NET for Android 錯誤 XA1041

範例訊息

error XA1041: The MSBuild property 'MonoAndroidAssetPrefix' has an invalid value of 'c:\Foo\Assets'. The value is expected to be a directory path representing the relative location of your Assets or Resources.
error XA1041: The MSBuild property 'MonoAndroidResourcePrefix' has an invalid value of 'c:\Foo\Resources'. The value is expected to be a directory path representing the relative location of your Assets or Resources.

Issue

問題在於,MonoAndroidResourcePrefix 和 MonoAndroidAssetPrefix 都應為相對目錄路徑。 這些資訊用來在我們打包應用程式時,將根目錄資訊從資產或資源中「剝離」出來。 錯誤之所以會被提出,是因為該性質包含一條完整的路徑。

舉例來說,考慮以下結構。

Foo.csproj
MyAssets/
    Foo.txt
    Subfolder/
        Bar.txt
MainActivity.cs

在最終的.apk階段,我們期望檔案Foo.txt和Bar.txt會被放入assets資料夾中。 為了正確執行這件事,建置系統需要知道資產所在資料夾的 。name 預設是「Assets」,但我們也應該支援其他資料夾名稱。

在上述範例中,我們預期使用者會將 設定 MonoAndroidAssetPrefix 為「MyAssets」的值。 這樣可以確保在包裝過程中移除通往「Foo.txt」等路徑中的「MyAssets」部分。 如果 MonoAndroidAssetPrefix 設定為 /Users/xxx/Foo/MyAssets 或 c:\Users\xxx\Foo\MyAssets,你會看到這個錯誤訊息。

解決方案

解決方法是不要為 MonoAndroidAssetPrefix 或 MonoAndroidResourcePrefix 的值使用完整路徑。 它們應該是相對的目錄路徑,與你資產存放的資料夾路徑相符。