Skip to content

Commit

Permalink
[One .NET] select defaults for App Bundles
Browse files Browse the repository at this point in the history
Fixes dotnet#6059

[WIP]
  • Loading branch information
dellis1972 committed Jul 22, 2021
1 parent 1e5bfa3 commit 9b7a554
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<!-- mono-symbolicate is not supported -->
<MonoSymbolArchive>false</MonoSymbolArchive>
<!--
Disable @(Content) from referenced projects
Disable @(Content) from referenced projects
See: https://github.com/dotnet/sdk/blob/955c0fc7b06e2fa34bacd076ed39f61e4fb61716/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets#L16
-->
<_GetChildProjectCopyToPublishDirectoryItems>false</_GetChildProjectCopyToPublishDirectoryItems>
Expand All @@ -44,6 +44,8 @@
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<EmbedAssembliesIntoApk Condition=" '$(EmbedAssembliesIntoApk)' == '' ">true</EmbedAssembliesIntoApk>
<AndroidManagedSymbols Condition=" '$(AndroidManagedSymbols)' == '' ">true</AndroidManagedSymbols>
<AndroidPackageFormat Condition=" '$(AndroidPackageFormat)' == '' " >aab</AndroidPackageFormat>
<AndroidPackageFormats Condition=" '$(AndroidPackageFormats)' == '' " >aab;apk</AndroidPackageFormats>
</PropertyGroup>

<!-- Application project settings -->
Expand Down
12 changes: 9 additions & 3 deletions src/Xamarin.Android.Build.Tasks/Tasks/BuildApkSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class BuildApkSet : BundleToolAdbTask

public string ExtraArgs { get; set; }

public bool GenerateUniversalApkSet { get; set; } = false;

public override bool RunTask ()
{
//NOTE: bundletool will not overwrite
Expand Down Expand Up @@ -69,11 +71,15 @@ internal override CommandLineBuilder GetCommandLineBuilder ()
var aapt2 = string.IsNullOrEmpty (Aapt2ToolExe) ? Aapt2ToolName : Aapt2ToolExe;
var cmd = base.GetCommandLineBuilder ();
cmd.AppendSwitch ("build-apks");
cmd.AppendSwitch ("--connected-device");
if (GenerateUniversalApkSet) {
cmd.AppendSwitchIfNotNull ("--mode ", "universal");
} else {
cmd.AppendSwitch ("--connected-device");
cmd.AppendSwitchIfNotNull ("--mode ", "default");
AppendAdbOptions (cmd);
}
cmd.AppendSwitchIfNotNull ("--bundle ", AppBundle);
cmd.AppendSwitchIfNotNull ("--output ", Output);
cmd.AppendSwitchIfNotNull ("--mode ", "default");
AppendAdbOptions (cmd);
cmd.AppendSwitchIfNotNull ("--aapt2 ", Path.Combine (Aapt2ToolPath, aapt2));
cmd.AppendSwitchIfNotNull ("--ks ", KeyStore);
cmd.AppendSwitchIfNotNull ("--ks-key-alias ", KeyAlias);
Expand Down
19 changes: 17 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/Unzip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,29 @@ public class Unzip : AndroidTask

public ITaskItem [] Sources { get; set; }
public ITaskItem [] DestinationDirectories { get; set; }
public ITaskItem [] Files { get; set; }

public override bool RunTask ()
{
foreach (var pair in Sources.Zip (DestinationDirectories, (s, d) => new { Source = s, Destination = d })) {
if (!Directory.Exists (pair.Destination.ItemSpec))
Directory.CreateDirectory (pair.Destination.ItemSpec);
using (var z = ZipArchive.Open (pair.Source.ItemSpec, FileMode.Open))
z.ExtractAll (pair.Destination.ItemSpec);
using (var z = ZipArchive.Open (pair.Source.ItemSpec, FileMode.Open)) {
if (Files == null || Files.Length == 0) {
z.ExtractAll (pair.Destination.ItemSpec);
} else {
foreach (var file in Files) {
ZipEntry entry = z.ReadEntry (file.ItemSpec);
if (entry == null) {
Log.LogDebugMessage ($"Skipping not existant file {file.ItemSpec}");
continue;
}
string destinationFileName = file.GetMetadata ("DestinationFileName");
Log.LogDebugMessage ($"Extracting {file.ItemSpec} to {destinationFileName ?? file.ItemSpec}");
entry.Extract (pair.Destination.ItemSpec, destinationFileName ?? file.ItemSpec);
}
}
}
}

return true;
Expand Down
36 changes: 36 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<UsingTask TaskName="Xamarin.Android.Tasks.PrepareAbiItems" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.WriteLockFile" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="GenerateCompressedAssembliesNativeSourceFiles" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.Unzip" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />

<!--
*******************************************
Expand Down Expand Up @@ -2361,11 +2362,44 @@ because xbuild doesn't support framework reference assemblies.
<Delete Files="%(ApkAbiFilesUnaligned.FullPath)" />
</Target>

<Target Name="_CreateUniversalApkFromBundle"
Condition=" '$(AndroidPackageFormat)' == 'aab' And $(AndroidPackageFormats.Contains('apk')) "
Inputs="$(_AppBundleIntermediate)"
Outputs="$(OutDir)$(_AndroidPackage)*-Signed.apk"
>
<Delete Files="$(_ApkSetIntermediate);$(OutDir)$(_AndroidPackage)*-Signed.apk" />
<BuildApkSet
ToolPath="$(JavaToolPath)"
JavaMaximumHeapSize="$(JavaMaximumHeapSize)"
JavaOptions="$(JavaOptions)"
JarPath="$(AndroidBundleToolJarPath)"
AdbToolPath="$(AdbToolPath)"
AdbTarget="$(AdbTarget)"
Aapt2ToolPath="$(Aapt2ToolPath)"
AppBundle="$(_AppBundleIntermediate)"
Output="$(_ApkSetIntermediate)"
KeyStore="$(_ApkKeyStore)"
KeyAlias="$(_ApkKeyAlias)"
KeyPass="$(_ApkKeyPass)"
StorePass="$(_ApkStorePass)"
ExtraArgs="$(AndroidBundleToolExtraArgs)"
GenerateUniversalApkSet="True"
/>
<!-- Extract the univeral.apk from the $(_ApkSetIntermediate) and then copy it to the output directory -->
<ItemGroup>
<_FilestoExtract Include="universal.apk" >
<DestinationFileName>$(_AndroidPackage)-Signed.apk</DestinationFileName>
</_FilestoExtract>
</ItemGroup>
<Unzip Sources="$(_ApkSetIntermediate)" DestinationDirectories="$(OutDir)" Files="@(_FilestoExtract)" />
</Target>

<PropertyGroup>
<SignAndroidPackageDependsOn Condition=" '$(BuildingInsideVisualStudio)' != 'True' ">
Build;
Package;
_Sign;
_CreateUniversalApkFromBundle;
</SignAndroidPackageDependsOn>
<!-- When inside an IDE, Build has just been run. This is a minimal list of targets for SignAndroidPackage. -->
<SignAndroidPackageDependsOn Condition=" '$(BuildingInsideVisualStudio)' == 'True' ">
Expand All @@ -2376,6 +2410,7 @@ because xbuild doesn't support framework reference assemblies.
CreateSatelliteAssemblies;
_CopyPackage;
_Sign;
_CreateUniversalApkFromBundle;
</SignAndroidPackageDependsOn>
</PropertyGroup>
<Target Name="SignAndroidPackage" DependsOnTargets="$(SignAndroidPackageDependsOn)">
Expand Down Expand Up @@ -2551,6 +2586,7 @@ because xbuild doesn't support framework reference assemblies.
KeyPass="$(_ApkKeyPass)"
StorePass="$(_ApkStorePass)"
ExtraArgs="$(AndroidBundleToolExtraArgs)"
Condition="!Exists('$(_ApkSetIntermediate)')"
/>
<InstallApkSet
ToolPath="$(JavaToolPath)"
Expand Down

0 comments on commit 9b7a554

Please sign in to comment.