Skip to content

Commit

Permalink
[One .NET] use .aar instead of @(EmbeddedResource) (#5131)
Browse files Browse the repository at this point in the history
Fixes: #2441
Implements: Documentation/proposals/OneDotNetEmbeddedResources.md

The creation of the `@(EmbeddedResource)` files:

  * `__AndroidEnvironment__*`
  * `__AndroidLibraryProjects__.zip`
  * `__AndroidNativeLibraries__.zip`
  * `*.jar`

...has moved to `Xamarin.Android.Legacy.targets`.

For One .NET, add a new `_CreateAar` MSBuild target and `<CreateAar/>`
MSBuild task which now creates `.aar` files in `$(OutputPath)` for
class libraries.

The `_ResolveAars` MSBuild target will locate `.aar` files next to
.NET assemblies and use them if found.

The MSBuild targets/tasks for consuming `@(EmbeddedResource)` files in
assemblies remains in place for backwards compatibility.  The only
change was to support the new `.netenv` directory inside `.aar` files.

The `_CategorizeAndroidLibraries` MSBuild target handles setting up
the new, simplified `@(AndroidLibrary)` item group.

I added several tests for these scenarios.

Other changes:

  * A new `<ExtractJarsFromAar/>` MSBuild task is needed for
    `@(LibraryProjectZip)` items for Java bindings.  This task extracts
    `.jar` files to `$(IntermediateOutputPath)library_project_jars` and
    `annotations.zip` items to `$(IntermediateOutputPath)library_project_annotations`.
  * The `@(LibraryProjectZip)` item group now works in .NET 5+.  We no
    longer need the `LibraryProjectZip` test category, as these tests
    pass.  Several of these tests needed to set `ProjectName` as they
    had two projects named `UnnamedProject.csproj`.
  * `<ResolveLibraryProjectImports/>` needs to store the full file
    *and* the extension in `obj\$(Configuration)\lp\map.cache` now as
     you would commonly have both a `Foo.dll` and `Foo.aar`.  I had to
     adjust `AssemblyIdentityMap` to account for this.
  * `.aar` files can no longer assume `%(AndroidSkipResourceProcessing)`
    is `false` by default.  `.aar` files coming from Xamarin.Android
    class libraries could have custom views that need to be fixed up at
    build time in the main Xamarin.Android application project.
  * When the nested, inner build for the
    `_ComputeFilesToPublishForRuntimeIdentifiers` MSBuild target runs,
    I further trimmed down which targets run.  `IncrementalClean` was
    running multiple times and deleting files!
  * Fixed where `Files.ExtractAll()` was extracting a `.DS_Store` file
    in the root of `.aar` files.
  • Loading branch information
jonathanpeppers authored Oct 16, 2020
1 parent c814358 commit fcd7cf8
Show file tree
Hide file tree
Showing 33 changed files with 981 additions and 291 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [proposal] Embedded Resources and `.nupkg` Files
# One .NET: Embedded Resources and `.nupkg` Files

Traditionally, a Xamarin.Android class library can contain many types
of Android-specific files:
Expand Down
2 changes: 1 addition & 1 deletion build-tools/automation/azure-pipelines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ variables:
# - This is a non-fork branch with name containing "mono-" (for Mono bumps)
IsMonoBranch: $[and(eq(variables['XA.Commercial.Build'], 'true'), ne(variables['System.PullRequest.IsFork'], 'True'), or(contains(variables['Build.SourceBranchName'], 'mono-'), contains(variables['System.PullRequest.SourceBranch'], 'mono-')))]
RunAllTests: $[or(eq(variables['XA.RunAllTests'], true), eq(variables['IsMonoBranch'], true))]
DotNetNUnitCategories: '& TestCategory != DotNetIgnore & TestCategory != AOT & TestCategory != LibraryProjectZip & TestCategory != MkBundle & TestCategory != MonoSymbolicate & TestCategory != PackagesConfig & TestCategory != StaticProject'
DotNetNUnitCategories: '& TestCategory != DotNetIgnore & TestCategory != AOT & TestCategory != MkBundle & TestCategory != MonoSymbolicate & TestCategory != PackagesConfig & TestCategory != StaticProject'
NUnit.NumberOfTestWorkers: 4
GitHub.Token: $(github--pat--vs-mobiletools-engineering-service2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ It is shared between "legacy" binding projects and .NET 5 projects.

<Target Name="ExportJarToXml"
DependsOnTargets="$(ExportJarToXmlDependsOnTargets)"
Condition=" '$(UsingAndroidNETSdk)' != 'true' Or '@(InputJar->Count())' != '0' Or '@(EmbeddedJar->Count())' != '0' ">
Condition=" '$(UsingAndroidNETSdk)' != 'true' Or '@(InputJar->Count())' != '0' Or '@(EmbeddedJar->Count())' != '0' Or '@(LibraryProjectZip->Count())' != '0' ">
<PropertyGroup>
<AllowUnsafeBlocks Condition=" '$(AllowUnsafeBlocks)' != 'true' ">true</AllowUnsafeBlocks>
</PropertyGroup>
</Target>

<Target Name="GenerateBindings"
Condition=" '$(UsingAndroidNETSdk)' != 'true' Or '@(InputJar->Count())' != '0' Or '@(EmbeddedJar->Count())' != '0' "
Condition=" '$(UsingAndroidNETSdk)' != 'true' Or '@(InputJar->Count())' != '0' Or '@(EmbeddedJar->Count())' != '0' Or '@(LibraryProjectZip->Count())' != '0' "
DependsOnTargets="ExportJarToXml;_ResolveMonoAndroidSdks"
Inputs="$(ApiOutputFile);@(TransformFile);@(ReferencePath);@(ReferenceDependencyPaths);$(MSBuildAllProjects)"
Outputs="$(_GeneratorStampFile)">
Expand All @@ -54,7 +54,7 @@ It is shared between "legacy" binding projects and .NET 5 projects.
<RemoveDirFixed Directories="$(GeneratedOutputPath)" Condition="Exists ('$(GeneratedOutputPath)')" />

<ItemGroup>
<AnnotationsZip Include="$(IntermediateOutputPath)library_project_annotations\*.zip" />
<AnnotationsZip Include="$(IntermediateOutputPath)library_project_annotations\**\*.zip" />
</ItemGroup>

<!-- Create the .cs binding source files -->
Expand Down Expand Up @@ -89,27 +89,11 @@ It is shared between "legacy" binding projects and .NET 5 projects.
</ItemGroup>
</Target>

<Target Name="AddEmbeddedJarsAsResources" DependsOnTargets="GenerateBindings">
<ItemGroup>
<EmbeddedResource Include="@(EmbeddedJar)">
<LogicalName>%(Filename)%(Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Target>

<Target Name="AddEmbeddedReferenceJarsAsResources" DependsOnTargets="GenerateBindings">
<ItemGroup>
<EmbeddedResource Include="@(EmbeddedReferenceJar)">
<LogicalName>__reference__%(Filename)%(Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Target>

<Target Name="ResolveLibraryProjects" DependsOnTargets="_CreateBindingResourceArchive" />
<Target Name="ResolveLibraryProjects" DependsOnTargets="$(_ResolveLibraryProjectsDependsOn)" />

<Target Name="AddLibraryJarsToBind" DependsOnTargets="ResolveLibraryProjects">
<ItemGroup>
<InputJar Include="$(IntermediateOutputPath)library_project_jars\*.jar" />
<InputJar Include="$(IntermediateOutputPath)library_project_jars\**\*.jar" />
</ItemGroup>
</Target>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<!--
***********************************************************************************************
Microsoft.Android.Sdk.AndroidLibraries.targets
This file contains the .NET 5-specific targets related to
@(AndroidLibrary) items and .aar files generation for class library
projects.
***********************************************************************************************
-->
<Project>

<UsingTask TaskName="Xamarin.Android.Tasks.CreateAar" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.ExtractJarsFromAar" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />

<ItemGroup>
<AvailableItemName Include="AndroidLibrary" />
</ItemGroup>
<ItemDefinitionGroup>
<AndroidLibrary>
<Bind>false</Bind>
<Pack>true</Pack>
</AndroidLibrary>
</ItemDefinitionGroup>
<PropertyGroup>
<_AarCacheFile>$(IntermediateOutputPath)$(TargetName).aar.cache</_AarCacheFile>
<_AarOutputPath>$(OutputPath)$(TargetName).aar</_AarOutputPath>
</PropertyGroup>

<Target Name="_CategorizeAndroidLibraries">
<ItemGroup Condition=" '$(AndroidApplication)' == 'true' ">
<AndroidAarLibrary Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.aar' " />
<AndroidJavaLibrary Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.jar' " />
<AndroidNativeLibrary Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.so' " />
</ItemGroup>
<ItemGroup Condition=" '$(AndroidApplication)' != 'true' ">
<AndroidAarLibrary Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.aar' and '%(AndroidLibrary.Bind)' != 'true' " />
<LibraryProjectZip Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.aar' and '%(AndroidLibrary.Bind)' == 'true' " />
<AndroidJavaLibrary Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.jar' and '%(AndroidLibrary.Bind)' != 'true' " />
<EmbeddedJar Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.jar' and '%(AndroidLibrary.Bind)' == 'true' " />
<EmbeddedNativeLibrary Include="@(AndroidLibrary)" Condition=" '%(AndroidLibrary.Extension)' == '.so' " />
<None Include="@(LibraryProjectZip)" CopyToOutputDirectory="PreserveNewest" Link="%(Filename)%(Extension)" />
</ItemGroup>
</Target>

<Target Name="_ResolveAars" Condition=" '$(AndroidApplication)' == 'true' ">
<ItemGroup>
<_AarSearchDirectory Include="@(_ReferencePath->'%(RootDir)%(Directory)')" />
<_AarSearchDirectory Include="@(_ReferenceDependencyPaths->'%(RootDir)%(Directory)')" />
<_AarDistinctDirectory Include="@(_AarSearchDirectory->Distinct())" />
<_AarFromLibraries Include="%(_AarDistinctDirectory.Identity)*.aar" />
</ItemGroup>
<ItemGroup Condition=" '@(_AarFromLibraries->Count())' != '0' ">
<AndroidAarLibrary Include="@(_AarFromLibraries)" Exclude="@(AndroidAarLibrary)" />
<!--
NOTE: %(AndroidSkipResourceProcessing) is required for located .aar's; there could be custom views.
Update in a second step, in case there is was an existing @(AndroidAarLibrary) with the same path.
-->
<AndroidAarLibrary Update="@(_AarFromLibraries)" AndroidSkipResourceProcessing="false" />
</ItemGroup>
</Target>

<Target Name="_CreateAarCache"
Condition=" '$(AndroidApplication)' != 'true' ">
<ItemGroup>
<_CreateAarInputs Include="@(AndroidAsset)" />
<_CreateAarInputs Include="@(_AndroidResourceDest)" />
<_CreateAarInputs Include="@(AndroidEnvironment)" />
<_CreateAarInputs Include="@(EmbeddedJar)" />
<_CreateAarInputs Include="@(EmbeddedNativeLibrary)" />
</ItemGroup>
<Hash
ItemsToHash="@(_CreateAarInputs)"
IgnoreCase="false">
<Output TaskParameter="HashResult" PropertyName="_AarDependencyHash" />
</Hash>
<WriteLinesToFile
Condition=" '$(_AarDependencyHash)' != '' "
Lines="$(_AarDependencyHash)"
File="$(_AarCacheFile)"
Overwrite="True"
WriteOnlyWhenDifferent="True"
/>
<ItemGroup Condition=" '$(_AarDependencyHash)' != '' ">
<_CreateAarInputs Include="$(_AarCacheFile)" />
<FileWrites Include="$(_AarCacheFile)" />
</ItemGroup>
</Target>

<Target Name="_CreateAar"
Condition=" '$(AndroidApplication)' != 'true' "
DependsOnTargets="_CreateAarCache"
Inputs="@(_CreateAarInputs)"
Outputs="$(_AarOutputPath)">
<CreateAar
AssetDirectory="$(MonoAndroidAssetsPrefix)"
AndroidAssets="@(AndroidAsset)"
AndroidResources="@(_AndroidResourceDest)"
AndroidEnvironment="@(AndroidEnvironment)"
JarFiles="@(AndroidJavaLibrary);@(EmbeddedJar)"
NativeLibraries="@(EmbeddedNativeLibrary)"
OutputFile="$(_AarOutputPath)"
/>
<ItemGroup>
<FileWrites Include="$(_AarOutputPath)" />
<None Include="$(_AarOutputPath)" Pack="true" PackagePath="lib\$(TargetFramework)" />
</ItemGroup>
</Target>

<Target Name="_ExtractAar"
Inputs="@(LibraryProjectZip)"
Outputs="$(_AndroidStampDirectory)_ExtractAar.stamp">
<ExtractJarsFromAar
OutputJarsDirectory="$(IntermediateOutputPath)library_project_jars"
OutputAnnotationsDirectory="$(IntermediateOutputPath)library_project_annotations"
Libraries="@(LibraryProjectZip)"
/>
<Touch Files="$(_AndroidStampDirectory)_ExtractAar.stamp" AlwaysCreate="true" />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ _ResolveAssemblies MSBuild target.
<OutputPath Condition=" '$(_OuterOutputPath)' != '' ">$(_OuterOutputPath)</OutputPath>
<OutDir Condition=" '$(_OuterOutputPath)' != '' ">$(_OuterOutputPath)</OutDir>
<PublishDir>$(OutputPath)</PublishDir>
<BuildDependsOn>_RemoveLegacyDesigner;$(BuildDependsOn)</BuildDependsOn>
<!-- We don't want IncrementalClean to run here, or files get deleted -->
<CoreBuildDependsOn>
$([MSBuild]::Unescape($(CoreBuildDependsOn.Replace('IncrementalClean;', ''))))
</CoreBuildDependsOn>
</PropertyGroup>

<Target Name="_ComputeFilesToPublishForRuntimeIdentifiers"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
_ValidateLinkMode;
_CheckNonIdealConfigurations;
_SetupDesignTimeBuildForBuild;
_CategorizeAndroidLibraries;
_CreatePropertiesCache;
_CleanIntermediateIfNeeded;
_CheckProjectItems;
_CheckForContent;
_ValidateAndroidPackageProperties;
AddLibraryJarsToBind;
$(BuildDependsOn);
_CompileDex;
$(_AfterCompileDex);
Expand Down Expand Up @@ -69,14 +71,14 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
<BuildDependsOn>
_ValidateLinkMode;
_SetupDesignTimeBuildForBuild;
_CategorizeAndroidLibraries;
_CreatePropertiesCache;
_CleanIntermediateIfNeeded;
_AddAndroidDefines;
_CreateNativeLibraryArchive;
_AddAndroidEnvironmentToCompile;
_CheckForContent;
_RemoveLegacyDesigner;
_ValidateAndroidPackageProperties;
AddLibraryJarsToBind;
$(BuildDependsOn);
</BuildDependsOn>
<_BeforeIncrementalClean>
Expand All @@ -90,15 +92,22 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
_SeparateAppExtensionReferences;
$(ResolveReferencesDependsOn);
_AddAndroidCustomMetaData;
_ResolveAars;
</CoreResolveReferencesDependsOn>
<ResolveReferencesDependsOn>
$(CoreResolveReferencesDependsOn);
UpdateAndroidInterfaceProxies;
UpdateAndroidResources;
AddBindingsToCompile;
AddEmbeddedJarsAsResources;
AddEmbeddedReferenceJarsAsResources;
</ResolveReferencesDependsOn>
<_UpdateAndroidResourcesDependsOn>
$(CoreResolveReferencesDependsOn);
_CreatePropertiesCache;
_CheckForDeletedResourceFile;
_ComputeAndroidResourcePaths;
_UpdateAndroidResgen;
_CreateAar;
</_UpdateAndroidResourcesDependsOn>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -136,6 +145,7 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
_ResolveMonoAndroidSdks;
_ExtractLibraryProjectImports;
_GetLibraryImports;
_ExtractAar;
_ExportJarToXml;
</ExportJarToXmlDependsOnTargets>
<GetAndroidDependenciesDependsOn>
Expand All @@ -145,6 +155,9 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
_ResolveAndroidTooling;
$(GetAndroidDependenciesDependsOn);
</GetAndroidDependenciesDependsOn>
<_ResolveLibraryProjectsDependsOn>
_ExtractAar;
</_ResolveLibraryProjectsDependsOn>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
<TargetPlatformSupported Condition=" '$(TargetPlatformIdentifier)' == 'Android' ">true</TargetPlatformSupported>
</PropertyGroup>
<!-- Build ordering, should be imported before Xamarin.Android.Common.targets -->
<Import Project="Microsoft.Android.Sdk.BuildOrder.targets" />
<Import Project="Microsoft.Android.Sdk.BuildOrder.targets" Condition=" '$(_ComputeFilesToPublishForRuntimeIdentifiers)' != 'true' " />

<Import Project="..\tools\Xamarin.Android.Common.targets" />
<Import Project="..\tools\Xamarin.Android.Bindings.Core.targets" />
<Import Project="..\tools\Xamarin.Android.Bindings.ClassParse.targets" />
<Import Project="Microsoft.Android.Sdk.AndroidLibraries.targets" />
<Import Project="Microsoft.Android.Sdk.Application.targets" Condition=" '$(AndroidApplication)' == 'true' " />
<Import Project="Microsoft.Android.Sdk.AssemblyResolution.targets" />
<Import Project="Microsoft.Android.Sdk.DefaultItems.targets" />
Expand Down
Loading

0 comments on commit fcd7cf8

Please sign in to comment.