Skip to content

Commit 06a4152

Browse files
[Xamarin.Android.Build.Tasks] @(LinkDescription) should be in Inputs for the linker (#4816)
Fixes: https://developercommunity.visualstudio.com/content/problem/1061307/changes-not-applied-on-build-after-linker-configur.html Changes to a `@(LinkDescription)` file did not trigger the `_LinkAssembliesShrink` MSBuild target to run again. The only way to see changes from this file was to `Rebuild` every time. Adding a test for this scenario is also nice, as it didn't seem like we had an MSBuild test verifying that `@(LinkDescription)` works in general.
1 parent 76fbda5 commit 06a4152

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#### Application and library build and deployment
2+
3+
* [Developer Community 1061307][0]: Fixes an issue where modifying
4+
`@(LinkDescription)` files would require a `Rebuild` to see the
5+
changes reflected in the Android application build output.
6+
7+
[0]: https://developercommunity.visualstudio.com/content/problem/1061307/changes-not-applied-on-build-after-linker-configur.html

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/LinkerTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,54 @@ void WarnAboutAppDomains (XamarinAndroidApplicationProject proj, string testName
202202
Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, "warning XA2000: Use of AppDomain.CreateDomain()"), "Should warn XA2000 about creating AppDomain.");
203203
}
204204
}
205+
206+
[Test]
207+
public void LinkDescription ()
208+
{
209+
string linker_xml = "<linker/>";
210+
211+
var proj = new XamarinAndroidApplicationProject {
212+
IsRelease = true,
213+
OtherBuildItems = {
214+
new BuildItem ("LinkDescription", "linker.xml") {
215+
TextContent = () => linker_xml
216+
}
217+
}
218+
};
219+
// So we can use Mono.Cecil to open assemblies directly
220+
proj.SetProperty ("AndroidEnableAssemblyCompression", "False");
221+
222+
using (var b = CreateApkBuilder ()) {
223+
Assert.IsTrue (b.Build (proj), "first build should have succeeded.");
224+
225+
linker_xml =
226+
@"<linker>
227+
<assembly fullname=""mscorlib"">
228+
<type fullname=""System.Console"">
229+
<method name=""Beep"" />
230+
</type>
231+
</assembly>
232+
</linker>";
233+
proj.Touch ("linker.xml");
234+
235+
Assert.IsTrue (b.Build (proj, doNotCleanupOnUpdate: true), "second build should have succeeded.");
236+
237+
var apk = Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, $"{proj.PackageName}.apk");
238+
FileAssert.Exists (apk);
239+
using (var zip = ZipHelper.OpenZip (apk)) {
240+
var entry = zip.ReadEntry ("assemblies/mscorlib.dll");
241+
Assert.IsNotNull (entry, "mscorlib.dll should exist in apk!");
242+
using (var stream = new MemoryStream ()) {
243+
entry.Extract (stream);
244+
stream.Position = 0;
245+
using (var assembly = AssemblyDefinition.ReadAssembly (stream)) {
246+
var type = assembly.MainModule.GetType ("System.Console");
247+
var method = type.Methods.FirstOrDefault (p => p.Name == "Beep");
248+
Assert.IsNotNull (method, "System.Console.Beep should exist!");
249+
}
250+
}
251+
}
252+
}
253+
}
205254
}
206255
}

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@ because xbuild doesn't support framework reference assemblies.
14741474

14751475
<Target Name="_LinkAssembliesShrink"
14761476
Condition="'$(AndroidLinkMode)' != 'None' and '$(UsingAndroidNETSdk)' != 'true' "
1477-
Inputs="@(ResolvedUserAssemblies);$(_AndroidBuildPropertiesCache)"
1477+
Inputs="@(ResolvedUserAssemblies);@(LinkDescription);$(_AndroidBuildPropertiesCache)"
14781478
Outputs="$(_AndroidLinkFlag)">
14791479

14801480
<PropertyGroup>

0 commit comments

Comments
 (0)