Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check aar has the correct paths. #9918

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

dellis1972
Copy link
Contributor

@dellis1972 dellis1972 commented Mar 14, 2025

Context #9879

We had a bug report from a user where a zip file ended up with a rooted path as the name for a zip entry.

Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
     642  Defl:X      381  41% 03-06-2025 17:04 50f5aca8  assets/D:/a/1/s/Foo.Mobile/Foo.Mobile.Droid/Assets/AboutAssets.txt

We had a hard time understanding why this was happening until we really dug into the MSbuild logs
The customer in this case had the following environment variable set on their build process.

AndroidProjectFolder=d:/a/1/s/Foo.Mobile.Droid.

This environment variable effected the code in the Maui MultiTargeting.targets [1] which has the following code

 <AndroidProjectFolder>Platform\Android\</AndroidProjectFolder>
 <MonoAndroidResourcePrefix>$(AndroidProjectFolder)Resources</MonoAndroidResourcePrefix>
 <MonoAndroidAssetsPrefix>$(AndroidProjectFolder)Assets</MonoAndroidAssetsPrefix>

By default this is not a problem. But when the environment variable is set, MSbuild will NOT override the default value.
So in this case we ended up with a MonoAndroidAssetsPrefix with a value of d:/a/1/s/Foo.Mobile.Droid/Assets.
The then caused all sorts of issues when creating .aar files or creating the final apk.

So lets add a new error message to check if the MonoAndroidAssetsPrefix or the MonoAndroidResourcePrefix are rooted paths.

[1] https://github.com/dotnet/maui/blob/e41df8ea555f663a552806c075036150300893df/src/MultiTargeting.targets#L77

@dellis1972 dellis1972 force-pushed the dev/dellis1972/createaarissue9879 branch from 0792bd1 to 3b97d32 Compare April 2, 2025 12:22
@dellis1972 dellis1972 force-pushed the dev/dellis1972/createaarissue9879 branch from 3b97d32 to 446cd14 Compare April 7, 2025 08:02
@dellis1972 dellis1972 marked this pull request as ready for review April 7, 2025 12:36
@jonpryor
Copy link
Member

jonpryor commented Apr 8, 2025

Draft commit message:

Context: https://github.com/dotnet/android/issues/9879

If you create an Android Library project which contains
Android Resources:

	% dotnet new androidlib
	% mkdir -p Resources/values
	% cat > Resources/values/strings.xml <<EOF
	<resources>
	    <string name="lib_name">net9_androidlib</string>
	</resources>
	EOF

then build the project with `$(MonoAndroidAssetsPrefix)` set to an
absolute path:

	dotnet build -p:MonoAndroidAssetsPrefix=`pwd`/Resources

then the resulting `$(MSBuildProjectName).aar` file contains the
absolute path!

	% unzip -l bin/Debug/net9.0-android/net9-androidlib.aar
	Archive:  bin/Debug/net9.0-android/net9-androidlib.aar
	  Length      Date    Time    Name
	---------  ---------- -----   ----
	       78  04-08-2025 14:32   assets//Users/…/net9-androidlib/Resources/values/strings.xml
	       71  04-08-2025 14:32   res/values/strings.xml
	       38  04-08-2025 14:32   .net/__res_name_case_map.txt

This is "nominally fine" on macOS, but if Windows is the source
machine then the entry within the `.aar` contains a `:`, a'la
`assets/D:/a/1/s/Foo.Mobile/Foo.Mobile.Droid/Assets/AboutAssets.txt`,
which can cause a subsequent error XARLP7024 as in dotnet/android#9879:

	…\Xamarin.Android.EmbeddedResource.targets(39,5): error XARLP7024: System.IO.IOException: The filename, directory name, or volume label syntax is incorrect. : 'D:\a\1\s\MyCompany.Mobile\MyCompany.Mobile.Droid\obj\Release\net9.0-android\lp\183\jl\assets\D:\a\1\s\MyCompany.Mobile\MyCompany.Mobile.Droid\Assets'.
	…\Xamarin.Android.EmbeddedResource.targets(39,5): error XARLP7024:    at System.IO.FileSystem.CreateDirectory(String fullPath, Byte[] securityDescriptor)
	…\Xamarin.Android.EmbeddedResource.targets(39,5): error XARLP7024:    at Microsoft.Android.Build.Tasks.Files.CopyIfStreamChangedOnce(Stream stream, String destination) in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/Files.cs:line 263
	…\Xamarin.Android.EmbeddedResource.targets(39,5): error XARLP7024:    at Microsoft.Android.Build.Tasks.Files.CopyIfStreamChanged(Stream stream, String destination) in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/Files.cs:line 238
	…\Xamarin.Android.EmbeddedResource.targets(39,5): error XARLP7024:    at Microsoft.Android.Build.Tasks.Files.ExtractAll(ZipArchive zip, String destination, Action`2 progressCallback, Func`2 modifyCallback, Func`2 deleteCallback, Func`2 skipCallback) in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/Files.cs:line 470
	…\Xamarin.Android.EmbeddedResource.targets(39,5): error XARLP7024:    at Xamarin.Android.Tasks.ResolveLibraryProjectImports.Extract(IDictionary`2 jars, ICollection`1 resolvedResourceDirectories, ICollection`1 resolvedAssetDirectories, ICollection`1 resolvedEnvironments, ICollection`1 proguardConfigFiles) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs:line 398
	…\Xamarin.Android.EmbeddedResource.targets(39,5): error XARLP7024:    at Xamarin.Android.Tasks.ResolveLibraryProjectImports.RunTask() in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs:line 95
	…\Xamarin.Android.EmbeddedResource.targets(39,5): error XARLP7024:    at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25

which is *not* fine.

Update the `<CreateAar/>` and `<AndroidComputeResPaths/>` tasks to
verify that the `$(MonoAndroidAssetsPrefix)` MSBuild value is *not*
an absolute path value.  If it is, then the "originating" build will
report an error xa1041:

	error XA1041: The MSBuild property 'MonoAndroidAssetsPrefix' has an invalid value of '/Absolute/Path'.
	The value is expected to be a directory path representing the relative location of your Assets or Resources.

@jonpryor
Copy link
Member

jonpryor commented Apr 8, 2025

@dellis1972: also please update Documentation/docs-mobile/building-apps/build-properties.md to document $(MonoAndroidAssetsPrefix).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants