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

[Xamarin.Android.Build.Tasks] Fix aapt_rules.txt corruption #7587

Merged
merged 3 commits into from
Dec 12, 2022
Merged
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 @@ -244,6 +244,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<ItemGroup Condition=" '$(_Aapt2ProguardRules)' != '' And Exists('$(_Aapt2ProguardRules)') ">
<ProguardConfiguration Include="$(_Aapt2ProguardRules)" />
<FileWrites Include="$(_Aapt2ProguardRules)" />
<FileWrites Include="$(IntermediateOutputPath)android\*\aapt_rules.txt" />
</ItemGroup>
</Target>
</Project>
30 changes: 24 additions & 6 deletions src/Xamarin.Android.Build.Tasks/Tasks/Aapt2Link.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.Linq;
Expand Down Expand Up @@ -79,8 +80,8 @@ public class Aapt2Link : Aapt2 {

AssemblyIdentityMap assemblyMap = new AssemblyIdentityMap ();
List<string> tempFiles = new List<string> ();
SortedSet<string> rulesFiles = new SortedSet<string> ();
Dictionary<string, long> apks = new Dictionary<string, long> ();
string proguardRuleOutputTemp;

protected override int GetRequiredDaemonInstances ()
{
Expand All @@ -92,8 +93,6 @@ public async override System.Threading.Tasks.Task RunTaskAsync ()
try {
assemblyMap.Load (Path.Combine (WorkingDirectory, AssemblyIdentityMapFile));

proguardRuleOutputTemp = GetTempFile ();

await this.WhenAll (ManifestFiles, ProcessManifest);

ProcessOutput ();
Expand All @@ -120,8 +119,19 @@ public async override System.Threading.Tasks.Task RunTaskAsync ()
}
}
}
if (!string.IsNullOrEmpty (ProguardRuleOutput))
Files.CopyIfChanged (proguardRuleOutputTemp, ProguardRuleOutput);
if (!string.IsNullOrEmpty (ProguardRuleOutput)) {
// combine the "proguard" temp files into one file.
var sb = new StringBuilder ();
sb.AppendLine ("#Auto Generated file. Do not Edit.");
lock (rulesFiles) {
foreach (var file in rulesFiles) {
sb.AppendLine ($"# Data from {file}");
foreach (var line in File.ReadLines (file))
sb.AppendLine (line);
}
}
Files.CopyIfStringChanged (sb.ToString (), ProguardRuleOutput);
}
} finally {
lock (tempFiles) {
foreach (var temp in tempFiles) {
Expand Down Expand Up @@ -285,7 +295,7 @@ string [] GenerateCommandLineCommands (string ManifestFile, string currentAbi, s

if (!string.IsNullOrEmpty (ProguardRuleOutput)) {
cmd.Add ("--proguard");
cmd.Add (GetFullPath (proguardRuleOutputTemp));
cmd.Add (GetFullPath (GetManifestRulesFile (manifestDir)));
}
cmd.Add ("-o");
cmd.Add (GetFullPath (currentResourceOutputFile));
Expand Down Expand Up @@ -341,6 +351,14 @@ void ProcessManifest (ITaskItem manifestFile)
}
}

string GetManifestRulesFile (string manifestDir)
{
string rulesFile = Path.Combine (manifestDir, "aapt_rules.txt");
jonathanpeppers marked this conversation as resolved.
Show resolved Hide resolved
lock (rulesFiles)
rulesFiles.Add (rulesFile);
return rulesFile;
}

string GetTempFile ()
{
var temp = Path.GetTempFileName ();
Expand Down