Skip to content

Commit abea285

Browse files
[tests] Let BuildReleaseArm64 test apk size (#5547)
Context: #5441 Test legacy/dotnet simple/xforms Release builds apk sizes against reference apkdesc files. Co-authored-by: Jonathan Peppers <jonathan.peppers@gmail.com>
1 parent fce6642 commit abea285

File tree

10 files changed

+4263
-5
lines changed

10 files changed

+4263
-5
lines changed

build-tools/automation/azure-pipelines.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ stages:
251251
testResultsFiles: TestResult-*.xml
252252
testRunTitle: Java Interop Tests - Windows Build Tree
253253

254+
- script: dotnet tool update apkdiff -g
255+
displayName: install apkdiff dotnet tool
256+
continueOnError: true
257+
254258
# Limit the amount of worker threads used to run these tests in parallel to half of what is currently available (8) on the Windows pool.
255259
# Using all available cores seems to occasionally bog down our machines and cause parallel test execution to slow down dramatically.
256260
# Only run a subset of the Xamarin.Android.Build.Tests against the local Windows build tree.
@@ -294,10 +298,8 @@ stages:
294298
- script: mono $(System.DefaultWorkingDirectory)/build-tools/xaprepare/xaprepare/bin/$(ApkTestConfiguration)/xaprepare.exe --s=EmulatorTestDependencies --no-emoji --run-mode=CI
295299
displayName: install emulator
296300

297-
- task: CmdLine@2
301+
- script: 'dotnet tool update apkdiff -g'
298302
displayName: install apkdiff dotnet tool
299-
inputs:
300-
script: 'dotnet tool update apkdiff -g'
301303
continueOnError: true
302304

303305
- template: yaml-templates/apk-instrumentation.yaml

build-tools/automation/yaml-templates/run-msbuild-mac-tests.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ jobs:
2727
artifactName: $(TestAssembliesArtifactName)
2828
downloadPath: $(System.DefaultWorkingDirectory)/bin/Test$(XA.Build.Configuration)
2929

30+
- task: CmdLine@2
31+
displayName: install apkdiff dotnet tool
32+
inputs:
33+
script: 'dotnet tool update apkdiff -g'
34+
continueOnError: true
35+
3036
- template: run-nunit-tests.yaml
3137
parameters:
3238
useDotNet: $(UseDotNet)

build-tools/automation/yaml-templates/run-msbuild-win-tests.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ jobs:
3232
artifactName: $(TestAssembliesArtifactName)
3333
downloadPath: $(System.DefaultWorkingDirectory)\bin\Test$(XA.Build.Configuration)
3434

35+
- task: CmdLine@2
36+
displayName: install apkdiff dotnet tool
37+
inputs:
38+
script: 'dotnet tool update apkdiff -g'
39+
continueOnError: true
40+
3541
# Limit the amount of worker threads used to run these tests in parallel to half of what is currently available (8) on the Windows pool.
3642
# Using all available cores seems to occasionally bog down our machines and cause parallel test execution to slow down dramatically.
3743
- template: run-nunit-tests.yaml

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public static string GetLinkedPath (ProjectBuilder builder, bool isRelease, stri
6464
}
6565

6666
[Test]
67+
[Category ("SmokeTests")]
6768
public void BuildReleaseArm64 ([Values (false, true)] bool forms)
6869
{
6970
var proj = forms ?
@@ -81,6 +82,16 @@ public void BuildReleaseArm64 ([Values (false, true)] bool forms)
8182
proj.AddDotNetCompatPackages ();
8283
}
8384

85+
byte [] apkDescData;
86+
var flavor = (forms ? "XForms" : "Simple") + (Builder.UseDotNet ? "DotNet" : "Legacy");
87+
var apkDescFilename = $"BuildReleaseArm64{flavor}.apkdesc";
88+
var apkDescReference = "reference.apkdesc";
89+
using (var stream = typeof (XamarinAndroidApplicationProject).Assembly.GetManifestResourceStream ($"Xamarin.ProjectTools.Resources.Base.{apkDescFilename}")) {
90+
apkDescData = new byte [stream.Length];
91+
stream.Read (apkDescData, 0, (int) stream.Length);
92+
}
93+
proj.OtherBuildItems.Add (new BuildItem ("ApkDescFile", apkDescReference) { BinaryContent = () => apkDescData });
94+
8495
// use BuildHelper.CreateApkBuilder so that the test directory is not removed in tearup
8596
using (var b = BuildHelper.CreateApkBuilder (Path.Combine ("temp", TestName))) {
8697
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
@@ -90,6 +101,14 @@ public void BuildReleaseArm64 ([Values (false, true)] bool forms)
90101
? GetLinkedPath (b, true, depsFilename)
91102
: Path.Combine (proj.Root, b.ProjectDirectory, depsFilename);
92103
FileAssert.Exists (depsFile);
104+
105+
const int ApkSizeThreshold = 50 * 1024;
106+
const int AssemblySizeThreshold = 50 * 1024;
107+
var apkFile = Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, proj.PackageName + "-Signed.apk");
108+
var apkDescPath = Path.Combine (Root, b.ProjectDirectory, apkDescFilename);
109+
var apkDescReferencePath = Path.Combine (Root, b.ProjectDirectory, apkDescReference);
110+
var (code, stdOut, stdErr) = RunApkDiffCommand ($"-s --save-description-2={apkDescPath} --test-apk-size-regression={ApkSizeThreshold} --test-assembly-size-regression={AssemblySizeThreshold} {apkDescReferencePath} {apkFile}");
111+
Assert.IsTrue (code == 0, $"apkdiff regression test failed with exit code: {code}\nstdOut: {stdOut}\nstdErr: {stdErr}");
93112
}
94113
}
95114

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BaseTest.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,28 @@ protected static string RunAdbCommand (string command, bool ignoreErrors = true,
237237
return RunProcess (adb, $"{adbTarget} {command}");
238238
}
239239

240+
protected static (int code, string stdOutput, string stdError) RunApkDiffCommand (string args)
241+
{
242+
string ext = Environment.OSVersion.Platform != PlatformID.Unix ? ".exe" : "";
243+
244+
try {
245+
return RunProcessWithExitCode ("apkdiff" + ext, args);
246+
} catch (System.ComponentModel.Win32Exception) {
247+
// apkdiff's location might not be in the $PATH, try known location
248+
var profileDir = Environment.GetEnvironmentVariable ("USERPROFILE");
249+
250+
return RunProcessWithExitCode (Path.Combine (profileDir, ".dotnet", "tools", "apkdiff" + ext), args);
251+
}
252+
}
253+
240254
protected static string RunProcess (string exe, string args)
255+
{
256+
var (_, stdOutput, stdError) = RunProcessWithExitCode (exe, args);
257+
258+
return stdOutput + stdError;
259+
}
260+
261+
protected static (int code, string stdOutput, string stdError) RunProcessWithExitCode (string exe, string args)
241262
{
242263
TestContext.Out.WriteLine ($"{nameof(RunProcess)}: {exe} {args}");
243264
var info = new ProcessStartInfo (exe, args) {
@@ -266,12 +287,12 @@ protected static string RunProcess (string exe, string args)
266287
if (!proc.WaitForExit ((int)TimeSpan.FromSeconds (30).TotalMilliseconds)) {
267288
proc.Kill ();
268289
TestContext.Out.WriteLine ($"{nameof (RunProcess)} timed out: {exe} {args}");
269-
return null; //Don't try to read stdout/stderr
290+
return (-1, null, null); //Don't try to read stdout/stderr
270291
}
271292

272293
proc.WaitForExit ();
273294

274-
return standardOutput.ToString ().Trim () + errorOutput.ToString ().Trim ();
295+
return (proc.ExitCode, standardOutput.ToString ().Trim (), errorOutput.ToString ().Trim ());
275296
}
276297
}
277298

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"Comment": null,
3+
"Entries": {
4+
"AndroidManifest.xml": {
5+
"Size": 2584
6+
},
7+
"res/drawable-mdpi-v4/icon.png": {
8+
"Size": 2199
9+
},
10+
"res/drawable-hdpi-v4/icon.png": {
11+
"Size": 4791
12+
},
13+
"res/drawable-xhdpi-v4/icon.png": {
14+
"Size": 7462
15+
},
16+
"res/drawable-xxhdpi-v4/icon.png": {
17+
"Size": 13092
18+
},
19+
"res/drawable-xxxhdpi-v4/icon.png": {
20+
"Size": 20118
21+
},
22+
"res/layout/main.xml": {
23+
"Size": 544
24+
},
25+
"resources.arsc": {
26+
"Size": 1724
27+
},
28+
"classes.dex": {
29+
"Size": 316988
30+
},
31+
"assemblies/UnnamedProject.dll": {
32+
"Size": 2955
33+
},
34+
"assemblies/System.Collections.Concurrent.dll": {
35+
"Size": 9341
36+
},
37+
"assemblies/System.Collections.dll": {
38+
"Size": 4490
39+
},
40+
"assemblies/System.Console.dll": {
41+
"Size": 6209
42+
},
43+
"assemblies/System.Linq.Expressions.dll": {
44+
"Size": 115171
45+
},
46+
"assemblies/System.Linq.dll": {
47+
"Size": 16987
48+
},
49+
"assemblies/System.ObjectModel.dll": {
50+
"Size": 3372
51+
},
52+
"assemblies/System.Private.CoreLib.dll": {
53+
"Size": 527251
54+
},
55+
"assemblies/Java.Interop.dll": {
56+
"Size": 67979
57+
},
58+
"assemblies/Mono.Android.dll": {
59+
"Size": 108095
60+
},
61+
"lib/arm64-v8a/libxamarin-app.so": {
62+
"Size": 11904
63+
},
64+
"lib/arm64-v8a/libSystem.IO.Compression.Native.so": {
65+
"Size": 776216
66+
},
67+
"lib/arm64-v8a/libSystem.Native.so": {
68+
"Size": 75872
69+
},
70+
"lib/arm64-v8a/libSystem.Security.Cryptography.Native.OpenSsl.so": {
71+
"Size": 100464
72+
},
73+
"lib/arm64-v8a/libmonosgen-2.0.so": {
74+
"Size": 18570656
75+
},
76+
"lib/arm64-v8a/libmonodroid.so": {
77+
"Size": 254616
78+
},
79+
"lib/arm64-v8a/libxa-internal-api.so": {
80+
"Size": 66544
81+
},
82+
"lib/arm64-v8a/libxamarin-debug-app-helper.so": {
83+
"Size": 191408
84+
},
85+
"META-INF/ANDROIDD.SF": {
86+
"Size": 2948
87+
},
88+
"META-INF/ANDROIDD.RSA": {
89+
"Size": 1213
90+
},
91+
"META-INF/MANIFEST.MF": {
92+
"Size": 2821
93+
}
94+
},
95+
"PackageSize": 8050955
96+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"Comment": null,
3+
"Entries": {
4+
"AndroidManifest.xml": {
5+
"Size": 2584
6+
},
7+
"res/drawable-mdpi-v4/icon.png": {
8+
"Size": 2199
9+
},
10+
"res/drawable-hdpi-v4/icon.png": {
11+
"Size": 4762
12+
},
13+
"res/drawable-xhdpi-v4/icon.png": {
14+
"Size": 7462
15+
},
16+
"res/drawable-xxhdpi-v4/icon.png": {
17+
"Size": 13092
18+
},
19+
"res/drawable-xxxhdpi-v4/icon.png": {
20+
"Size": 20118
21+
},
22+
"res/layout/main.xml": {
23+
"Size": 544
24+
},
25+
"resources.arsc": {
26+
"Size": 1724
27+
},
28+
"classes.dex": {
29+
"Size": 316956
30+
},
31+
"assemblies/UnnamedProject.dll": {
32+
"Size": 2861
33+
},
34+
"assemblies/Java.Interop.dll": {
35+
"Size": 75724
36+
},
37+
"assemblies/Mono.Android.dll": {
38+
"Size": 264062
39+
},
40+
"assemblies/mscorlib.dll": {
41+
"Size": 779345
42+
},
43+
"assemblies/System.Core.dll": {
44+
"Size": 149498
45+
},
46+
"assemblies/System.dll": {
47+
"Size": 12986
48+
},
49+
"lib/arm64-v8a/libxamarin-app.so": {
50+
"Size": 19824
51+
},
52+
"lib/arm64-v8a/libmonodroid.so": {
53+
"Size": 254616
54+
},
55+
"lib/arm64-v8a/libxa-internal-api.so": {
56+
"Size": 66544
57+
},
58+
"lib/arm64-v8a/libmono-btls-shared.so": {
59+
"Size": 2160056
60+
},
61+
"lib/arm64-v8a/libmonosgen-2.0.so": {
62+
"Size": 6823104
63+
},
64+
"lib/arm64-v8a/libmono-native.so": {
65+
"Size": 1150064
66+
},
67+
"META-INF/ANDROIDD.SF": {
68+
"Size": 2225
69+
},
70+
"META-INF/ANDROIDD.RSA": {
71+
"Size": 1213
72+
},
73+
"META-INF/MANIFEST.MF": {
74+
"Size": 2098
75+
}
76+
},
77+
"PackageSize": 5142228
78+
}

0 commit comments

Comments
 (0)