Skip to content
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

Rework Design Time Build to use .aar files directly. #9423

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
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
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,37 @@ 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="Restore;_GetProjectResourceDirectory"
Comment on lines +73 to +75
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions here:

  1. Does this really need to run Restore?
  2. What happens if the @(ProjectReference) is a plain net9.0 project or something old like netstandard2.0 or PCL?

SkipNonexistentTargets="true"
>
<Output TaskParameter="TargetOutputs" ItemName="LibraryResourceDirectories" />
</MSBuild>
</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"
Inputs="$(_AndroidResFlagFile);@(_AndroidResourceDest);@(LibraryResourceDirectories->'%(StampFile)')"
Outputs="$(_DesignerIntermediateOutputPath)R.txt"
>
<!-- Generate an R.txt file using the Managed Parser -->
<GenerateRtxt
AdditionalResourceDirectories="@(LibraryResourceDirectories)"
AarLibraries="@(AndroidAarLibrary)"
CaseMapFile="$(_GenerateResourceCaseMapFile)"
JavaPlatformJarPath="$(JavaPlatformJarPath)"
ResourceDirectory="$(MonoAndroidResDirIntermediate)"
Expand All @@ -75,13 +99,18 @@ Copyright (C) 2016 Xamarin. All rights reserved.
</Target>

<Target Name="_GenerateResourceCaseMap"
DependsOnTargets="_ComputeAndroidResourcePaths;_SetupDesignerProperties;_GetLibraryImports"
DependsOnTargets="_ComputeAndroidResourcePaths;_SetupDesignerProperties;_GetLibraryImports;_CollectProjectReferenceResources"
Inputs="@(_AndroidResourceDest);@(LibraryResourceDirectories->'%(StampFile)')"
Outputs="$(_GenerateResourceCaseMapFile)"
>
<ItemGroup>
<!-- Only use the aar files if we have not extracted the data -->
<_LocalAarFiles Include="@(AndroidAarLibrary)" Condition=" '@(LibraryResourceDirectories->Count())' == '0' " />
</ItemGroup>
<!-- Generate a ResourceMap file for the project and its resources -->
<GenerateResourceCaseMap
AdditionalResourceDirectories="@(LibraryResourceDirectories)"
AarLibraries="@(_LocalAarFiles)"
OutputFile="$(_GenerateResourceCaseMapFile)"
ProjectDir="$(ProjectDir)"
ResourceDirectory="$(MonoAndroidResDirIntermediate)"
Expand Down Expand Up @@ -159,6 +188,7 @@ Copyright (C) 2016 Xamarin. All rights reserved.
<PropertyGroup>
<_BuildResourceDesignerDependsOn>
_SetupDesignerProperties;
_ResolveAars;
_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)
Comment on lines 90 to +93
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the inner-build per RID run during a design-time build? Does it even need to?

It's doing stuff related to the trimmer & AOT, it seems like should figure out how to skip it?

;AppendRuntimeIdentifierToOutputPath=true
;ResolveAssemblyReferencesFindRelatedSatellites=false
;SkipCompilerExecution=true
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,6 +26,6 @@
<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'"/>

</Project>
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,38 @@ 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);
foreach (var entry in zip) {
if (entry.IsDirectory)
continue;
if (entry.FullName != resmap)
continue;
Log.LogDebugMessage ($"Found: {entry.FullName}");
using var ms = new MemoryStream();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thought with MemoryStreamPool here.

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));
}
// no need to read the rest of the files we found the one we want
break;
}
}
}

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 @@ -761,20 +761,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
Loading