Skip to content

Commit

Permalink
Rework Design Time Build to use .aar files directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
dellis1972 committed Jan 14, 2025
1 parent 0475a60 commit 7541fbc
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void CopyExtraBuildFiles (string destinationRoot, Context context)
"*log",
"TestOutput-*.txt",
"Timing_*",
"*.runsettings",
};

void CopyExtraTestFiles (string destinationRoot, Context context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.


<Target Name="_InjectAaptDependencies">
<PropertyGroup>
<PropertyGroup Condition="'$(DesignTimeBuild)' != 'True' Or ('$(DesignTimeBuild)' == 'True' And '$(AndroidUseManagedDesignTimeResourceGenerator)' == 'False') ">
<_SetLatestTargetFrameworkVersionDependsOnTargets>
$(_SetLatestTargetFrameworkVersionDependsOnTargets);
_CreateAapt2VersionCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Copyright (C) 2016 Xamarin. All rights reserved.
per TargetFramework.
-->
<_DesignerIntermediateOutputPath Condition=" '$(_DesignerIntermediateOutputPath)' == '' And '$(_OuterIntermediateOutputPath)' != '' ">$(_OuterIntermediateOutputPath)</_DesignerIntermediateOutputPath>
<_DesignerIntermediateOutputPath Condition=" '$(_DesignerIntermediateOutputPath)' == '' And '$(_OuterIntermediateOutputPath)' != '' And '$(AndroidUseDesignerAssembly)' == 'True' And '$(DesignTimeBuild)' == 'true' ">$(_OuterIntermediateOutputPath)designtime\</_DesignerIntermediateOutputPath>
<_DesignerIntermediateOutputPath Condition=" '$(_DesignerIntermediateOutputPath)' == '' And '$(AndroidUseDesignerAssembly)' == 'True' And '$(DesignTimeBuild)' == 'true' ">$(IntermediateOutputPath)designtime\</_DesignerIntermediateOutputPath>
<_DesignerIntermediateOutputPath Condition=" '$(_DesignerIntermediateOutputPath)' == '' ">$(IntermediateOutputPath)</_DesignerIntermediateOutputPath>
<_GenerateResourceDesignerAssemblyOutput>$(_DesignerIntermediateOutputPath)$(_DesignerAssemblyName).dll</_GenerateResourceDesignerAssemblyOutput>
<_GenerateResourceDesignerClassFile Condition=" '$(Language)' == 'F#' ">$(_DesignerIntermediateOutputPath)_$(_DesignerAssemblyName).fs</_GenerateResourceDesignerClassFile>
Expand All @@ -56,15 +58,44 @@ Copyright (C) 2016 Xamarin. All rights reserved.
<Message Text="IntermediateOutputPath: $(IntermediateOutputPath)" />
</Target>

<Target Name="_GetProjectResourceDirectory" Returns="@(_ProjectReferenceResourceDirectory)">
<ItemGroup>
<_ProjectReferenceResourceDirectory Include="$(MSBuildProjectDirectory)\$(MonoAndroidResourcePrefix)"
StampFile="$(MSBuildProjectFile)"
Condition="!Exists('$(OutputPath)$(TargetName).aar')"
/>
</ItemGroup>
</Target>

<Target Name="_CollectProjectReferenceResources"
Condition="'$(AndroidUseDesignerAssembly)' == 'True' And '$(DesignTimeBuild)' == 'True' "
>
<MSBuild
Projects="@(ProjectReference)"
Targets="_GetProjectResourceDirectory"
SkipNonexistentTargets="true"
>
<Output TaskParameter="TargetOutputs" ItemName="LibraryResourceDirectories" />
</MSBuild>
</Target>

<Target Name="_CalculateDesignTimeAars" Condition=" '$(DesignTimeBuild)' == 'True' ">
<ItemGroup>
<!-- Only use the aar files if we have not extracted the data -->
<_DesignTimeAarFiles Include="@(AndroidAarLibrary)" Condition=" '@(LibraryResourceDirectories->Count())' == '0' " />
</ItemGroup>
</Target>

<Target Name="_GenerateRtxt"
Condition="'$(AndroidUseDesignerAssembly)' == 'True' And '$(DesignTimeBuild)' == 'True' "
DependsOnTargets="_CreatePropertiesCache;_ResolveSdks;_ResolveAndroidTooling;_GetJavaPlatformJar;_GenerateAndroidResourceDir;_SetupDesignerProperties"
DependsOnTargets="_CreatePropertiesCache;_ResolveSdks;_ResolveAndroidTooling;_GetJavaPlatformJar;_GenerateAndroidResourceDir;_SetupDesignerProperties;_CollectProjectReferenceResources;_CalculateDesignTimeAars"
Inputs="$(_AndroidResFlagFile);@(_AndroidResourceDest);@(LibraryResourceDirectories->'%(StampFile)')"
Outputs="$(_DesignerIntermediateOutputPath)R.txt"
>
<!-- Generate an R.txt file using the Managed Parser -->
<GenerateRtxt
AdditionalResourceDirectories="@(LibraryResourceDirectories)"
AarLibraries="@(_DesignTimeAarFiles)"
CaseMapFile="$(_GenerateResourceCaseMapFile)"
JavaPlatformJarPath="$(JavaPlatformJarPath)"
ResourceDirectory="$(MonoAndroidResDirIntermediate)"
Expand All @@ -75,13 +106,14 @@ Copyright (C) 2016 Xamarin. All rights reserved.
</Target>

<Target Name="_GenerateResourceCaseMap"
DependsOnTargets="_ComputeAndroidResourcePaths;_SetupDesignerProperties;_GetLibraryImports"
DependsOnTargets="_ComputeAndroidResourcePaths;_SetupDesignerProperties;_GetLibraryImports;_CollectProjectReferenceResources;_CalculateDesignTimeAars"
Inputs="@(_AndroidResourceDest);@(LibraryResourceDirectories->'%(StampFile)')"
Outputs="$(_GenerateResourceCaseMapFile)"
>
<!-- Generate a ResourceMap file for the project and its resources -->
<GenerateResourceCaseMap
AdditionalResourceDirectories="@(LibraryResourceDirectories)"
AarLibraries="@(_DesignTimeAarFiles)"
OutputFile="$(_GenerateResourceCaseMapFile)"
ProjectDir="$(ProjectDir)"
ResourceDirectory="$(MonoAndroidResDirIntermediate)"
Expand Down Expand Up @@ -159,6 +191,8 @@ Copyright (C) 2016 Xamarin. All rights reserved.
<PropertyGroup>
<_BuildResourceDesignerDependsOn>
_SetupDesignerProperties;
_ResolveAars;
_CalculateDesignTimeAars;
_GenerateResourceCaseMap;
_GenerateRtxt;
_GenerateResourceDesignerIntermediateClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ projects.
<_AarOutputPath>$(OutputPath)$(TargetName).aar</_AarOutputPath>
</PropertyGroup>

<Target Name="_ResolveAars">
<Target Name="_ResolveAars" AfterTargets="ResolveReferences">
<ItemGroup>
<_AarSearchDirectory Include="@(_ReferencePath->'%(RootDir)%(Directory)')" />
<_AarSearchDirectory Include="@(_ReferenceDependencyPaths->'%(RootDir)%(Directory)')" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ _ResolveAssemblies MSBuild target.
<_AdditionalProperties>
_ComputeFilesToPublishForRuntimeIdentifiers=true
;SelfContained=true
;DesignTimeBuild=$(DesignTimeBuild)
;AppendRuntimeIdentifierToOutputPath=true
;ResolveAssemblyReferencesFindRelatedSatellites=false
;SkipCompilerExecution=true
Expand All @@ -105,6 +106,7 @@ _ResolveAssemblies MSBuild target.
<_ProjectToBuild Include="$(MSBuildProjectFile)" AdditionalProperties="RuntimeIdentifier=%(_RIDs.Identity);$(_AdditionalProperties)" />
</ItemGroup>
<MSBuild
Condition=" '$(DesignTimeBuild)' != 'true' "
Projects="@(_ProjectToBuild)"
BuildInParallel="$(_AndroidBuildRuntimeIdentifiersInParallel)"
Targets="_ComputeFilesToPublishForRuntimeIdentifiers">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,16 @@ properties that determine build ordering.
<ResolveReferencesDependsOn>
$(CoreResolveReferencesDependsOn);
UpdateAndroidResources;
_BuildResourceDesigner;
UpdateAndroidInterfaceProxies;
_CheckForInvalidDesignerConfig;
</ResolveReferencesDependsOn>
<DesignTimeResolveAssemblyReferencesDependsOn>
$(DesignTimeResolveAssemblyReferencesDependsOn);
_BuildResourceDesigner;
</DesignTimeResolveAssemblyReferencesDependsOn>
<_UpdateAndroidResourcesDependsOn>
$(CoreResolveReferencesDependsOn);
_CreatePropertiesCache;
_CheckForDeletedResourceFile;
_ComputeAndroidResourcePaths;
_UpdateAndroidResgen;
_CreateAar;
_BuildResourceDesigner;
</_UpdateAndroidResourcesDependsOn>
<CompileDependsOn>
_SetupMSBuildAllProjects;
Expand All @@ -142,9 +136,9 @@ properties that determine build ordering.
_CollectGeneratedManagedBindingFiles;
_ClearGeneratedManagedBindings;
AddBindingsToCompile;
$(CompileDependsOn);
_BuildResourceDesigner;
_AddResourceDesignerFiles;
$(CompileDependsOn);
_CheckAndroidHttpClientHandlerType;
</CompileDependsOn>
<CoreCompileDependsOn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<Import Project="Microsoft.Android.Sdk.BundledVersions.targets" />
<Import Project="Microsoft.Android.Sdk.DefaultProperties.targets" />
<Import Project="$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Common.Debugging.props"
Condition="Exists('$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Common.Debugging.props')"/>
Condition="Exists('$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Common.Debugging.props') And '$(DesignTimeBuild)' != 'true'"/>
<Import Project="Microsoft.Android.Sdk.NativeAOT.targets" Condition=" '$(_AndroidNativeAot)' == 'true' " />

</Project>
37 changes: 37 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceCaseMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Android.Build.Tasks;
using Xamarin.Android.Tools;
using Xamarin.Tools.Zip;

namespace Xamarin.Android.Tasks
{
Expand All @@ -22,6 +24,8 @@ public class GenerateResourceCaseMap : AndroidTask

public ITaskItem[] AdditionalResourceDirectories { get; set; }

public string[] AarLibraries { get; set; }

[Required]
public ITaskItem OutputFile { get; set; }

Expand Down Expand Up @@ -64,6 +68,39 @@ public override bool RunTask ()
AddRename (tok [1].Replace ('/', Path.DirectorySeparatorChar), tok [0].Replace ('/', Path.DirectorySeparatorChar));
}
}
var resmap = ".net/__res_name_case_map.txt";
foreach (var aar in AarLibraries ?? Array.Empty<string>()) {
Log.LogDebugMessage ($"Processing Aar file {aar}");
if (!File.Exists (aar)) {
Log.LogDebugMessage ($"Skipping non-existent aar: {aar}");
continue;
}
using (var file = File.OpenRead (aar)) {
using var zip = ZipArchive.Open (file);
if (!zip.ContainsEntry (resmap)) {
Log.LogDebugMessage ($"Skipping non-existent file: {resmap}");
continue;
}
ZipEntry entry = zip.ReadEntry (resmap);
Log.LogDebugMessage ($"Found: {entry.FullName}");
var ms = MemoryStreamPool.Shared.Rent ();
try {
entry.Extract (ms);
ms.Position = 0;
using var reader = new StreamReader (ms);
string line;
// Read each line until the end of the file
while ((line = reader.ReadLine()) != null) {
if (string.IsNullOrEmpty (line))
continue;
string [] tok = line.Split (';');
AddRename (tok [1].Replace ('/', Path.DirectorySeparatorChar), tok [0].Replace ('/', Path.DirectorySeparatorChar));
}
} finally {
MemoryStreamPool.Shared.Return (ms);
}
}
}

if (MonoAndroidHelper.SaveMapFile (BuildEngine4, Path.GetFullPath (OutputFile.ItemSpec), resource_fixup)) {
Log.LogDebugMessage ($"Writing to: {OutputFile.ItemSpec}");
Expand Down
4 changes: 3 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/GenerateRtxt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class GenerateRtxt : AndroidTask
public string ResourceDirectory { get; set; }
public string[] AdditionalResourceDirectories { get; set; }

public string[] AarLibraries { get; set; }

public string JavaPlatformJarPath { get; set; }

public string ResourceFlagFile { get; set; }
Expand All @@ -31,7 +33,7 @@ public override bool RunTask ()

var javaPlatformDirectory = string.IsNullOrEmpty (JavaPlatformJarPath) ? "" : Path.GetDirectoryName (JavaPlatformJarPath);
var parser = new FileResourceParser () { Log = Log, JavaPlatformDirectory = javaPlatformDirectory, ResourceFlagFile = ResourceFlagFile};
var resources = parser.Parse (ResourceDirectory, AdditionalResourceDirectories, resource_fixup);
var resources = parser.Parse (ResourceDirectory, AdditionalResourceDirectories, AarLibraries, resource_fixup);

// only update if it changed.
writer.Write (RTxtFile, resources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ public void BuildAfterUpgradingNuget ()
}

[Test]
[Category ("SmokeTests")]
public void BuildInDesignTimeMode ([Values(false, true)] bool useManagedParser)
{
var proj = new XamarinAndroidApplicationProject () {
Expand All @@ -762,20 +763,20 @@ public void BuildInDesignTimeMode ([Values(false, true)] bool useManagedParser)
builder.Target = "UpdateAndroidResources";
builder.Build (proj, parameters: new string[] { "DesignTimeBuild=true" });
Assert.IsFalse (builder.Output.IsTargetSkipped ("_CreatePropertiesCache"), "target \"_CreatePropertiesCache\" should have been run.");
Assert.IsFalse (builder.Output.IsTargetSkipped ("_ResolveLibraryProjectImports"), "target \"_ResolveLibraryProjectImports\' should have been run.");
Assert.IsFalse (builder.Output.IsTargetSkipped ("_GenerateRtxt"), "target \"_GenerateRtxt\' should have been run.");
var intermediate = Path.Combine (Root, builder.ProjectDirectory, proj.IntermediateOutputPath);
var librarycache = Path.Combine (intermediate, "designtime", "libraryprojectimports.cache");
Assert.IsTrue (File.Exists (librarycache), $"'{librarycache}' should exist.");
librarycache = Path.Combine (intermediate, "libraryprojectimports.cache");
Assert.IsFalse (File.Exists (librarycache), $"'{librarycache}' should not exist.");
var rTxtFile = Path.Combine (intermediate, "designtime", "R.txt");
Assert.IsTrue (File.Exists (rTxtFile), $"'{rTxtFile}' should exist.");
rTxtFile = Path.Combine (intermediate, "R.txt");
Assert.IsFalse (File.Exists (rTxtFile), $"'{rTxtFile}' should not exist.");
builder.Build (proj, parameters: new string[] { "DesignTimeBuild=true" });
Assert.IsFalse (builder.Output.IsTargetSkipped ("_CreatePropertiesCache"), "target \"_CreatePropertiesCache\" should have been run.");
Assert.IsTrue (builder.Output.IsTargetSkipped ("_ResolveLibraryProjectImports"), "target \"_ResolveLibraryProjectImports\' should have been skipped.");
Assert.IsTrue (builder.Output.IsTargetSkipped ("_GenerateRtxt"), "target \"_GenerateRtxt\' should have been skipped.");
Assert.IsTrue (builder.Clean (proj), "Clean Should have succeeded");
builder.Target = "_CleanDesignTimeIntermediateDir";
Assert.IsTrue (builder.Build (proj), "_CleanDesignTimeIntermediateDir should have succeeded");
librarycache = Path.Combine (intermediate, "designtime", "libraryprojectimports.cache");
Assert.IsFalse (File.Exists (librarycache), $"'{librarycache}' should not exist.");
rTxtFile = Path.Combine (intermediate, "designtime", "R.txt");
Assert.IsFalse (File.Exists (rTxtFile), $"'{rTxtFile}' should not exist.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1199,17 +1199,26 @@ public void DesignTimeBuild ()
{
var proj = new XamarinAndroidApplicationProject ();
using (var b = CreateApkBuilder (Path.Combine ("temp", $"{nameof (IncrementalBuildTest)}{TestName}"))) {
b.BuildLogFile = "dtb1.log";
Assert.IsTrue (b.DesignTimeBuild (proj), "first dtb should have succeeded.");
var target = "_ResolveLibraryProjectImports";
var target = "_GenerateResourceDesignerAssembly";
Assert.IsFalse (b.Output.IsTargetSkipped (target), $"`{target}` should not have been skipped.");
var importsTarget = "_ResolveLibraryProjectImports";
Assert.IsTrue (b.Output.IsTargetSkipped (importsTarget, defaultIfNotUsed: true), $"`{importsTarget}` should have been skipped.");
// DesignTimeBuild=true lowercased
var parameters = new [] { "DesignTimeBuild=true" };
b.BuildLogFile = "compile.log";
Assert.IsTrue (b.RunTarget (proj, "Compile", doNotCleanupOnUpdate: true, parameters: parameters), "second dtb should have succeeded.");
Assert.IsTrue (b.Output.IsTargetSkipped (target), $"`{target}` should have been skipped.");
Assert.IsTrue (b.Output.IsTargetSkipped (target, defaultIfNotUsed: true), $"`{target}` should have been skipped.");
Assert.IsTrue (b.Output.IsTargetSkipped (importsTarget, defaultIfNotUsed: true), $"`{importsTarget}` should have been skipped.");
b.BuildLogFile = "updategeneratedfiles.log";
Assert.IsTrue (b.RunTarget (proj, "UpdateGeneratedFiles", doNotCleanupOnUpdate: true, parameters: parameters), "UpdateGeneratedFiles should have succeeded.");
Assert.IsTrue (b.Output.IsTargetSkipped (target), $"`{target}` should have been skipped.");
Assert.IsTrue (b.Output.IsTargetSkipped (target, defaultIfNotUsed: true), $"`{target}` should have been skipped.");
Assert.IsTrue (b.Output.IsTargetSkipped (importsTarget, defaultIfNotUsed: true), $"`{importsTarget}` should have been skipped.");
b.BuildLogFile = "build.log";
Assert.IsTrue (b.Build (proj, doNotCleanupOnUpdate: true, saveProject: false), "full build should have succeeded.");
Assert.IsFalse (b.Output.IsTargetSkipped (target), $"`{target}` should not have been skipped.");
Assert.IsFalse (b.Output.IsTargetSkipped (importsTarget), $"`{importsTarget}` should not have been skipped.");
}
}

Expand All @@ -1234,7 +1243,7 @@ public void DesignTimeBuildSignAndroidPackage ()
Assert.IsTrue (builder.RunTarget (proj, "SignAndroidPackage", parameters: parameters), $"{proj.ProjectName} should succeed");
builder.Output.AssertTargetIsNotSkipped ("_GenerateResourceCaseMap", occurrence: 2);
builder.Output.AssertTargetIsSkipped ("_GenerateRtxt", occurrence: 1);
builder.Output.AssertTargetIsSkipped ("_GenerateResourceDesignerIntermediateClass", occurrence: 1);
builder.Output.AssertTargetIsNotSkipped ("_GenerateResourceDesignerIntermediateClass");
builder.Output.AssertTargetIsSkipped ("_GenerateResourceDesignerAssembly", occurrence: 2);
builder.BuildLogFile = "build2.log";
Assert.IsTrue (builder.RunTarget (proj, "SignAndroidPackage", parameters: parameters), $"{proj.ProjectName} should succeed 2");
Expand Down
Loading

0 comments on commit 7541fbc

Please sign in to comment.