Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] ReOrder when Assets are Processed.
Browse files Browse the repository at this point in the history
Context dotnet#1150

The way the current system works is that the `UpdateAndroidAssets`
target is called as part of the `ResolveReferencesDependsOn`. This
makes it impossible to do any kind of processing of assets after
the project has already built. It also menas that a straight up
call to `Build` will result in all the assets being copied into
the intermediate directory. If your project has a large number
of assets that is going to take time. Note this might also be
called as part of a designtime build at the moment... not good.

So is there a reason why we call `UpdateAndroidAssets` before
`Compile`? We don't generate any entries in the Designer.cs files
for them.. We don't clean them up.. we dont appear to do anything
except package them! So the anser looks to be a resounding NO!

So we shall move the `UpdateAndroidAssets` to be part of the
`_CreateBaseApkDependsOnTargets`. This means they will be
processed just before we create the base apk. This will
occur after compilation has completed. So we have a win win..

This commit also includes a unit test which does some post processing
after the `Compile` target to update an asset, it makes sure that
post processing does work.
  • Loading branch information
dellis1972 committed Jan 5, 2018
1 parent 5b2e027 commit b6f90fe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,51 @@ namespace Xamarin.Android.Build.Tests
[Parallelizable (ParallelScope.Children)]
public class BuildAssetsTest : BaseTest
{
[Test]
public void CheckPostCompileAssetsIncludedInAPK ()
{
var path = Path.Combine ("temp", TestName);
var proj = new XamarinAndroidApplicationProject () {
ProjectName = "App1",
IsRelease = true,
OtherBuildItems = {
new AndroidItem.AndroidAsset ("Assets\\asset3.txt") {
TextContent = () => "PlaceHolder",
Encoding = Encoding.ASCII,
},
new AndroidItem.AndroidAsset ("Assets\\subfolder\\asset4.txt") {
TextContent = () => "Asset4",
Encoding = Encoding.ASCII,
},
},
Imports = {
new Import (() => "My.Test.target") {
TextContent = () => @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
<Target Name=""CustomTarget"" AfterTargets=""Compile"" >
<WriteLinesToFile
File=""Assets\\asset3.txt""
Lines=""Asset3""
Overwrite=""true""/>
</Target>
</Project>"
},
},

};
using (var b = CreateApkBuilder (Path.Combine (path, proj.ProjectName))) {
Assert.IsTrue (b.Build (proj), "{0} should have built successfully.", proj.ProjectName);
using (var apk = ZipHelper.OpenZip (Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, "android", "bin", "packaged_resources"))) {
var item = "assets/asset3.txt";
var data = ZipHelper.ReadFileFromZip (apk, item);
Assert.IsNotNull (data, "{0} should be in the apk.", item);
var text = Encoding.ASCII.GetString (data);
Assert.AreEqual ("Asset3\n", text, $"The Contents of {item} should be \"Asset3\" but was {text}");
}
Directory.Delete (Path.Combine (Root, path), recursive: true);
}

}

[Test]
public void CheckAssetsAreIncludedInAPK ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,6 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
</CoreResolveReferencesDependsOn>
<ResolveReferencesDependsOn>
$(CoreResolveReferencesDependsOn);
UpdateAndroidAssets;
UpdateAndroidInterfaceProxies;
UpdateAndroidResources;
$(ApplicationResolveReferencesDependsOn);
Expand Down Expand Up @@ -1907,6 +1906,7 @@ because xbuild doesn't support framework reference assemblies.
_CheckDuplicateJavaLibraries;
_GetAdditionalResourcesFromAssemblies;
_CreateAdditionalResourceCache;
UpdateAndroidAssets;
$(_AfterCreateBaseApkDependsOnTargets);
</_CreateBaseApkDependsOnTargets>
<_CreateBaseApkInputs>
Expand Down

0 comments on commit b6f90fe

Please sign in to comment.