Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Add Support for bundletool meta-data
Browse files Browse the repository at this point in the history
Context dotnet#5378

Add a new property `AndroidCreateProguardMappingFile` to control the
creation of the proguard mapping file.
This will default to `False` because it does has some impact on the final
apk sizes.

Also Provide a default value for `AndroidProguardMappingFile` when using
`AndroidLinkTool=r8` and `AndroidCreateProguardMappingFile=True`.

Add support for providing meta-data files to `bundletool` when building
the app bundle. This allows developers to provide additional meta-data
such as the proguard mapping file. The new ItemGroup is called
`AndroidAppBundleMetaDataFile`.
  • Loading branch information
dellis1972 committed Jan 17, 2022
1 parent c227042 commit e148a97
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 17 deletions.
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
36 changes: 35 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 `False`.

Added in Xamarin.Android 12.3.

## AndroidDebugKeyAlgorithm

Specifies the default
Expand Down Expand Up @@ -987,7 +1009,19 @@ 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 not produced.

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

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

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

This will then generate the following file `$(OutputPath)mapping.txt`.

This property was added in Xamarin.Android 11.2.

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
14 changes: 14 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,7 @@ because xbuild doesn't support framework reference assemblies.
_CreateApplicationSharedLibraries;
_GetMonoPlatformJarPath;
_GetLibraryImports;
_SetProguardMappingFileProperty;
_CalculateProguardConfigurationFiles;
</_CompileToDalvikDependsOnTargets>
<_CompileToDalvikInputs>
Expand All @@ -1826,6 +1827,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 +1974,12 @@ because xbuild doesn't support framework reference assemblies.
$(ApkFileIntermediate)
</_BuildApkEmbedOutputs>
</PropertyGroup>
<ItemGroup>
<AndroidAppBundleMetaDataFile
Condition=" '$(AndroidProguardMappingFile)' != '' And Exists ('$(AndroidProguardMappingFile)') "
Include="com.android.tools.build.obfuscation/proguard.map:$(AndroidProguardMappingFile)"
/>
</ItemGroup>
</Target>

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"Size": 2896
},
"assemblies/assemblies.blob": {
"Size": 2026794
"Size": 2027538
},
"assemblies/assemblies.manifest": {
"Size": 1574
},
"classes.dex": {
"Size": 2940564
"Size": 2940568
},
"lib/armeabi-v7a/libmono-btls-shared.so": {
"Size": 1112688
Expand All @@ -20,16 +20,16 @@
"Size": 736396
},
"lib/armeabi-v7a/libmonodroid.so": {
"Size": 232320
"Size": 234328
},
"lib/armeabi-v7a/libmonosgen-2.0.so": {
"Size": 4456332
"Size": 4456576
},
"lib/armeabi-v7a/libxa-internal-api.so": {
"Size": 48844
},
"lib/armeabi-v7a/libxamarin-app.so": {
"Size": 46832
"Size": 46844
},
"lib/x86/libmono-btls-shared.so": {
"Size": 1459584
Expand All @@ -38,16 +38,16 @@
"Size": 803352
},
"lib/x86/libmonodroid.so": {
"Size": 303316
"Size": 305648
},
"lib/x86/libmonosgen-2.0.so": {
"Size": 4212220
"Size": 4212336
},
"lib/x86/libxa-internal-api.so": {
"Size": 61112
},
"lib/x86/libxamarin-app.so": {
"Size": 45500
"Size": 45632
},
"META-INF/android.arch.core_runtime.version": {
"Size": 6
Expand Down Expand Up @@ -1610,5 +1610,5 @@
"Size": 320016
}
},
"PackageSize": 9500681
"PackageSize": 9504777
}

0 comments on commit e148a97

Please sign in to comment.