diff --git a/Documentation/guides/AndroidAssetPacks.md b/Documentation/guides/AndroidAssetPacks.md new file mode 100644 index 00000000000..920bc2d5959 --- /dev/null +++ b/Documentation/guides/AndroidAssetPacks.md @@ -0,0 +1,235 @@ +# Android Asset Packs + +Google Android began supporting splitting up the app package into multiple +packs with the introduction of the `aab` package format. This format allows +the developer to split the app up into multiple `packs`. Each `pack` can be +downloaded to the device either at install time or on demand. This allows +application developers to save space and install time by only installing +the required parts of the app initially, then installing other `packs` +as required. + +There are two types of `pack`: Feature packs and Asset packs. +*Feature* pack contains *non-native* Java code and other resources. +Code in these types of `pack` can be launched via the `Context.StartActivity()` +API call. At this time due to various constraints .NET Android cannot support +Feature packs. + +*Asset* packs contain *only* +[`@(AndroidAsset)`](~/android/deploy-test/building-apps/build-items.md#androidasset) items. +It *cannot* contain any code or other resources. This type of `pack` can be +installed at install-time, fast-follow or ondemand. It is most useful for apps +which contain a lot of Assets, such as Games or Multi Media applications. +See the [Android Asset Delivery documentation](https://developer.android.com/guide/playcore/asset-delivery) +for details on how this all works. + +## Asset Pack Specification + +We want to provide our users the ability to use `Asset` packs without relying +on [Alternative Methods](#alternativemethods) + +Add support for new `%(AndroidAsset.AssetPack)` item metadata, which +allows the build system to split up the assets into packs automatically: + +```xml + + + + + +``` + +The default value for `%(AndroidAsset.AssetPack)` is `base`, which will +cause the asset to be included in the main application package. + +As auto import of items is now common, we need a way for a user to add +this additional attribute to auto included items. This can be done by +using `Update`: + +```xml + + + + + +``` + +`%(AndroidAsset.DeliveryType)` item metadata can be specified to control what +*type* of asset pack is produced. Valid values are: + + * `InstallTime`: Asset pack will be delivered when the app is installed. + This is the default value for assets not in the base package. + * `FastFollow`: Asset pack will be downloaded automatically as soon as the app is installed. + * `OnDemand`: Asset pack will be downloaded while the app is running. + +The `DeliveryType` for a given asset pack is based on the *first* +`@(AndroidAsset.DeliveryType)` value encountered for a `%(AssetPack)` name. + +Consider the following example, in which `Asset/movie2.mp4` and `Asset/movie3.mp4` +are both in the `assets1` pack, which will have a `%(DeliveryType)` of `InstallTime` +(the first encountered value "wins"). `Asset/movie1.mp4` will be in the base package, +while `Asset/movie4.mp4` will be in the "asset2" asset pack. + +```xml + + + + + + +``` + +See Google's [documentation](https://developer.android.com/guide/playcore/asset-delivery#asset-updates) for details on what each of the `DeliveryType` values do. + +If however you have a large number of assets it might be cleaner in the +`.csproj` to make use of the `base` value for the `%(AssetPack)` attribute. +In this scenario you update *all* assets to be in a single asset pack then use +`AssetPack="base"` metadata to declare which specific assets end up in the base +aab file. With this you can use wildcards to move most assets into the asset pack: + +```xml + + + + + +``` + +In this example, `movie.mp4` and `some.png` will end up in the `base` aab file, +but *all the other assets* will end up in the `assets1` asset pack. + +At this time the `@(AndroidAsset)` build action does not support `%(AssetPack)` +or `%(DeliveryType)` Metadata in Library Projects. + +NOTE: `AssetPacks` are only used when the +[`$(AndroidPackageFormat)`](~/android/deploy-test/building-apps/build-properties.md#debugsymbols) +property is set to `aab` (the default for Release). +When using the `apk` setting the assets will be placed inside the `apk`. + +## Release Configuration + +In order for the application to function correctly we need to inform the `R8` +linker which Java classes we need to keep. To do this we need to add the +following lines to a `ProGuard.cfg` file which is in the root of our project folder: + +``` +-keep com.google.android.play.* +``` + +Alternatively you can create a file called `ProGuard.cfg` and use the +[@(ProguardConfiguration)](~/android/deploy-test/building-apps/build-items.md#proguardconfiguration) +build action. Adding these lines will ensure that all the required Java components are not linked +away during the Release build. + +## Testing and Debugging + +In order to test your asset packs in the `Debug` configuration, you will need to +make some changes to your `.csproj`. Firstly we need to change the +`$(AndroidPackageFormat)` to `aab`. It will be `aab` by default for `Release` builds, +but will default to `apk` for `Debug` builds. Setting the `AndroidPackageFormat` to `aab` +will disable fast deployment, so it is advised that you only do this when you need to test +your `AssetPacks`. + +To test your asset packs add the following to the first `` in your `.csproj`. + +```xml +aab +--local-testing $(AndroidBundleToolExtraArgs) +``` + +The `--local-testing` argument tells the `bundletool` application to install all the asset packs +in a local cache on the device. `InstallTime` packs will be installed during the app installation process. + +`FastFollow` packs behave like `OnDemand` packs. They will not automatically installed when the app +is sideloaded. You will need to request them manually when the game starts. + +For more details see [https://developer.android.com/guide/playcore/asset-delivery/test](https://developer.android.com/guide/playcore/asset-delivery/test). + +## Implementation Details + +There are a few changes we need to make in order to support this feature. +One of the issues we will hit is the build times when dealing with large assets. +Current the assets which are to be included in the `aab` are ***copied*** +into the `$(IntermediateOutputPath)assets` directory. This folder is +then passed to `aapt2` for the build process. + +The new system adds a new directory `$(IntermediateOutputPath)assetpacks`. +This directory would contain a subdirectory for each `pack` that the +user wants to include. + +```dotnetcli +assetpacks/ + assets1/ + assets/ + movie2.mp4 + assets2/ + assets/ + movie3.mp4 +``` + +All the building of the `pack` zip file would take place in these subfolders. +The name of the pack will be based on the main "packagename" with the asset pack +name appended to the end. e.g `com.microsoft.assetpacksample.assets1`. + +During the build process we identify all the `AndroidAsset` items which +have an `AssetPack` attribute. These files are then copied to the +new `$(IntermediateOutputPath)assetpacks` directory rather than the +existing `$(IntermediateOutputPath)assets` directory. This allows us to +continue to support the normal `AndroidAsset` behavior while adding the +new system. + +Once we have collected and copied all the assets we then use the new +`` Task to figure out which asset packs we need to create. +We then call the `` task to create a required +`AndroidManifest.xml` file for the asset pack. This file will end +up in the same `$(IntermediateOutputPath)assetpacks` directory. +We call this Task `` because it can be used +to create any feature pack if and when we get to implement full feature +packs. + +```dotnetcli +assetpacks/ + assets1/ + AndroidManifest.xml + assets/ + movie2.mp4 + assets2/ + AndroidManifest.xml + assets/ + movie3.mp4 +``` + +We can then call `aapt2` to build these packs into `.zip` files. A new +task `` task takes care of this. This is a special version +of `Aapt2Link` which implements linking for asset packs only. +It also takes care of a few problems which `aapt2` introduces. For some +reason the zip file that is created has the `AndroidManifest.xml` file +in the wrong place. It creates it in the root of the zip file, but the +`bundletool` expects it to be in a `manifest` directory. +`bundletool` will error out if its not in the right place. +So `` takes care of this for us. It also removes a +`resources.pb` which gets added. Again, `bundletool` will error if this +file is in the zip file. + +Once the zip files have been created they are then added to the +`@(AndroidAppBundleModules)` ItemGroup. This will ensure that when the +final `.aab` file is generated they are included as asset packs. + +## Alternative Methods + +An alternative method is available on [github](https://github.com/infinitespace-studios/MauiAndroidAssetPackExample). +This method allows developers to place additional assets in a special +[NoTargets](https://github.com/microsoft/MSBuildSdks/blob/main/src/NoTargets/README.md) project. +This project is built just after the final `aab` is produced. It builds a zip +file which is then added to the `@(Modules)` ItemGroup in the main application. +This zip is then included into the +final app as an additional feature. + +Using a separate project like in the hack is one way to go. It does have some +issues though. + + 1. It is a `special` type of project. It requires a `global.json` which imports + the `NoTargets` sdk. + 2. There is no IDE support for building this type of project. + +Having the user go through a number of hoops to implement this for +.NET Android or .NET MAUI is not ideal. diff --git a/Documentation/guides/building-apps/build-items.md b/Documentation/guides/building-apps/build-items.md index cd78fd4fa02..176e875aa3a 100644 --- a/Documentation/guides/building-apps/build-items.md +++ b/Documentation/guides/building-apps/build-items.md @@ -19,6 +19,37 @@ or library project is built. Supports [Android Assets](https://developer.android.com/guide/topics/resources/providing-resources#OriginalFiles), files that would be included in the `assets` folder in a Java Android project. +Starting with .NET 9 the `@(AndroidAsset)` build action also supports additional metadata for generating [Asset Packs](https://developer.android.com/guide/playcore/asset-delivery). The `%(AndroidAsset.AssetPack)` metadata can be used to automatically generate an asset pack of that name. This feature is only supported when the [`$(AndroidPackageFormat)`](#androidpackageformat) is set to `.aab`. The following example will place `movie2.mp4` and `movie3.mp4` in separate asset packs. + +```xml + + + + + +``` + +This feature can be used to include large files in your application which would normally exceed the max +package size limits of Google Play. + +If you have a large number of assets it might be more efficient to make use of the `base` asset pack. +In this scenario you update ALL assets to be in a single asset pack then use the `AssetPack="base"` metadata +to declare which specific assets end up in the base aab file. With this you can use wildcards to move most +assets into the asset pack. + +```xml + + + + + +``` + +In this example, `movie.mp4` and `some.png` will end up in the `base` aab file, while all the other assets +will end up in the `assets1` asset pack. + +The additional metadata is only supported on .NET Android 9 and above. + ## AndroidAarLibrary The Build action of `AndroidAarLibrary` should be used to directly diff --git a/Documentation/guides/building-apps/build-process.md b/Documentation/guides/building-apps/build-process.md index 42e4837ed9a..0ec934e1610 100644 --- a/Documentation/guides/building-apps/build-process.md +++ b/Documentation/guides/building-apps/build-process.md @@ -195,6 +195,7 @@ Extension points include: - [`$(AfterGenerateAndroidManifest)](~/android/deploy-test/building-apps/build-properties.md#aftergenerateandroidmanifest) - [`$(BeforeGenerateAndroidManifest)](~/android/deploy-test/building-apps/build-properties.md#beforegenerateandroidmanifest) +- [`$(BeforeBuildAndroidAssetPacks)`](~/android/deploy-test/building-apps/build-properties.md#beforebuildandroidassetpacks) A word of caution about extending the build process: If not written correctly, build extensions can affect your build diff --git a/Documentation/guides/building-apps/build-properties.md b/Documentation/guides/building-apps/build-properties.md index cd29dca973e..5e3468f8a55 100644 --- a/Documentation/guides/building-apps/build-properties.md +++ b/Documentation/guides/building-apps/build-properties.md @@ -832,6 +832,18 @@ APK root directory. The format of the path is `lib\ARCH\wrap.sh` where + `x86_64` + `x86` +## AndroidIncludeAssetPacksInPackage + +This property controls if an Asset Packs build automatically are auto +included in the final `.aab` file. It will default to `true`. + +In certain cases the user might want to release an interim release. In +these cases the user does not need to update the asset pack. Especially +if the contents of the asset pack have not changed. This property allows +the user to skip the asset packs if they are not required. + +Added in .NET 9 + ## AndroidInstallJavaDependencies The default value is `true` for command line builds. When set to `true`, enables @@ -1674,6 +1686,13 @@ as support for `$(AotAssemblies)` will be removed in a future release. Extra options to pass to `aprofutil`. +## BeforeBuildAndroidAssetPacks + +MSBuild Targets listed in this +property will run directly before the `AssetPack` items are built. + +Added in .NET 9 + ## BeforeGenerateAndroidManifest MSBuild Targets listed in this diff --git a/Documentation/guides/messages/README.md b/Documentation/guides/messages/README.md index bab9d5dc5d0..df2c6915f35 100644 --- a/Documentation/guides/messages/README.md +++ b/Documentation/guides/messages/README.md @@ -100,6 +100,9 @@ package from all the users on device and try again. If that does not work you ca Fast Deployment is not currently supported on this device. Please file an issue with the exact error message using the 'Help->Send Feedback->Report a Problem' menu item in Visual Studio or 'Help->Report a Problem' in Visual Studio for Mac. ++ [XA0138](xa0138.md): %(AndroidAsset.AssetPack) and %(AndroidAsset.AssetPack) item metadata are only supported when `$(AndroidApplication)` is `true`. ++ [XA0139](xa0139.md): `@(AndroidAsset)` `{0}` has invalid `DeliveryType` metadata of `{1}`. Supported values are `installtime`, `ondemand` or `fastfollow` ++ [XA0140](xa0140.md): ## XA1xxx: Project related diff --git a/Documentation/guides/messages/xa0138.md b/Documentation/guides/messages/xa0138.md new file mode 100644 index 00000000000..6dc8a4341ae --- /dev/null +++ b/Documentation/guides/messages/xa0138.md @@ -0,0 +1,13 @@ +title: Xamarin.Android error XA0138 +description: XA0138 error code +ms.date: 02/05/2024 +--- +# Xamarin.Android error XA0138 + +## Issue + +%(AndroidAsset.AssetPack) and %(AndroidAsset.AssetPack) item metadata are only supported when `$(AndroidApplication)` is `true`. + +## Solution + +Remove the 'AssetPack' or 'DeliveryType' Metadata from your `AndroidAsset` build Items in the project the error was raised for. diff --git a/Documentation/guides/messages/xa0139.md b/Documentation/guides/messages/xa0139.md new file mode 100644 index 00000000000..1ba498b9736 --- /dev/null +++ b/Documentation/guides/messages/xa0139.md @@ -0,0 +1,13 @@ +title: Xamarin.Android error XA0138 +description: XA0138 error code +ms.date: 02/05/2024 +--- +# Xamarin.Android error XA0138 + +## Issue + +`@(AndroidAsset)` `{0}` has an invalid `DeliveryType` metadata of `{1}`. Supported values are `installtime`, `ondemand` or `fastfollow`. + +## Solution + +Make sure that all `DeliveryType` attributes are one of the following valid values, `installtime`, `ondemand` or `fastfollow`. diff --git a/Documentation/guides/messages/xa0140.md b/Documentation/guides/messages/xa0140.md new file mode 100644 index 00000000000..3d25181c887 --- /dev/null +++ b/Documentation/guides/messages/xa0140.md @@ -0,0 +1,13 @@ +title: Xamarin.Android error XA0138 +description: XA0140 error code +ms.date: 02/05/2024 +--- +# Xamarin.Android error XA0140 + +## Issue + +The AssetPack value defined for `{0}` has invalid characters. `{1}` should only contain A-z, a-z, 0-9 or an underscore. + +## Solution + +Make sure that all `AssetPack` attributes only contain valid characters. diff --git a/build-tools/installers/create-installers.targets b/build-tools/installers/create-installers.targets index 562180003e0..c3897d62330 100644 --- a/build-tools/installers/create-installers.targets +++ b/build-tools/installers/create-installers.targets @@ -136,6 +136,7 @@ <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Aapt2.targets" /> <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Analysis.targets" /> <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Application.targets" /> + <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Assets.targets" /> <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Bindings.ClassParse.targets" /> <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Bindings.Core.targets" /> <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)Xamarin.Android.Bindings.Maven.targets" /> diff --git a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Assets.targets b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Assets.targets new file mode 100644 index 00000000000..00dc628acaa --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Assets.targets @@ -0,0 +1,151 @@ + + + + + + + + + + + + + $(IntermediateOutputPath)assets\ + $(IntermediateOutputPath)assetpacks + Assets + true + + + + + UpdateAndroidAssets + ;_CalculateAssetsWithAssetPackMetaData + ;_CalculateAssetPacks + ;$(BeforeBuildAndroidAssetPacks) + ;_CreateAssetPackManifests + + + + + + + + + + + + + + + + + <_AssetDirectories Include="$(MonoAndroidAssetsDirIntermediate)" /> + <_AssetDirectories Include="@(_AssetPacks->'%(AssetPackDirectory)')" /> + + + + + + + + + + + + + <_AssetsWithAssetPackMetaData Include="@(AndroidAsset)" Condition=" '%(AndroidAsset.AssetPack)' != '' " /> + + + + + + + + + + + <_AssetPacks Include="@(_AndroidAsset)"> + $(MonoAndroidAssetPacksDirIntermediate)\%(_AndroidAsset.AssetPack)\assets + $(MonoAndroidAssetPacksDirIntermediate)\%(_AndroidAsset.AssetPack).zip + $(MonoAndroidAssetPacksDirIntermediate)\%(_AndroidAsset.AssetPack)\AndroidManifest.xml + InstallTime + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.BuildOrder.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.BuildOrder.targets index 19d0c698d1f..31c9442e8e9 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.BuildOrder.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.BuildOrder.targets @@ -76,6 +76,7 @@ properties that determine build ordering. _LintChecks; _IncludeNativeSystemLibraries; _CheckGoogleSdkRequirements; + BuildAndroidAssetPacks; @@ -91,6 +92,7 @@ properties that determine build ordering. _AddAndroidDefines; _CheckForContent; _CheckForObsoleteFrameworkAssemblies; + _CalculateAssetsWithAssetPackMetaData; _RemoveLegacyDesigner; _ValidateAndroidPackageProperties; AddLibraryJarsToBind; diff --git a/src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs b/src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs index e46244bf171..c5df25bdcc2 100644 --- a/src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs +++ b/src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs @@ -1600,5 +1600,15 @@ public static string XA1036 { return ResourceManager.GetString("XA1036", resourceCulture); } } + + /// + /// Looks up a localized string similar to AndroidManifest.xml //uses-sdk/@android:minSdkVersion '{0}' does not match the $(SupportedOSPlatformVersion) value '{1}' in the project file (if there is no $(SupportedOSPlatformVersion) value in the project file, then a default value has been assumed). + ///Either change the value in the AndroidManifest.xml to match the $(SupportedOSPlatformVersion) value, or remove the value in the AndroidManifest.xml (and add a $(SupportedOSPlatformVersion) value to the project file if it doesn't already exist).. + /// + public static string XA1039 { + get { + return ResourceManager.GetString("XA1039", resourceCulture); + } + } } } diff --git a/src/Xamarin.Android.Build.Tasks/Properties/Resources.resx b/src/Xamarin.Android.Build.Tasks/Properties/Resources.resx index a1a45e18ff3..f473cd0f265 100644 --- a/src/Xamarin.Android.Build.Tasks/Properties/Resources.resx +++ b/src/Xamarin.Android.Build.Tasks/Properties/Resources.resx @@ -328,6 +328,20 @@ If this file comes from a NuGet package, update to a newer version of the NuGet The following are literal names and should not be translated: 'DebugType', 'portable' The capitalized word "Portable" that appears earlier in the message is plain text and should be translated, but the lowercase word "portable" later in the message is a literal value and should not be translated. {0} - The file name of a deprecated symbol file + + + %(AndroidAsset.AssetPack) and %(AndroidAsset.AssetPack) item metadata are only supported when `$(AndroidApplication)` is `true`. + + + + `@(AndroidAsset)` `{0}` has an invalid `DeliveryType` metadata of `{1}`. Supported values are `installtime`, `ondemand` or `fastfollow`. + {0} - The file name +{1} - The value of the attribute in the project file. + + + The AssetPack value defined for `{0}` has invalid characters. `{1}` should only contain A-z, a-z, 0-9 or an underscore. + {0} - The file name +{1} - The value of the attribute in the metadata. There was a problem parsing {0}. This is likely due to incomplete or invalid XML. Exception: {1} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Aapt2LinkAssetPack.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Aapt2LinkAssetPack.cs new file mode 100644 index 00000000000..7c9a1f16a5a --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Aapt2LinkAssetPack.cs @@ -0,0 +1,74 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Threading; +using System.Xml; +using System.Xml.Linq; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Text.RegularExpressions; +using System.Collections.Generic; +using Xamarin.Android.Tools; +using Microsoft.Android.Build.Tasks; + +namespace Xamarin.Android.Tasks { + + public class Aapt2LinkAssetPack : Aapt2 { + public override string TaskPrefix => "A2LAP"; + + [Required] + public ITaskItem Manifest { get; set; } + + [Required] + public ITaskItem[] AssetDirectories { get; set; } + + [Required] + public string PackageName { get; set; } + + [Required] + public ITaskItem OutputArchive { get; set; } + + protected override int GetRequiredDaemonInstances () + { + return Math.Min (1, DaemonMaxInstanceCount); + } + + public async override System.Threading.Tasks.Task RunTaskAsync () + { + RunAapt (GenerateCommandLineCommands (Manifest, OutputArchive), OutputArchive.ItemSpec); + ProcessOutput (); + if (File.Exists (OutputArchive.ItemSpec)) { + // move the manifest to the right place. + using (var zip = new ZipArchiveEx (OutputArchive.ItemSpec, File.Exists (OutputArchive.ItemSpec) ? FileMode.Open : FileMode.Create)) { + zip.MoveEntry ("AndroidManifest.xml", "manifest/AndroidManifest.xml"); + zip.Archive.DeleteEntry ("resources.pb"); + // Fix up aapt2 not dealing with '\' in subdirectories for assets. + zip.FixupWindowsPathSeparators ((a, b) => Log.LogDebugMessage ($"Fixing up malformed entry `{a}` -> `{b}`")); + } + } + await System.Threading.Tasks.Task.CompletedTask; + } + + protected string[] GenerateCommandLineCommands (ITaskItem manifest, ITaskItem output) + { + //link --manifest AndroidManifest.xml --proto-format --custom-package $(Package) -A $(AssetsDirectory) -o $(_TempOutputFile) + List cmd = new List (); + cmd.Add ("link"); + if (MonoAndroidHelper.LogInternalExceptions) + cmd.Add ("-v"); + cmd.Add ("--manifest"); + cmd.Add (GetFullPath (manifest.ItemSpec)); + cmd.Add ("--proto-format"); + cmd.Add ("--custom-package"); + cmd.Add (PackageName); + foreach (var assetDirectory in AssetDirectories) { + cmd.Add ("-A"); + cmd.Add (GetFullPath (assetDirectory.ItemSpec)); + } + cmd.Add ($"-o"); + cmd.Add (GetFullPath (output.ItemSpec)); + return cmd.ToArray (); + } + } +} \ No newline at end of file diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/AndroidComputeResPaths.cs b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidComputeResPaths.cs index 96a3ccadbe9..4115073151c 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/AndroidComputeResPaths.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidComputeResPaths.cs @@ -45,6 +45,8 @@ public class AndroidComputeResPaths : AndroidTask [Required] public string IntermediateDir { get; set; } + public string AssetPackIntermediateDir { get; set; } + public string Prefixes { get; set; } public bool LowercaseFilenames { get; set; } @@ -83,6 +85,7 @@ public override bool RunTask () continue; //compute the target path string rel; + var assetPack = item.GetMetadata ("AssetPack"); var logicalName = item.GetMetadata ("LogicalName").Replace ('\\', Path.DirectorySeparatorChar); if (item.GetMetadata ("IsWearApplicationResource") == "True") { rel = item.ItemSpec.Substring (IntermediateDir.Length); @@ -126,6 +129,13 @@ public override bool RunTask () } string dest = Path.GetFullPath (Path.Combine (IntermediateDir, baseFileName)); string intermediateDirFullPath = Path.GetFullPath (IntermediateDir); + if (!string.IsNullOrEmpty (assetPack) && + (string.Compare (assetPack, "base", StringComparison.OrdinalIgnoreCase) != 0) && + !string.IsNullOrEmpty (AssetPackIntermediateDir)) { + dest = Path.GetFullPath (Path.Combine (AssetPackIntermediateDir, assetPack, "assets", baseFileName)); + intermediateDirFullPath = Path.GetFullPath (AssetPackIntermediateDir); + } + // if the path ends up "outside" of our target intermediate directory, just use the filename if (String.Compare (intermediateDirFullPath, 0, dest, 0, intermediateDirFullPath.Length, StringComparison.OrdinalIgnoreCase) != 0) { dest = Path.GetFullPath (Path.Combine (IntermediateDir, Path.GetFileName (baseFileName))); diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CreateDynamicFeatureManifest.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CreateDynamicFeatureManifest.cs new file mode 100644 index 00000000000..48e6fbb9bca --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CreateDynamicFeatureManifest.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Xml; +using System.Xml.Linq; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; + +using Xamarin.Android.Tools; +using Microsoft.Android.Build.Tasks; + +namespace Xamarin.Android.Tasks +{ + public class CreateDynamicFeatureManifest : AndroidTask + { + readonly XNamespace androidNS = "http://schemas.android.com/apk/res/android"; + readonly XNamespace distNS = "http://schemas.android.com/apk/distribution"; + readonly XNamespace toolsNS = "http://schemas.android.com/tools"; + + public override string TaskPrefix => "CDFM"; + + [Required] + public string FeatureSplitName { get; set; } + + [Required] + public string FeatureDeliveryType { get; set; } + + [Required] + public string FeatureType { get; set; } + + [Required] + public string PackageName { get; set; } + + [Required] + public ITaskItem OutputFile { get; set; } + + public string FeatureTitleResource { get; set; } + + public string MinSdkVersion { get; set; } + + public string TargetSdkVersion { get; set; } + + public bool IsFeatureSplit { get; set; } = false; + public bool IsInstant { get; set; } = false; + public bool HasCode { get; set; } = false; + + public override bool RunTask () + { + XDocument doc = new XDocument (); + switch (FeatureType.ToLowerInvariant ()) { + case "assetpack": + GenerateAssetPackManifest (doc); + break; + default: + GenerateFeatureManifest (doc); + break; + } + doc.SaveIfChanged (OutputFile.ItemSpec); + return !Log.HasLoggedErrors; + } + + void GenerateFeatureManifest (XDocument doc) + { + XAttribute featureTitleResource = null; + if (!string.IsNullOrEmpty (FeatureTitleResource)) + featureTitleResource = new XAttribute (distNS + "title", FeatureTitleResource); + XElement usesSdk = new XElement ("uses-sdk"); + if (!string.IsNullOrEmpty (MinSdkVersion)) + usesSdk.Add (new XAttribute (androidNS + "minSdkVersion", MinSdkVersion)); + if (!string.IsNullOrEmpty (MinSdkVersion)) + usesSdk.Add (new XAttribute (androidNS + "targetSdkVersion", TargetSdkVersion)); + doc.Add (new XElement ("manifest", + new XAttribute (XNamespace.Xmlns + "android", androidNS), + new XAttribute (XNamespace.Xmlns + "tools", toolsNS), + new XAttribute (XNamespace.Xmlns + "dist", distNS), + new XAttribute (androidNS + "versionCode", "1"), + new XAttribute (androidNS + "versionName", "1.0"), + new XAttribute ("package", PackageName), + new XAttribute ("featureSplit", FeatureSplitName), + new XAttribute (androidNS + "isFeatureSplit", IsFeatureSplit), + new XElement (distNS + "module", + featureTitleResource, + new XAttribute (distNS + "instant", IsInstant), + new XElement (distNS + "delivery", + GetDistribution () + ), + new XElement (distNS + "fusing", + new XAttribute (distNS + "include", true) + ) + ), + usesSdk, + new XElement ("application", + new XAttribute (androidNS + "hasCode", HasCode), + new XAttribute (toolsNS + "replace", "android:hasCode") + ) + ) + ); + } + + void GenerateAssetPackManifest (XDocument doc) + { + doc.Add (new XElement ("manifest", + new XAttribute (XNamespace.Xmlns + "android", androidNS), + new XAttribute (XNamespace.Xmlns + "tools", toolsNS), + new XAttribute (XNamespace.Xmlns + "dist", distNS), + new XAttribute ("package", PackageName), + new XAttribute ("split", FeatureSplitName), + new XElement (distNS + "module", + new XAttribute (distNS + "type", "asset-pack"), + new XElement (distNS + "delivery", + GetDistribution () + ), + new XElement (distNS + "fusing", + new XAttribute (distNS + "include", true) + ) + ) + ) + ); + } + + XElement GetDistribution () + { + XElement distribution; + switch (FeatureDeliveryType.ToLowerInvariant ()) + { + case "ondemand": + distribution = new XElement (distNS + "on-demand"); + break; + case "fastfollow": + distribution = new XElement (distNS + "fast-follow"); + break; + case "installtime": + default: + distribution = new XElement (distNS + "install-time"); + break; + } + return distribution; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetAssetPacks.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetAssetPacks.cs new file mode 100644 index 00000000000..4645204bd1d --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetAssetPacks.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Microsoft.Android.Build.Tasks; +using Irony; +using System.Text.RegularExpressions; + +namespace Xamarin.Android.Tasks +{ + // We have a list of files, we want to get the + // ones that actually exist on disk. + public class GetAssetPacks : AndroidTask + { + readonly Regex validAssetPackName = new Regex ("^[A-Z0-9_]+$", RegexOptions.Compiled | RegexOptions.IgnoreCase); + public override string TaskPrefix => "GAP"; + + [Required] + public ITaskItem[] Assets { get; set; } + + [Required] + public ITaskItem IntermediateDir { get; set; } + + public string[] MetadataToCopy { get; set; } = { "DeliveryType" }; + + [Output] + public ITaskItem[] AssetPacks { get; set; } + + public override bool RunTask () + { + Dictionary assetPacks = new Dictionary (); + Dictionary> files = new Dictionary> (); + foreach (var asset in Assets) { + var assetPack = asset.GetMetadata ("AssetPack"); + if (string.IsNullOrEmpty (assetPack) || string.Compare (assetPack, "base", StringComparison.OrdinalIgnoreCase) == 0) + continue; + if (!IsAssetPackNameValid (assetPack)) { + Log.LogCodedError ("XA0140", $"The AssetPack value defined for {asset.ItemSpec} is invalid. '{assetPack}' should match the following Regex '[A-Za-z0-9_]'."); + continue; + } + if (!assetPacks.TryGetValue (assetPack, out ITaskItem item)) { + item = new TaskItem (assetPack); + item.SetMetadata ("AssetPack", assetPack); + item.SetMetadata ("AssetPackCacheFile", Path.Combine (IntermediateDir.ItemSpec, assetPack, "assetpack.cache")); + assetPacks[assetPack] = item; + } + foreach (var metadata in MetadataToCopy) { + if (string.IsNullOrEmpty (item.GetMetadata (metadata))) + item.SetMetadata (metadata, asset.GetMetadata (metadata)); + } + if (!files.ContainsKey (assetPack)) { + files[assetPack] = new List (); + } + files[assetPack].Add (asset.ItemSpec); + } + + foreach (var kvp in assetPacks) { + // write out the file cache list + // write out the metadata as well. + ITaskItem item = kvp.Value; + var cacheFile = kvp.Value.GetMetadata ("AssetPackCacheFile"); + using (var sw = MemoryStreamPool.Shared.CreateStreamWriter ()) { + foreach (var file in files [kvp.Key]) { + sw.WriteLine ($"{file}:{File.GetLastWriteTimeUtc (file)}"); + } + var deliveryType = item.GetMetadata ("DeliveryType") ?? "InstallTime"; + if (!IsDeliveryTypeValid (item, deliveryType)) { + Log.LogCodedError ("XA1039", Properties.Resources.XA1039, item.ItemSpec, deliveryType); + } + sw.WriteLine (deliveryType); + sw.Flush (); + Files.CopyIfStreamChanged (sw.BaseStream, cacheFile); + } + } + + AssetPacks = assetPacks.Values.ToArray(); + + return !Log.HasLoggedErrors; + } + + bool IsDeliveryTypeValid (ITaskItem item, string deliveryType) + { + if (string.Compare (deliveryType, "installtime", StringComparison.OrdinalIgnoreCase) == 0 && + string.Compare (deliveryType, "ondemand", StringComparison.OrdinalIgnoreCase) == 0 && + string.Compare (deliveryType, "fastfollow", StringComparison.OrdinalIgnoreCase) == 0) { + return false; + } + return true; + } + + bool IsAssetPackNameValid (string assetPackName) + { + return validAssetPackName.IsMatch (assetPackName); + } + } +} \ No newline at end of file diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/RemoveUnknownFiles.cs b/src/Xamarin.Android.Build.Tasks/Tasks/RemoveUnknownFiles.cs index c6a42050a3c..28a2253f640 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/RemoveUnknownFiles.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/RemoveUnknownFiles.cs @@ -19,10 +19,12 @@ public class RemoveUnknownFiles : AndroidTask public ITaskItem[] Files { get; set; } [Required] - public string Directory { get; set; } + public string[] Directories { get; set; } public bool RemoveDirectories { get; set; } + public string FileType { get; set; } = "AndroidResource"; + [Output] public ITaskItem[] RemovedFiles { get; set; } @@ -31,8 +33,6 @@ public class RemoveUnknownFiles : AndroidTask public override bool RunTask () { - var absDir = Path.GetFullPath (Directory); - HashSet knownFiles; List removedFiles = new List (); List removedDirectories = new List (); @@ -43,27 +43,40 @@ public override bool RunTask () else knownFiles = new HashSet (Files.Select (f => f.GetMetadata ("FullPath"))); - var files = System.IO.Directory.GetFiles (absDir, "*", SearchOption.AllDirectories); - foreach (string f in files) - if (!knownFiles.Contains (f)) { - Log.LogDebugMessage ("Deleting File {0}", f); - var item = new TaskItem (f.Replace (absDir, "res" + Path.DirectorySeparatorChar)); - removedFiles.Add (item); - Microsoft.Android.Build.Tasks.Files.SetWriteable (f); - File.Delete (f); + var root = "res"; + if (FileType == "AndroidAsset") + root = "assets"; + + foreach (var directory in Directories) { + var absDir = Path.GetFullPath (directory); + if (!System.IO.Directory.Exists (absDir)) { + Log.LogDebugMessage ("Skipping Directory {0}. It does not exists yet.", directory); + continue; } - - if (RemoveDirectories) { - var knownDirs = new HashSet (knownFiles.Select (d => Path.GetDirectoryName (d))); - var dirs = System.IO.Directory.GetDirectories (absDir, "*", SearchOption.AllDirectories); + var files = System.IO.Directory.GetFiles (absDir, "*", SearchOption.AllDirectories); + foreach (string f in files) { + if (!knownFiles.Contains (f)) { + Log.LogDebugMessage ("Deleting File {0}", f); + var item = new TaskItem (f.Replace (absDir, root + Path.DirectorySeparatorChar)); + removedFiles.Add (item); + Microsoft.Android.Build.Tasks.Files.SetWriteable (f); + File.Delete (f); + } + } + + if (RemoveDirectories) { + var knownDirs = new HashSet (knownFiles.Select (d => Path.GetDirectoryName (d))); + var dirs = System.IO.Directory.GetDirectories (absDir, "*", SearchOption.AllDirectories); - foreach (string d in dirs.OrderByDescending (s => s.Length)) - if (!knownDirs.Contains (d) && IsDirectoryEmpty (d)) { - Log.LogDebugMessage ("Deleting Directory {0}", d); - removedDirectories.Add (new TaskItem(d)); - Microsoft.Android.Build.Tasks.Files.SetDirectoryWriteable (d); - System.IO.Directory.Delete (d); + foreach (string d in dirs.OrderByDescending (s => s.Length)) { + if (!knownDirs.Contains (d) && IsDirectoryEmpty (d)) { + Log.LogDebugMessage ("Deleting Directory {0}", d); + removedDirectories.Add (new TaskItem(d)); + Microsoft.Android.Build.Tasks.Files.SetDirectoryWriteable (d); + System.IO.Directory.Delete (d); + } } + } } RemovedFiles = removedFiles.ToArray (); diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/AssetPackTests.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/AssetPackTests.cs new file mode 100644 index 00000000000..29bbea9403d --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/AssetPackTests.cs @@ -0,0 +1,232 @@ +using System; +using NUnit.Framework; +using System.IO; +using System.Text; +using Xamarin.ProjectTools; + +namespace Xamarin.Android.Build.Tests +{ + [Category ("Node-3")] + [Parallelizable (ParallelScope.Children)] + public class AssetPackTests : BaseTest + { + [Test] + [Category ("SmokeTests")] + public void BuildLibraryWithAssetPack ([Values (true, false)] bool isRelease) + { + var path = Path.Combine ("temp", TestName); + var lib = new XamarinAndroidLibraryProject { + IsRelease = isRelease, + OtherBuildItems = { + new AndroidItem.AndroidAsset ("Assets\\asset1.txt") { + TextContent = () => "Asset1", + Encoding = Encoding.ASCII, + MetadataValues="AssetPack=assetpack1", + }, + } + }; + using (var builder = CreateDllBuilder (Path.Combine (path, lib.ProjectName))) { + builder.ThrowOnBuildFailure = false; + Assert.IsFalse (builder.Build (lib), $"{lib.ProjectName} should fail."); + StringAssertEx.Contains ("error XA0138:", builder.LastBuildOutput, + "Build Output did not contain error XA0138'."); + } + } + + [Test] + [Category ("SmokeTests")] + public void BuildApplicationWithAssetPackThatHasInvalidName ([Values (true, false)] bool isRelease) + { + var path = Path.Combine ("temp", TestName); + var app = new XamarinAndroidApplicationProject { + IsRelease = isRelease, + OtherBuildItems = { + new AndroidItem.AndroidAsset ("Assets\\asset1.txt") { + TextContent = () => "Asset1", + Encoding = Encoding.ASCII, + MetadataValues="AssetPack=asset-pack1", + }, + } + }; + app.SetProperty ("AndroidPackageFormat", "aab"); + using (var builder = CreateApkBuilder (Path.Combine (path, app.ProjectName))) { + builder.ThrowOnBuildFailure = false; + Assert.IsFalse (builder.Build (app), $"{app.ProjectName} should fail."); + StringAssertEx.Contains ("error XA0140:", builder.LastBuildOutput, + "Build Output did not contain error XA0140'."); + } + } + + [Test] + [Category ("SmokeTests")] + public void BuildApplicationWithAssetPackOutsideProjectDirectory ([Values (true, false)] bool isRelease) + { + var path = Path.Combine ("temp", TestName); + var app = new XamarinAndroidApplicationProject { + ProjectName = "MyApp", + IsRelease = isRelease, + OtherBuildItems = { + new AndroidItem.AndroidAsset ("..\\Assets\\asset1.txt") { + TextContent = () => "Asset1", + Encoding = Encoding.ASCII, + MetadataValues="AssetPack=assetpack1;Link=Assets\\asset1.txt", + }, + new AndroidItem.AndroidAsset ("..\\Assets\\asset2.txt") { + TextContent = () => "Asset2", + Encoding = Encoding.ASCII, + MetadataValues="AssetPack=assetpack1;Link=Assets\\asset2.txt", + }, + new AndroidItem.AndroidAsset ("..\\Assets\\SubDirectory\\asset3.txt") { + TextContent = () => "Asset2", + Encoding = Encoding.ASCII, + MetadataValues="AssetPack=assetpack1;Link=Assets\\SubDirectory\\asset3.txt", + }, + } + }; + app.SetProperty ("AndroidPackageFormat", "aab"); + using (var appBuilder = CreateApkBuilder (Path.Combine (path, app.ProjectName))) { + Assert.IsTrue (appBuilder.Build (app), $"{app.ProjectName} should succeed"); + // Check the final aab has the required feature files in it. + var aab = Path.Combine (Root, appBuilder.ProjectDirectory, + app.OutputPath, $"{app.PackageName}.aab"); + using (var zip = ZipHelper.OpenZip (aab)) { + Assert.IsFalse (zip.ContainsEntry ("base/assets/asset1.txt"), "aab should not contain base/assets/asset1.txt"); + Assert.IsFalse (zip.ContainsEntry ("base/assets/asset2.txt"), "aab should not contain base/assets/asset2.txt"); + Assert.IsFalse (zip.ContainsEntry ("base/assets/SubDirectory/asset3.txt"), "aab should not contain base/assets/SubDirectory/asset3.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack1/assets/asset1.txt"), "aab should contain assetpack1/assets/asset1.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack1/assets/asset2.txt"), "aab should contain assetpack1/assets/asset2.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack1/assets/SubDirectory/asset3.txt"), "aab should contain assetpack1/assets/SubDirectory/asset3.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack1/assets.pb"), "aab should contain assetpack1/assets.pb"); + Assert.IsFalse (zip.ContainsEntry ("assetpack1/resources.pb"), "aab should not contain assetpack1/resources.pb"); + } + } + } + + [Test] + [Category ("SmokeTests")] + public void BuildApplicationWithAssetPackOverrides ([Values (true, false)] bool isRelease) + { + var path = Path.Combine ("temp", TestName); + var app = new XamarinAndroidApplicationProject { + ProjectName = "MyApp", + IsRelease = isRelease, + OtherBuildItems = { + new AndroidItem.AndroidAsset ("Assets\\asset1.txt") { + TextContent = () => "Asset1", + Encoding = Encoding.ASCII, + MetadataValues="AssetPack=assetpack1", + }, + new AndroidItem.AndroidAsset ("Assets\\asset2.txt") { + TextContent = () => "Asset2", + Encoding = Encoding.ASCII, + MetadataValues="AssetPack=base", + }, + } + }; + app.SetProperty ("AndroidPackageFormat", "aab"); + using (var appBuilder = CreateApkBuilder (Path.Combine (path, app.ProjectName))) { + Assert.IsTrue (appBuilder.Build (app), $"{app.ProjectName} should succeed"); + // Check the final aab has the required feature files in it. + var aab = Path.Combine (Root, appBuilder.ProjectDirectory, + app.OutputPath, $"{app.PackageName}.aab"); + using (var zip = ZipHelper.OpenZip (aab)) { + Assert.IsFalse (zip.ContainsEntry ("base/assets/asset1.txt"), "aab should not contain base/assets/asset1.txt"); + Assert.IsTrue (zip.ContainsEntry ("base/assets/asset2.txt"), "aab should contain base/assets/asset2.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack1/assets/asset1.txt"), "aab should contain assetpack1/assets/asset1.txt"); + Assert.IsFalse (zip.ContainsEntry ("assetpack1/assets/asset2.txt"), "aab should not contain assetpack1/assets/asset2.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack1/assets.pb"), "aab should contain assetpack1/assets.pb"); + Assert.IsFalse (zip.ContainsEntry ("assetpack1/resources.pb"), "aab should not contain assetpack1/resources.pb"); + } + } + } + + [Test] + [Category ("SmokeTests")] + public void BuildApplicationWithAssetPack ([Values (true, false)] bool isRelease) { + var path = Path.Combine ("temp", TestName); + var asset3 = new AndroidItem.AndroidAsset ("Assets\\asset3.txt") { + TextContent = () => "Asset3", + Encoding = Encoding.ASCII, + MetadataValues="AssetPack=assetpack1", + }; + var app = new XamarinAndroidApplicationProject { + ProjectName = "MyApp", + IsRelease = isRelease, + OtherBuildItems = { + new AndroidItem.AndroidAsset ("Assets\\asset1.txt") { + TextContent = () => "Asset1", + Encoding = Encoding.ASCII, + }, + new AndroidItem.AndroidAsset ("Assets\\asset2.txt") { + TextContent = () => "Asset2", + Encoding = Encoding.ASCII, + MetadataValues="AssetPack=assetpack1;DeliveryType=InstallTime", + }, + asset3, + new AndroidItem.AndroidAsset ("Assets\\asset4.txt") { + TextContent = () => "Asset4", + Encoding = Encoding.ASCII, + MetadataValues="AssetPack=assetpack2;DeliveryType=OnDemand", + }, + new AndroidItem.AndroidAsset ("Assets\\asset5.txt") { + TextContent = () => "Asset5", + Encoding = Encoding.ASCII, + MetadataValues="AssetPack=assetpack3;DeliveryType=FastFollow", + }, + } + }; + app.SetProperty ("AndroidPackageFormat", "aab"); + using (var appBuilder = CreateApkBuilder (Path.Combine (path, app.ProjectName))) { + Assert.IsTrue (appBuilder.Build (app), $"{app.ProjectName} should succeed"); + // Check the final aab has the required feature files in it. + var aab = Path.Combine (Root, appBuilder.ProjectDirectory, + app.OutputPath, $"{app.PackageName}.aab"); + var asset3File = Path.Combine (Root, path, app.ProjectName, + app.IntermediateOutputPath, "assetpacks", "assetpack1", "assets", "asset3.txt"); + using (var zip = ZipHelper.OpenZip (aab)) { + Assert.IsTrue (zip.ContainsEntry ("base/assets/asset1.txt"), "aab should contain base/assets/asset1.txt"); + Assert.IsFalse (zip.ContainsEntry ("base/assets/asset2.txt"), "aab should not contain base/assets/asset2.txt"); + Assert.IsFalse (zip.ContainsEntry ("base/assets/asset3.txt"), "aab should not contain base/assets/asset3.txt"); + Assert.IsFalse (zip.ContainsEntry ("base/assets/asset4.txt"), "aab should not contain base/assets/asset4.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack1/assets/asset2.txt"), "aab should contain assetpack1/assets/asset2.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack1/assets/asset3.txt"), "aab should contain assetpack1/assets/asset3.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack2/assets/asset4.txt"), "aab should contain assetpack2/assets/asset4.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack3/assets/asset5.txt"), "aab should contain assetpack3/assets/asset5.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack1/assets.pb"), "aab should contain assetpack1/assets.pb"); + Assert.IsFalse (zip.ContainsEntry ("assetpack1/resources.pb"), "aab should not contain assetpack1/resources.pb"); + } + Assert.IsTrue (appBuilder.Build (app, doNotCleanupOnUpdate: true, saveProject: false), $"{app.ProjectName} should succeed"); + appBuilder.Output.AssertTargetIsSkipped ("_CreateAssetPackManifests"); + appBuilder.Output.AssertTargetIsSkipped ("_BuildAssetPacks"); + appBuilder.Output.AssertTargetIsSkipped ("_GenerateAndroidAssetsDir"); + FileAssert.Exists (asset3File, $"file {asset3File} should exist."); + asset3.TextContent = () => "Asset3 Updated"; + asset3.Timestamp = DateTime.UtcNow.AddSeconds(1); + Assert.IsTrue (appBuilder.Build (app, doNotCleanupOnUpdate: true, saveProject: false), $"{app.ProjectName} should succeed"); + appBuilder.Output.AssertTargetIsNotSkipped ("_CreateAssetPackManifests"); + appBuilder.Output.AssertTargetIsNotSkipped ("_BuildAssetPacks"); + appBuilder.Output.AssertTargetIsNotSkipped ("_GenerateAndroidAssetsDir"); + FileAssert.Exists (asset3File, $"file {asset3File} should exist."); + Assert.AreEqual (asset3.TextContent (), File.ReadAllText (asset3File), $"Contents of {asset3File} should have been updated."); + app.OtherBuildItems.Remove (asset3); + Assert.IsTrue (appBuilder.Build (app, doNotCleanupOnUpdate: true), $"{app.ProjectName} should succeed"); + FileAssert.DoesNotExist (asset3File, $"file {asset3File} should not exist."); + using (var zip = ZipHelper.OpenZip (aab)) { + Assert.IsTrue (zip.ContainsEntry ("base/assets/asset1.txt"), "aab should contain base/assets/asset1.txt"); + Assert.IsFalse (zip.ContainsEntry ("base/assets/asset2.txt"), "aab should not contain base/assets/asset2.txt"); + Assert.IsFalse (zip.ContainsEntry ("base/assets/asset3.txt"), "aab should not contain base/assets/asset3.txt"); + Assert.IsFalse (zip.ContainsEntry ("base/assets/asset4.txt"), "aab should not contain base/assets/asset4.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack1/assets/asset2.txt"), "aab should contain assetpack1/assets/asset2.txt"); + Assert.IsFalse (zip.ContainsEntry ("assetpack1/assets/asset3.txt"), "aab should not contain assetpack1/assets/asset3.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack2/assets/asset4.txt"), "aab should contain assetpack2/assets/asset4.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack3/assets/asset5.txt"), "aab should contain assetpack3/assets/asset5.txt"); + Assert.IsTrue (zip.ContainsEntry ("assetpack1/assets.pb"), "aab should contain assetpack1/assets.pb"); + Assert.IsFalse (zip.ContainsEntry ("assetpack1/resources.pb"), "aab should not contain assetpack1/resources.pb"); + } + appBuilder.Output.AssertTargetIsNotSkipped ("_CreateAssetPackManifests"); + appBuilder.Output.AssertTargetIsNotSkipped ("_BuildAssetPacks"); + appBuilder.Output.AssertTargetIsNotSkipped ("_GenerateAndroidAssetsDir"); + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/ZipArchiveEx.cs b/src/Xamarin.Android.Build.Tasks/Utilities/ZipArchiveEx.cs index 90a8371aad7..54e510218ad 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/ZipArchiveEx.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/ZipArchiveEx.cs @@ -124,6 +124,22 @@ public void RemoveFile (string folder, string file) zip.DeleteEntry ((ulong)index); } + public bool MoveEntry (string from, string to) + { + if (!zip.ContainsEntry (from)) { + return false; + } + var entry = zip.ReadEntry (from); + using (var stream = new MemoryStream ()) { + entry.Extract (stream); + stream.Position = 0; + zip.AddEntry (to, stream); + zip.DeleteEntry (entry); + Flush (); + } + return true; + } + public void AddDirectory (string folder, string folderInArchive, CompressionMethod method = CompressionMethod.Default) { if (!string.IsNullOrEmpty (folder)) { diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets index c5b0be5e0e8..bde341316ae 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets @@ -98,6 +98,10 @@ PreserveNewest Xamarin.Android.Aapt2.targets + + PreserveNewest + Xamarin.Android.Assets.targets + PreserveNewest Xamarin.Android.Javac.targets diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index ebe4019ccab..9a5dc68772a 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -353,12 +353,6 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved. - - - $(IntermediateOutputPath)assets\ - Assets - - + @@ -595,8 +590,8 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved. - $(IntermediateOutputPath)android\bin\$(_AndroidPackage).apk - <_BaseZipIntermediate>$(IntermediateOutputPath)android\bin\base.zip + $(IntermediateOutputPath)android\bin\$(_AndroidPackage).apk + <_BaseZipIntermediate Condition=" '$(_BaseZipIntermediate)' == '' ">$(IntermediateOutputPath)android\bin\base.zip <_AppBundleIntermediate>$(IntermediateOutputPath)android\bin\$(_AndroidPackage).aab <_ApkSetIntermediate>$(IntermediateOutputPath)android\bin\$(_AndroidPackage).apks <_UniversalApkSetIntermediate>$(IntermediateOutputPath)android\bin\$(_AndroidPackage)-Universal.apks @@ -872,27 +867,6 @@ because xbuild doesn't support framework reference assemblies. /> - - - - - - - - - - - - - - - - - - $(IntermediateOutputPath)res\ @@ -1097,7 +1071,7 @@ because xbuild doesn't support framework reference assemblies. <_AndroidResourceDest Include="@(_WearableApplicationDescriptionFile);@(_BundledWearApplicationApkResourceFile)" /> - + @@ -1801,15 +1775,6 @@ because xbuild doesn't support framework reference assemblies. LibraryProjectImportsDirectoryName="$(_LibraryProjectImportsDirectoryName)"> - - -