Skip to content

[Xamarin.Android.Build.Tasks] Add Support for bundletool meta-data #6616

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

Merged
merged 1 commit into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Documentation/guides/building-apps/build-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ Used to provide an AOT profile, for use with profile-guided AOT.
It can be also used from Visual Studio by setting the `AndroidAotProfile`
build action to a file containing an AOT profile.

## AndroidAppBundleMetaDataFile

Specifies a file that will be included as metadata in the Android App Bundle.
The format of the flag value is `<bundle-path>:<physical-file>` where
`bundle-path` denotes the file location inside the App Bundle's metadata
directory, and `physical-file` is an existing file containing the raw data
to be stored.

```xml
<ItemGroup>
<AndroidAppBundleMetaDataFile
Include="com.android.tools.build.obfuscation/proguard.map:$(OutputPath)mapping.txt"
/>
</ItemGroup>
```

See [bundletool](https://developer.android.com/studio/build/building-cmdline#build_your_app_bundle_using_bundletool) documentation for more details.

Added in Xamarin.Android 12.3.

## AndroidBoundLayout

Indicates that the layout file is to have code-behind generated for it in case when
Expand Down
35 changes: 34 additions & 1 deletion Documentation/guides/building-apps/build-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,28 @@ should be created instead of having support for all ABIs in a single `.apk`.
See also the [Building ABI-Specific APKs](~/android/deploy-test/building-apps/abi-specific-apks.md)
guide.

## AndroidCreateProguardMappingFile

A boolean property that controls if a proguard mapping file is
generated as part of the build process.

Adding the following to your csproj will cause the file to be
generated. This uses the [`AndroidProguardMappingFile`](#androidproguardmappingfile) property
to control the location of the final mapping file.

```
<AndroidCreateProguardMappingFile>True</AndroidCreateProguardMappingFile>
```

Note that if you are using `.aab` files the mapping file with be
automatically included in your package. So there is no need to upload
it to the Google Play Store manually. If you are using `.apk` files
you will need to manually upload the [`AndroidProguardMappingFile`](#androidproguardmappingfile).

The default value is `True` when using `AndroidLinkTool=r8`.

Added in Xamarin.Android 12.3.

## AndroidDebugKeyAlgorithm

Specifies the default
Expand Down Expand Up @@ -987,7 +1009,18 @@ mean the `mapping.txt` file will be produced in the `$(OutputPath)`
folder. This file can then be used when uploading packages to the
Google Play Store.

The default value is `$(OutputPath)mapping.txt`.
By default this file is produced automatically when using `AndroidLinkTool=r8`
and will generate the following file `$(OutputPath)mapping.txt`.

If you do not want to generate this mapping file you can use the
[`AndroidCreateProguardMappingFile`](#androidcreateproguardmappingfile) property to stop creating it .
Add the following in your project

```
<AndroidCreateProguardMappingFile>False</AndroidCreateProguardMappingFile>
```

or use `-p:AndroidCreateProguardMappingFile=False` on the command line.

This property was added in Xamarin.Android 11.2.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This file contains the implementation for 'dotnet publish'.
<_AllPackageFormats Include="$(AndroidPackageFormat);$(AndroidPackageFormats)" />
<_AndroidPackageFormats Include="@(_AllPackageFormats->Distinct())" />
<_AndroidFilesToPublish Include="$(OutputPath)*.%(_AndroidPackageFormats.Identity)" />
<_AndroidFilesToPublish Include="$(AndroidProguardMappingFile)" Condition="Exists ('$(AndroidProguardMappingFile)')" />
<ResolvedFileToPublish Remove="@(ResolvedFileToPublish)" />
<ResolvedFileToPublish Include="@(_AndroidFilesToPublish)" RelativePath="%(FileName)%(Extension)" />
</ItemGroup>
Expand Down
9 changes: 7 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/BuildAppBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Xamarin.Android.Tasks
{
/// <summary>
/// Invokes `bundletool` to create an Android App Bundle (.aab file)
///
///
/// Usage: bundletool build-bundle --modules=base.zip --output=foo.aab --config=BundleConfig.json
/// </summary>
public class BuildAppBundle : BundleTool
Expand Down Expand Up @@ -63,9 +63,11 @@ public class BuildAppBundle : BundleTool
public string BaseZip { get; set; }

public string CustomBuildConfigFile { get; set; }

public string [] Modules { get; set; }

public ITaskItem [] MetaDataFiles { get; set; }

[Required]
public string Output { get; set; }

Expand Down Expand Up @@ -130,6 +132,9 @@ internal override CommandLineBuilder GetCommandLineBuilder ()
cmd.AppendSwitchIfNotNull ("--modules ", string.Join (",", modules));
cmd.AppendSwitchIfNotNull ("--output ", Output);
cmd.AppendSwitchIfNotNull ("--config ", temp);
foreach (var file in MetaDataFiles ?? Array.Empty<ITaskItem> ()) {
cmd.AppendSwitch ($"--metadata-file={file.ItemSpec}");
}
return cmd;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/Proguard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected override string GenerateCommandLineCommands ()
if (!string.IsNullOrEmpty (ProguardMappingFileOutput)) {
xamcfg.WriteLine ("-keepattributes SourceFile");
xamcfg.WriteLine ("-keepattributes LineNumberTable");
xamcfg.WriteLine ($"-printmapping {Path.GetFullPath (ProguardMappingFileOutput)}");
xamcfg.WriteLine ($"-printmapping \"{Path.GetFullPath (ProguardMappingFileOutput)}\"");
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/R8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected override CommandLineBuilder GetCommandLineBuilder ()
if (!string.IsNullOrEmpty (ProguardMappingFileOutput)) {
xamcfg.WriteLine ("-keepattributes SourceFile");
xamcfg.WriteLine ("-keepattributes LineNumberTable");
xamcfg.WriteLine ($"-printmapping {Path.GetFullPath (ProguardMappingFileOutput)}");
xamcfg.WriteLine ($"-printmapping \"{Path.GetFullPath (ProguardMappingFileOutput)}\"");
}
}
}
Expand All @@ -125,7 +125,7 @@ protected override CommandLineBuilder GetCommandLineBuilder ()
if (!string.IsNullOrEmpty (ProguardMappingFileOutput)) {
lines.Add ("-keepattributes SourceFile");
lines.Add ("-keepattributes LineNumberTable");
lines.Add ($"-printmapping {Path.GetFullPath (ProguardMappingFileOutput)}");
lines.Add ($"-printmapping \"{Path.GetFullPath (ProguardMappingFileOutput)}\"");
}
File.WriteAllLines (temp, lines);
tempFiles.Add (temp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public void CheckProguardMappingFileExists ()
};
proj.SetProperty (proj.ReleaseProperties, KnownProperties.AndroidDexTool, "d8");
proj.SetProperty (proj.ReleaseProperties, KnownProperties.AndroidLinkTool, "r8");
// Projects must provide a $(AndroidProguardMappingFile) value to opt in
proj.SetProperty (proj.ReleaseProperties, "AndroidProguardMappingFile", @"$(OutputPath)\mapping.txt");
// Projects must set $(AndroidCreateProguardMappingFile) to true to opt in
proj.SetProperty (proj.ReleaseProperties, "AndroidCreateProguardMappingFile", true);

using (var b = CreateApkBuilder ()) {
string mappingFile = Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, "mapping.txt");
Expand Down
15 changes: 15 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<AndroidEnableDesugar Condition=" '$(AndroidEnableDesugar)' == '' And ('$(AndroidDexTool)' == 'd8' Or '$(AndroidLinkTool)' == 'r8') ">True</AndroidEnableDesugar>
<AndroidEnableDesugar Condition=" '$(AndroidEnableDesugar)' == '' ">False</AndroidEnableDesugar>
<AndroidR8IgnoreWarnings Condition=" '$(AndroidR8IgnoreWarnings)' == '' ">True</AndroidR8IgnoreWarnings>
<AndroidCreateProguardMappingFile Condition="'$(AndroidCreateProguardMappingFile)' == '' And '$(AndroidLinkTool)' == 'r8'">True</AndroidCreateProguardMappingFile>

<!-- Figure out which is the main packaging format we want-->
<AndroidPackageFormat Condition=" $(AndroidPackageFormats.Contains('aab')) And '$(AndroidPackageFormat)' == '' ">aab</AndroidPackageFormat>
Expand Down Expand Up @@ -1807,6 +1808,7 @@ because xbuild doesn't support framework reference assemblies.
_CreateApplicationSharedLibraries;
_GetMonoPlatformJarPath;
_GetLibraryImports;
_SetProguardMappingFileProperty;
_CalculateProguardConfigurationFiles;
</_CompileToDalvikDependsOnTargets>
<_CompileToDalvikInputs>
Expand All @@ -1826,6 +1828,12 @@ because xbuild doesn't support framework reference assemblies.
</_CompileDexDependsOn>
</PropertyGroup>

<Target Name="_SetProguardMappingFileProperty">
<PropertyGroup>
<AndroidProguardMappingFile Condition=" '$(AndroidLinkTool)' == 'r8' And '$(AndroidCreateProguardMappingFile)' == 'True' ">$(OutputPath)mapping.txt</AndroidProguardMappingFile>
</PropertyGroup>
</Target>

<Target Name="_CalculateProguardConfigurationFiles">
<!-- Support the $(ProguardConfigFiles) property to keep backwards compatibility -->
<ItemGroup Condition=" '$(ProguardConfigFiles)' != '' ">
Expand Down Expand Up @@ -1967,6 +1975,12 @@ because xbuild doesn't support framework reference assemblies.
$(ApkFileIntermediate)
</_BuildApkEmbedOutputs>
</PropertyGroup>
<ItemGroup>
<AndroidAppBundleMetaDataFile
Condition=" Exists('$(AndroidProguardMappingFile)') "
Include="com.android.tools.build.obfuscation/proguard.map:$(AndroidProguardMappingFile)"
/>
</ItemGroup>
</Target>

<PropertyGroup>
Expand Down Expand Up @@ -2064,6 +2078,7 @@ because xbuild doesn't support framework reference assemblies.
Output="$(_AppBundleIntermediate)"
UncompressedFileExtensions="$(AndroidStoreUncompressedFileExtensions)"
CustomBuildConfigFile="$(AndroidBundleConfigurationFile)"
MetaDataFiles="@(AndroidAppBundleMetaDataFile)"
/>
</Target>

Expand Down
Loading