-
Notifications
You must be signed in to change notification settings - Fork 538
/
Copy pathXASdkTests.cs
1175 lines (1076 loc) · 47.6 KB
/
XASdkTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Reflection;
using System.Text;
using Mono.Cecil;
using NUnit.Framework;
using Xamarin.Android.Tasks;
using Xamarin.Android.Tools;
using Xamarin.ProjectTools;
using Xamarin.Tools.Zip;
using Microsoft.Android.Build.Tasks;
#if !NET472
namespace Xamarin.Android.Build.Tests
{
[TestFixture]
[NonParallelizable] // On MacOS, parallel /restore causes issues
[Category ("Node-5")]
public class XASdkTests : BaseTest
{
/// <summary>
/// The full path to the project directory
/// </summary>
public string FullProjectDirectory { get; set; }
static readonly object [] DotNetBuildLibrarySource = new object [] {
new object [] {
/* isRelease */ false,
/* duplicateAar */ false,
},
new object [] {
/* isRelease */ false,
/* duplicateAar */ true,
},
new object [] {
/* isRelease */ true,
/* duplicateAar */ false,
},
};
[Test]
[Category ("SmokeTests")]
[TestCaseSource (nameof (DotNetBuildLibrarySource))]
public void DotNetBuildLibrary (bool isRelease, bool duplicateAar)
{
var path = Path.Combine ("temp", TestName);
var env_var = "MY_ENVIRONMENT_VAR";
var env_val = "MY_VALUE";
// Setup dependencies App A -> Lib B -> Lib C
var libC = new XASdkProject (outputType: "Library") {
ProjectName = "LibraryC",
IsRelease = isRelease,
Sources = {
new BuildItem.Source ("Bar.cs") {
TextContent = () => "public class Bar { }",
},
new AndroidItem.AndroidResource (() => "Resources\\drawable\\IMALLCAPS.png") {
BinaryContent = () => XamarinAndroidApplicationProject.icon_binary_mdpi,
},
new AndroidItem.ProguardConfiguration ("proguard.txt") {
TextContent = () => @"-ignorewarnings",
},
}
};
libC.OtherBuildItems.Add (new AndroidItem.AndroidAsset ("Assets\\bar\\bar.txt") {
BinaryContent = () => Array.Empty<byte> (),
});
var activity = libC.Sources.FirstOrDefault (s => s.Include () == "MainActivity.cs");
if (activity != null)
libC.Sources.Remove (activity);
var libCBuilder = CreateDotNetBuilder (libC, Path.Combine (path, libC.ProjectName));
Assert.IsTrue (libCBuilder.Build (), $"{libC.ProjectName} should succeed");
var aarPath = Path.Combine (FullProjectDirectory, libC.OutputPath, $"{libC.ProjectName}.aar");
FileAssert.Exists (aarPath);
using (var aar = ZipHelper.OpenZip (aarPath)) {
aar.AssertContainsEntry (aarPath, "assets/bar/bar.txt");
aar.AssertContainsEntry (aarPath, "proguard.txt");
}
var libB = new XASdkProject (outputType: "Library") {
ProjectName = "LibraryB",
IsRelease = isRelease,
Sources = {
new BuildItem.Source ("Foo.cs") {
TextContent = () =>
@"public class Foo : Bar
{
public Foo ()
{
int x = LibraryB.Resource.Drawable.IMALLCAPS;
}
}",
},
new AndroidItem.AndroidResource ("Resources\\layout\\test.axml") {
TextContent = () => {
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<ImageView xmlns:android=\"http://schemas.android.com/apk/res/android\" android:src=\"@drawable/IMALLCAPS\" />";
}
},
new AndroidItem.AndroidAsset ("Assets\\foo\\foo.txt") {
BinaryContent = () => Array.Empty<byte> (),
},
new AndroidItem.AndroidResource ("Resources\\layout\\MyLayout.axml") {
TextContent = () => "<?xml version=\"1.0\" encoding=\"utf-8\" ?><LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" />"
},
new AndroidItem.AndroidResource ("Resources\\raw\\bar.txt") {
BinaryContent = () => Array.Empty<byte> (),
},
new AndroidItem.AndroidLibrary ("sub\\directory\\foo.jar") {
BinaryContent = () => ResourceData.JavaSourceJarTestJar,
},
new AndroidItem.AndroidLibrary ("sub\\directory\\bar.aar") {
WebContent = "https://repo1.maven.org/maven2/com/balysv/material-menu/1.1.0/material-menu-1.1.0.aar",
},
new AndroidItem.AndroidJavaSource ("JavaSourceTestExtension.java") {
Encoding = Encoding.ASCII,
TextContent = () => ResourceData.JavaSourceTestExtension,
},
new AndroidItem.ProguardConfiguration ("proguard.txt") {
TextContent = () => @"-ignorewarnings",
},
}
};
libB.OtherBuildItems.Add (new AndroidItem.AndroidEnvironment ("env.txt") {
TextContent = () => $"{env_var}={env_val}",
});
libB.OtherBuildItems.Add (new AndroidItem.AndroidEnvironment ("sub\\directory\\env.txt") {
TextContent = () => $"{env_var}={env_val}",
});
libB.OtherBuildItems.Add (new AndroidItem.AndroidLibrary ("sub\\directory\\arm64-v8a\\libfoo.so") {
BinaryContent = () => Array.Empty<byte> (),
});
libB.OtherBuildItems.Add (new AndroidItem.AndroidNativeLibrary (default (Func<string>)) {
Update = () => "libfoo.so",
MetadataValues = "Link=x86\\libfoo.so",
BinaryContent = () => Array.Empty<byte> (),
});
libB.AddReference (libC);
activity = libB.Sources.FirstOrDefault (s => s.Include () == "MainActivity.cs");
if (activity != null)
libB.Sources.Remove (activity);
var libBBuilder = CreateDotNetBuilder (libB, Path.Combine (path, libB.ProjectName));
Assert.IsTrue (libBBuilder.Build (), $"{libB.ProjectName} should succeed");
var projectJarHash = Files.HashString (Path.Combine (libB.IntermediateOutputPath,
"binding", "bin", $"{libB.ProjectName}.jar").Replace ("\\", "/"));
// Check .aar file for class library
var libBOutputPath = Path.Combine (FullProjectDirectory, libB.OutputPath);
aarPath = Path.Combine (libBOutputPath, $"{libB.ProjectName}.aar");
FileAssert.Exists (aarPath);
FileAssert.Exists (Path.Combine (libBOutputPath, "bar.aar"));
using (var aar = ZipHelper.OpenZip (aarPath)) {
aar.AssertContainsEntry (aarPath, "assets/foo/foo.txt");
aar.AssertContainsEntry (aarPath, "res/layout/mylayout.xml");
aar.AssertContainsEntry (aarPath, "res/raw/bar.txt");
aar.AssertContainsEntry (aarPath, ".net/__res_name_case_map.txt");
aar.AssertContainsEntry (aarPath, ".net/env/190E30B3D205731E.env");
aar.AssertContainsEntry (aarPath, ".net/env/2CBDAB7FEEA94B19.env");
aar.AssertContainsEntry (aarPath, "libs/A1AFA985571E728E.jar");
aar.AssertContainsEntry (aarPath, $"libs/{projectJarHash}.jar");
aar.AssertContainsEntry (aarPath, "jni/arm64-v8a/libfoo.so");
aar.AssertContainsEntry (aarPath, "jni/x86/libfoo.so");
}
// Check EmbeddedResource files do not exist
var assemblyPath = Path.Combine (FullProjectDirectory, libB.OutputPath, $"{libB.ProjectName}.dll");
FileAssert.Exists (assemblyPath);
using (var assembly = AssemblyDefinition.ReadAssembly (assemblyPath)) {
Assert.AreEqual (0, assembly.MainModule.Resources.Count);
}
var appA = new XASdkProject {
ProjectName = "AppA",
IsRelease = isRelease,
Sources = {
new BuildItem.Source ("Bar.cs") {
TextContent = () => "public class Bar : Foo { }",
}
}
};
appA.AddReference (libB);
if (duplicateAar) {
// Test a duplicate @(AndroidLibrary) item with the same path of LibraryB.aar
appA.OtherBuildItems.Add (new AndroidItem.AndroidLibrary (aarPath));
}
var appBuilder = CreateDotNetBuilder (appA, Path.Combine (path, appA.ProjectName));
Assert.IsTrue (appBuilder.Build (), $"{appA.ProjectName} should succeed");
// Check .apk/.aab for assets, res, and native libraries
var apkPath = Path.Combine (FullProjectDirectory, appA.OutputPath, $"{appA.PackageName}-Signed.apk");
FileAssert.Exists (apkPath);
using (var apk = ZipHelper.OpenZip (apkPath)) {
apk.AssertContainsEntry (apkPath, "assets/foo/foo.txt");
apk.AssertContainsEntry (apkPath, "assets/bar/bar.txt");
apk.AssertContainsEntry (aarPath, "res/layout/mylayout.xml");
apk.AssertContainsEntry (apkPath, "res/raw/bar.txt");
apk.AssertContainsEntry (apkPath, "lib/arm64-v8a/libfoo.so");
apk.AssertContainsEntry (apkPath, "lib/x86/libfoo.so");
}
// Check classes.dex contains foo.jar
var intermediate = Path.Combine (FullProjectDirectory, appA.IntermediateOutputPath);
var dexFile = Path.Combine (intermediate, "android", "bin", "classes.dex");
FileAssert.Exists (dexFile);
var proguardFiles = Directory.GetFiles (Path.Combine (intermediate, "lp"), "proguard.txt", SearchOption.AllDirectories);
Assert.AreEqual (2, proguardFiles.Length, "There should be only two proguard.txt files.");
string className = "Lcom/xamarin/android/test/msbuildtest/JavaSourceJarTest;";
Assert.IsTrue (DexUtils.ContainsClass (className, dexFile, AndroidSdkPath), $"`{dexFile}` should include `{className}`!");
className = "Lcom/xamarin/android/test/msbuildtest/JavaSourceTestExtension;";
Assert.IsTrue (DexUtils.ContainsClass (className, dexFile, AndroidSdkPath), $"`{dexFile}` should include `{className}`!");
// Check environment variable
var environmentFiles = EnvironmentHelper.GatherEnvironmentFiles (intermediate, "x86", required: true);
var environmentVariables = EnvironmentHelper.ReadEnvironmentVariables (environmentFiles);
Assert.IsTrue (environmentVariables.TryGetValue (env_var, out string actual), $"Environment should contain {env_var}");
Assert.AreEqual (env_val, actual, $"{env_var} should be {env_val}");
// Check Resource.designer.cs
var resource_designer_cs = Path.Combine (intermediate, "Resource.designer.cs");
FileAssert.Exists (resource_designer_cs);
var resource_designer_text = File.ReadAllText (resource_designer_cs);
StringAssert.Contains ("public const int MyLayout", resource_designer_text);
StringAssert.Contains ("global::LibraryB.Resource.Drawable.IMALLCAPS = global::AppA.Resource.Drawable.IMALLCAPS", resource_designer_text);
}
[Test]
public void DotNetNew ([Values ("android", "androidlib", "android-bindinglib", "androidwear")] string template)
{
var dotnet = CreateDotNetBuilder ();
Assert.IsTrue (dotnet.New (template), $"`dotnet new {template}` should succeed");
File.WriteAllBytes (Path.Combine (dotnet.ProjectDirectory, "foo.jar"), ResourceData.JavaSourceJarTestJar);
Assert.IsTrue (dotnet.New ("android-activity"), "`dotnet new android-activity` should succeed");
Assert.IsTrue (dotnet.New ("android-layout", Path.Combine (dotnet.ProjectDirectory, "Resources", "layout")), "`dotnet new android-layout` should succeed");
// Debug build
Assert.IsTrue (dotnet.Build (), "`dotnet build` should succeed");
dotnet.AssertHasNoWarnings ();
// Release build
Assert.IsTrue (dotnet.Build (parameters: new [] { "Configuration=Release" }), "`dotnet build` should succeed");
dotnet.AssertHasNoWarnings ();
}
static readonly object[] DotNetPackTargetFrameworks = new object[] {
new object[] {
"net6.0",
"android",
31,
},
new object[] {
"net6.0",
"android31",
31,
},
new object[] {
"net7.0",
"android",
XABuildConfig.AndroidDefaultTargetDotnetApiLevel,
},
new object[] {
"net7.0",
$"android{XABuildConfig.AndroidDefaultTargetDotnetApiLevel}",
XABuildConfig.AndroidDefaultTargetDotnetApiLevel,
},
new object[] {
"net8.0",
"android",
XABuildConfig.AndroidDefaultTargetDotnetApiLevel,
},
new object[] {
"net8.0",
$"android{XABuildConfig.AndroidDefaultTargetDotnetApiLevel}",
XABuildConfig.AndroidDefaultTargetDotnetApiLevel,
},
};
[Test]
[TestCaseSource (nameof (DotNetPackTargetFrameworks))]
public void DotNetPack (string dotnetVersion, string platform, int apiLevel)
{
var targetFramework = $"{dotnetVersion}-{platform}";
var proj = new XASdkProject (outputType: "Library") {
TargetFramework = targetFramework,
IsRelease = true,
Sources = {
new BuildItem.Source ("Foo.cs") {
TextContent = () => "public class Foo { }",
},
new AndroidItem.AndroidResource ("Resources\\raw\\bar.txt") {
BinaryContent = () => Array.Empty<byte> (),
},
new AndroidItem.AndroidLibrary ("sub\\directory\\foo.jar") {
BinaryContent = () => ResourceData.JavaSourceJarTestJar,
},
new AndroidItem.AndroidLibrary ("sub\\directory\\bar.aar") {
WebContent = "https://repo1.maven.org/maven2/com/balysv/material-menu/1.1.0/material-menu-1.1.0.aar",
},
new AndroidItem.AndroidJavaSource ("JavaSourceTest.java") {
Encoding = Encoding.ASCII,
TextContent = () =>
@"package com.xamarin.android.test.msbuildtest;
public class JavaSourceTest {
public String Say (String quote) {
return quote;
}
}",
},
},
};
proj.AddNuGetSourcesForOlderTargetFrameworks ();
if (IsPreviewFrameworkVersion (targetFramework)) {
proj.SetProperty ("EnablePreviewFeatures", "true");
}
proj.OtherBuildItems.Add (new AndroidItem.AndroidLibrary ("sub\\directory\\arm64-v8a\\libfoo.so") {
BinaryContent = () => Array.Empty<byte> (),
});
proj.OtherBuildItems.Add (new AndroidItem.AndroidNativeLibrary (default (Func<string>)) {
Update = () => "libfoo.so",
MetadataValues = "Link=x86\\libfoo.so",
BinaryContent = () => Array.Empty<byte> (),
});
proj.OtherBuildItems.Add (new AndroidItem.LibraryProjectZip ("..\\baz.aar") {
WebContent = "https://repo1.maven.org/maven2/com/balysv/material-menu/1.1.0/material-menu-1.1.0.aar",
MetadataValues = "Bind=false",
});
proj.OtherBuildItems.Add (new AndroidItem.AndroidLibrary (default (Func<string>)) {
Update = () => "nopack.aar",
WebContent = "https://repo1.maven.org/maven2/com/balysv/material-menu/1.1.0/material-menu-1.1.0.aar",
MetadataValues = "Pack=false;Bind=false",
});
var dotnet = CreateDotNetBuilder (proj);
Assert.IsTrue (dotnet.Pack (), "`dotnet pack` should succeed");
var nupkgPath = Path.Combine (FullProjectDirectory, proj.OutputPath, "..", $"{proj.ProjectName}.1.0.0.nupkg");
FileAssert.Exists (nupkgPath);
using var nupkg = ZipHelper.OpenZip (nupkgPath);
nupkg.AssertContainsEntry (nupkgPath, $"lib/{dotnetVersion}-android{apiLevel}.0/{proj.ProjectName}.dll");
nupkg.AssertContainsEntry (nupkgPath, $"lib/{dotnetVersion}-android{apiLevel}.0/{proj.ProjectName}.aar");
nupkg.AssertContainsEntry (nupkgPath, $"lib/{dotnetVersion}-android{apiLevel}.0/bar.aar");
nupkg.AssertDoesNotContainEntry (nupkgPath, "content/bar.aar");
nupkg.AssertDoesNotContainEntry (nupkgPath, "content/sub/directory/bar.aar");
nupkg.AssertDoesNotContainEntry (nupkgPath, $"contentFiles/any/{dotnetVersion}-android{apiLevel}.0/sub/directory/bar.aar");
nupkg.AssertDoesNotContainEntry (nupkgPath, $"lib/{dotnetVersion}-android{apiLevel}.0/nopack.aar");
nupkg.AssertDoesNotContainEntry (nupkgPath, "content/nopack.aar");
nupkg.AssertDoesNotContainEntry (nupkgPath, $"contentFiles/any/{dotnetVersion}-android{apiLevel}.0/nopack.aar");
//TODO: this issue is not fixed in net6.0-android MSBuild targets
if (dotnetVersion != "net6.0") {
nupkg.AssertContainsEntry (nupkgPath, $"lib/{dotnetVersion}-android{apiLevel}.0/baz.aar");
}
}
[Test]
public void DotNetLibraryAarChanges ()
{
var proj = new XASdkProject (outputType: "Library");
proj.Sources.Add (new AndroidItem.AndroidResource ("Resources\\raw\\foo.txt") {
TextContent = () => "foo",
});
proj.Sources.Add (new AndroidItem.AndroidResource ("Resources\\raw\\bar.txt") {
TextContent = () => "bar",
});
var dotnet = CreateDotNetBuilder (proj);
Assert.IsTrue (dotnet.Build (), "first build should succeed");
var aarPath = Path.Combine (FullProjectDirectory, proj.OutputPath, $"{proj.ProjectName}.aar");
FileAssert.Exists (aarPath);
using (var aar = ZipHelper.OpenZip (aarPath)) {
aar.AssertEntryContents (aarPath, "res/raw/foo.txt", contents: "foo");
aar.AssertEntryContents (aarPath, "res/raw/bar.txt", contents: "bar");
}
// Change res/raw/bar.txt contents
WaitFor (1000);
var bar_txt = Path.Combine (FullProjectDirectory, "Resources", "raw", "bar.txt");
File.WriteAllText (bar_txt, contents: "baz");
Assert.IsTrue (dotnet.Build (), "second build should succeed");
FileAssert.Exists (aarPath);
using (var aar = ZipHelper.OpenZip (aarPath)) {
aar.AssertEntryContents (aarPath, "res/raw/foo.txt", contents: "foo");
aar.AssertEntryContents (aarPath, "res/raw/bar.txt", contents: "baz");
}
// Delete res/raw/bar.txt
File.Delete (bar_txt);
Assert.IsTrue (dotnet.Build (), "third build should succeed");
FileAssert.Exists (aarPath);
using (var aar = ZipHelper.OpenZip (aarPath)) {
aar.AssertEntryContents (aarPath, "res/raw/foo.txt", contents: "foo");
aar.AssertDoesNotContainEntry (aarPath, "res/raw/bar.txt");
}
}
[Test]
public void AppWithSingleJar ()
{
var proj = new XASdkProject {
Sources = {
new AndroidItem.AndroidLibrary ("Jars\\javaclasses.jar") {
BinaryContent = () => ResourceData.JavaSourceJarTestJar,
}
}
};
var dotnet = CreateDotNetBuilder (proj);
Assert.IsTrue (dotnet.Build (), "first build should succeed");
var assemblyPath = Path.Combine (FullProjectDirectory, proj.OutputPath, $"{proj.ProjectName}.dll");
var typeName = "Com.Xamarin.Android.Test.Msbuildtest.JavaSourceJarTest";
FileAssert.Exists (assemblyPath);
using (var assembly = AssemblyDefinition.ReadAssembly (assemblyPath)) {
Assert.IsNotNull (assembly.MainModule.GetType (typeName), $"{assemblyPath} should contain {typeName}");
}
// Remove the @(AndroidLibrary) & build again
proj.Sources.RemoveAt (proj.Sources.Count - 1);
Directory.Delete (Path.Combine (FullProjectDirectory, "Jars"), recursive: true);
Assert.IsTrue (dotnet.Build (), "second build should succeed");
FileAssert.Exists (assemblyPath);
using (var assembly = AssemblyDefinition.ReadAssembly (assemblyPath)) {
Assert.IsNull (assembly.MainModule.GetType (typeName), $"{assemblyPath} should *not* contain {typeName}");
}
}
[Test]
public void GenerateResourceDesigner_false()
{
var proj = new XASdkProject (outputType: "Library") {
Sources = {
new AndroidItem.AndroidResource (() => "Resources\\drawable\\foo.png") {
BinaryContent = () => XamarinAndroidCommonProject.icon_binary_mdpi,
},
}
};
// Turn off Resource.designer.cs and remove usage of it
proj.SetProperty ("AndroidGenerateResourceDesigner", "false");
proj.MainActivity = proj.DefaultMainActivity
.Replace ("Resource.Layout.Main", "0")
.Replace ("Resource.Id.myButton", "0");
var dotnet = CreateDotNetBuilder (proj);
Assert.IsTrue (dotnet.Build(target: "CoreCompile", parameters: new string[] { "BuildingInsideVisualStudio=true" }), "Designtime build should succeed.");
var intermediate = Path.Combine (FullProjectDirectory, proj.IntermediateOutputPath);
var resource_designer_cs = Path.Combine (intermediate, "designtime", "Resource.designer.cs");
FileAssert.DoesNotExist (resource_designer_cs);
Assert.IsTrue (dotnet.Build (), "build should succeed");
resource_designer_cs = Path.Combine (intermediate, "Resource.designer.cs");
FileAssert.DoesNotExist (resource_designer_cs);
var assemblyPath = Path.Combine (FullProjectDirectory, proj.OutputPath, $"{proj.ProjectName}.dll");
FileAssert.Exists (assemblyPath);
using var assembly = AssemblyDefinition.ReadAssembly (assemblyPath);
var typeName = $"{proj.ProjectName}.Resource";
var type = assembly.MainModule.GetType (typeName);
Assert.IsNull (type, $"{assemblyPath} should *not* contain {typeName}");
}
[Test]
[Category ("SmokeTests")]
public void DotNetBuildBinding ()
{
var proj = new XASdkProject (outputType: "Library");
// Both transform files should be applied
proj.Sources.Add (new AndroidItem.TransformFile ("Transforms.xml") {
TextContent = () =>
@"<metadata>
<attr path=""/api/package[@name='com.xamarin.android.test.msbuildtest']"" name=""managedName"">FooBar</attr>
</metadata>",
});
proj.Sources.Add (new AndroidItem.TransformFile ("Transforms\\Metadata.xml") {
TextContent = () =>
@"<metadata>
<attr path=""/api/package[@managedName='FooBar']"" name=""managedName"">MSBuildTest</attr>
</metadata>",
});
proj.Sources.Add (new AndroidItem.AndroidLibrary ("javaclasses.jar") {
BinaryContent = () => ResourceData.JavaSourceJarTestJar,
});
proj.OtherBuildItems.Add (new BuildItem ("JavaSourceJar", "javaclasses-sources.jar") {
BinaryContent = () => ResourceData.JavaSourceJarTestSourcesJar,
});
proj.OtherBuildItems.Add (new AndroidItem.AndroidJavaSource ("JavaSourceTestExtension.java") {
Encoding = Encoding.ASCII,
TextContent = () => ResourceData.JavaSourceTestExtension,
Metadata = { { "Bind", "True"} },
});
var dotnet = CreateDotNetBuilder (proj);
Assert.IsTrue (dotnet.Build (), "`dotnet build` should succeed");
var assemblyPath = Path.Combine (FullProjectDirectory, proj.OutputPath, "UnnamedProject.dll");
FileAssert.Exists (assemblyPath);
using (var assembly = AssemblyDefinition.ReadAssembly (assemblyPath)) {
var typeName = "MSBuildTest.JavaSourceJarTest";
var type = assembly.MainModule.GetType (typeName);
Assert.IsNotNull (type, $"{assemblyPath} should contain {typeName}");
typeName = "MSBuildTest.JavaSourceTestExtension";
type = assembly.MainModule.GetType (typeName);
Assert.IsNotNull (type, $"{assemblyPath} should contain {typeName}");
}
}
static readonly object [] DotNetBuildSource = new object [] {
new object [] {
/* runtimeIdentifiers */ "android-arm",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ true,
},
new object [] {
/* runtimeIdentifiers */ "android-arm64",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-x86",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-x64",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm",
/* isRelease */ true,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm",
/* isRelease */ true,
/* aot */ false,
/* usesAssemblyStore */ true,
},
new object [] {
/* runtimeIdentifiers */ "android-arm",
/* isRelease */ true,
/* aot */ true,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm",
/* isRelease */ true,
/* aot */ true,
/* usesAssemblyStore */ true,
},
new object [] {
/* runtimeIdentifiers */ "android-arm64",
/* isRelease */ true,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm;android-arm64;android-x86;android-x64",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm;android-arm64;android-x86;android-x64",
/* isRelease */ false,
/* aot */ false,
/* usesAssemblyStore */ true,
},
new object [] {
/* runtimeIdentifiers */ "android-arm;android-arm64;android-x86",
/* isRelease */ true,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm;android-arm64;android-x86;android-x64",
/* isRelease */ true,
/* aot */ false,
/* usesAssemblyStore */ false,
},
new object [] {
/* runtimeIdentifiers */ "android-arm;android-arm64;android-x86;android-x64",
/* isRelease */ true,
/* aot */ false,
/* usesAssemblyStore */ true,
},
new object [] {
/* runtimeIdentifiers */ "android-arm;android-arm64;android-x86;android-x64",
/* isRelease */ true,
/* aot */ true,
/* usesAssemblyStore */ false,
},
};
[Test]
[Category ("SmokeTests")]
[TestCaseSource (nameof (DotNetBuildSource))]
public void DotNetBuild (string runtimeIdentifiers, bool isRelease, bool aot, bool usesAssemblyStore)
{
var proj = new XASdkProject {
IsRelease = isRelease,
ExtraNuGetConfigSources = {
// Microsoft.AspNetCore.Components.WebView is not in dotnet-public
"https://api.nuget.org/v3/index.json",
},
PackageReferences = {
new Package { Id = "Xamarin.AndroidX.AppCompat", Version = "1.3.1.1" },
// Using * here, so we explicitly get newer packages
new Package { Id = "Microsoft.AspNetCore.Components.WebView", Version = "6.0.0-*" },
new Package { Id = "Microsoft.Extensions.FileProviders.Embedded", Version = "6.0.0-*" },
new Package { Id = "Microsoft.JSInterop", Version = "6.0.0-*" },
new Package { Id = "System.Text.Json", Version = "6.0.0-*" },
},
Sources = {
new BuildItem ("EmbeddedResource", "Foo.resx") {
TextContent = () => InlineData.ResxWithContents ("<data name=\"CancelButton\"><value>Cancel</value></data>")
},
new BuildItem ("EmbeddedResource", "Foo.es.resx") {
TextContent = () => InlineData.ResxWithContents ("<data name=\"CancelButton\"><value>Cancelar</value></data>")
},
new AndroidItem.TransformFile ("Transforms.xml") {
// Remove two methods that introduced warnings:
// Com.Balysv.Material.Drawable.Menu.MaterialMenuView.cs(214,30): warning CS0114: 'MaterialMenuView.OnRestoreInstanceState(IParcelable)' hides inherited member 'View.OnRestoreInstanceState(IParcelable?)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
// Com.Balysv.Material.Drawable.Menu.MaterialMenuView.cs(244,56): warning CS0114: 'MaterialMenuView.OnSaveInstanceState()' hides inherited member 'View.OnSaveInstanceState()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
TextContent = () => "<metadata><remove-node path=\"/api/package[@name='com.balysv.material.drawable.menu']/class[@name='MaterialMenuView']/method[@name='onRestoreInstanceState']\" /><remove-node path=\"/api/package[@name='com.balysv.material.drawable.menu']/class[@name='MaterialMenuView']/method[@name='onSaveInstanceState']\" /></metadata>",
},
new AndroidItem.AndroidLibrary ("material-menu-1.1.0.aar") {
WebContent = "https://repo1.maven.org/maven2/com/balysv/material-menu/1.1.0/material-menu-1.1.0.aar"
},
}
};
proj.MainActivity = proj.DefaultMainActivity.Replace (": Activity", ": AndroidX.AppCompat.App.AppCompatActivity");
proj.SetProperty ("AndroidUseAssemblyStore", usesAssemblyStore.ToString ());
proj.SetProperty ("RunAOTCompilation", aot.ToString ());
proj.OtherBuildItems.Add (new AndroidItem.InputJar ("javaclasses.jar") {
BinaryContent = () => ResourceData.JavaSourceJarTestJar,
});
proj.OtherBuildItems.Add (new BuildItem ("JavaSourceJar", "javaclasses-sources.jar") {
BinaryContent = () => ResourceData.JavaSourceJarTestSourcesJar,
});
proj.OtherBuildItems.Add (new AndroidItem.AndroidJavaSource ("JavaSourceTestExtension.java") {
Encoding = Encoding.ASCII,
TextContent = () => ResourceData.JavaSourceTestExtension,
Metadata = { { "Bind", "True"} },
});
if (!runtimeIdentifiers.Contains (";")) {
proj.SetProperty (KnownProperties.RuntimeIdentifier, runtimeIdentifiers);
} else {
proj.SetProperty (KnownProperties.RuntimeIdentifiers, runtimeIdentifiers);
}
var dotnet = CreateDotNetBuilder (proj);
Assert.IsTrue (dotnet.Build (), "`dotnet build` should succeed");
dotnet.AssertHasNoWarnings ();
var outputPath = Path.Combine (FullProjectDirectory, proj.OutputPath);
var intermediateOutputPath = Path.Combine (FullProjectDirectory, proj.IntermediateOutputPath);
if (!runtimeIdentifiers.Contains (";")) {
outputPath = Path.Combine (outputPath, runtimeIdentifiers);
intermediateOutputPath = Path.Combine (intermediateOutputPath, runtimeIdentifiers);
}
var files = Directory.EnumerateFileSystemEntries (outputPath)
.Select (Path.GetFileName)
.OrderBy (f => f, StringComparer.OrdinalIgnoreCase)
.ToArray ();
IEnumerable<string> expectedFiles;
if (isRelease) {
expectedFiles = new string[] {
$"{proj.PackageName}.aab",
$"{proj.PackageName}-Signed.aab",
$"{proj.PackageName}-Signed.apk",
"es",
$"{proj.ProjectName}.dll",
$"{proj.ProjectName}.pdb",
$"{proj.ProjectName}.runtimeconfig.json",
$"{proj.ProjectName}.xml",
};
} else {
expectedFiles = new string[] {
$"{proj.PackageName}.apk",
$"{proj.PackageName}-Signed.apk",
"es",
$"{proj.ProjectName}.dll",
$"{proj.ProjectName}.pdb",
$"{proj.ProjectName}.runtimeconfig.json",
$"{proj.ProjectName}.xml",
};
}
expectedFiles = expectedFiles.OrderBy(f => f, StringComparer.OrdinalIgnoreCase);
CollectionAssert.AreEquivalent (expectedFiles, files, $"Expected: {string.Join (";", expectedFiles)}\n Found: {string.Join (";", files)}");
var assemblyPath = Path.Combine (outputPath, $"{proj.ProjectName}.dll");
FileAssert.Exists (assemblyPath);
using (var assembly = AssemblyDefinition.ReadAssembly (assemblyPath)) {
var typeName = "Com.Xamarin.Android.Test.Msbuildtest.JavaSourceJarTest";
Assert.IsNotNull (assembly.MainModule.GetType (typeName), $"{assemblyPath} should contain {typeName}");
typeName = "Com.Balysv.Material.Drawable.Menu.MaterialMenuView";
Assert.IsNotNull (assembly.MainModule.GetType (typeName), $"{assemblyPath} should contain {typeName}");
typeName = "Com.Xamarin.Android.Test.Msbuildtest.JavaSourceTestExtension";
Assert.IsNotNull (assembly.MainModule.GetType (typeName), $"{assemblyPath} should contain {typeName}");
}
var rids = runtimeIdentifiers.Split (';');
// Check AndroidManifest.xml
var manifestPath = Path.Combine (intermediateOutputPath, "android", "AndroidManifest.xml");
FileAssert.Exists (manifestPath);
var manifest = XDocument.Load (manifestPath);
XNamespace ns = "http://schemas.android.com/apk/res/android";
var uses_sdk = manifest.Root.Element ("uses-sdk");
Assert.AreEqual ("21", uses_sdk.Attribute (ns + "minSdkVersion").Value);
Assert.AreEqual (XABuildConfig.AndroidDefaultTargetDotnetApiLevel.ToString(),
uses_sdk.Attribute (ns + "targetSdkVersion").Value);
bool expectEmbeddedAssembies = !(CommercialBuildAvailable && !isRelease);
var apkPath = Path.Combine (outputPath, $"{proj.PackageName}-Signed.apk");
FileAssert.Exists (apkPath);
var helper = new ArchiveAssemblyHelper (apkPath, usesAssemblyStore, rids);
helper.AssertContainsEntry ($"assemblies/{proj.ProjectName}.dll", shouldContainEntry: expectEmbeddedAssembies);
helper.AssertContainsEntry ($"assemblies/{proj.ProjectName}.pdb", shouldContainEntry: !CommercialBuildAvailable && !isRelease);
helper.AssertContainsEntry ($"assemblies/Mono.Android.dll", shouldContainEntry: expectEmbeddedAssembies);
helper.AssertContainsEntry ($"assemblies/es/{proj.ProjectName}.resources.dll", shouldContainEntry: expectEmbeddedAssembies);
foreach (var abi in rids.Select (AndroidRidAbiHelper.RuntimeIdentifierToAbi)) {
helper.AssertContainsEntry ($"lib/{abi}/libmonodroid.so");
helper.AssertContainsEntry ($"lib/{abi}/libmonosgen-2.0.so");
if (rids.Length > 1) {
helper.AssertContainsEntry ($"assemblies/{abi}/System.Private.CoreLib.dll", shouldContainEntry: expectEmbeddedAssembies);
} else {
helper.AssertContainsEntry ("assemblies/System.Private.CoreLib.dll", shouldContainEntry: expectEmbeddedAssembies);
}
if (aot) {
helper.AssertContainsEntry ($"lib/{abi}/libaot-{proj.ProjectName}.dll.so");
helper.AssertContainsEntry ($"lib/{abi}/libaot-Mono.Android.dll.so");
}
}
}
// TODO: <uses-sdk android:minSdkVersion="32" android:targetSdkVersion="32" />
// Causes warning: D8 : warning : An API level of 32 is not supported by this compiler. Please use an API level of 31 or earlier
// Add a 32 parameter here when we get a newer version of r8.
[Test]
public void SupportedOSPlatformVersion ([Values (21, 31)] int minSdkVersion)
{
var proj = new XASdkProject {
SupportedOSPlatformVersion = minSdkVersion.ToString (),
};
// Call AccessibilityTraversalAfter from API level 22
// https://developer.android.com/reference/android/view/View#getAccessibilityTraversalAfter()
proj.MainActivity = proj.DefaultMainActivity.Replace ("button!.Click", "button!.AccessibilityTraversalAfter.ToString ();\nbutton!.Click");
var dotnet = CreateDotNetBuilder (proj);
Assert.IsTrue (dotnet.Build (), "`dotnet build` should succeed");
if (minSdkVersion < 22) {
StringAssertEx.Contains ("warning CA1416", dotnet.LastBuildOutput, "Should get warning about Android 22 API");
} else {
dotnet.AssertHasNoWarnings ();
}
var manifestPath = Path.Combine (FullProjectDirectory, proj.IntermediateOutputPath, "android", "AndroidManifest.xml");
FileAssert.Exists (manifestPath);
var manifest = XDocument.Load (manifestPath);
XNamespace ns = "http://schemas.android.com/apk/res/android";
Assert.AreEqual (minSdkVersion.ToString (), manifest.Root.Element ("uses-sdk").Attribute (ns + "minSdkVersion").Value);
}
[Test]
[Category ("SmokeTests")]
public void DotNetBuildXamarinForms ([Values (true, false)] bool useInterpreter)
{
var proj = new XamarinFormsXASdkProject ();
proj.SetProperty ("UseInterpreter", useInterpreter.ToString ());
var dotnet = CreateDotNetBuilder (proj);
Assert.IsTrue (dotnet.Build (), "`dotnet build` should succeed");
dotnet.AssertHasNoWarnings ();
}
static readonly object[] DotNetTargetFrameworks = new object[] {
new object[] {
"net6.0",
"android",
XABuildConfig.AndroidDefaultTargetDotnetApiLevel,
},
new object[] {
"net7.0",
"android",
XABuildConfig.AndroidDefaultTargetDotnetApiLevel,
},
new object[] {
"net8.0",
"android",
XABuildConfig.AndroidDefaultTargetDotnetApiLevel,
},
new object[] {
"net8.0",
$"android{XABuildConfig.AndroidDefaultTargetDotnetApiLevel}",
XABuildConfig.AndroidDefaultTargetDotnetApiLevel,
},
new object[] {
"net8.0",
XABuildConfig.AndroidLatestStableApiLevel == XABuildConfig.AndroidDefaultTargetDotnetApiLevel ? null : $"android{XABuildConfig.AndroidLatestStableApiLevel}.0",
XABuildConfig.AndroidLatestStableApiLevel,
},
new object[] {
"net8.0",
XABuildConfig.AndroidLatestUnstableApiLevel == XABuildConfig.AndroidLatestStableApiLevel ? null : $"android{XABuildConfig.AndroidLatestUnstableApiLevel}.0",
XABuildConfig.AndroidLatestUnstableApiLevel,
},
};
static bool IsPreviewFrameworkVersion (string targetFramework)
{
return (targetFramework.Contains ($"{XABuildConfig.AndroidLatestUnstableApiLevel}")
&& XABuildConfig.AndroidLatestUnstableApiLevel != XABuildConfig.AndroidLatestStableApiLevel);
}
[Test]
public void DotNetPublish ([Values (false, true)] bool isRelease, [ValueSource(nameof(DotNetTargetFrameworks))] object[] data)
{
var dotnetVersion = (string)data[0];
var platform = (string)data[1];
var apiLevel = (int)data[2];
if (string.IsNullOrEmpty (platform))
Assert.Ignore ($"Test for API level {apiLevel} was skipped as it matched the default or latest stable API level.");
var targetFramework = $"{dotnetVersion}-{platform}";
const string runtimeIdentifier = "android-arm";
var proj = new XASdkProject {
TargetFramework = targetFramework,
IsRelease = isRelease,
};
proj.AddNuGetSourcesForOlderTargetFrameworks ();
proj.SetProperty (KnownProperties.RuntimeIdentifier, runtimeIdentifier);
var preview = IsPreviewFrameworkVersion (targetFramework);
if (preview) {
proj.SetProperty ("EnablePreviewFeatures", "true");
}
var dotnet = CreateDotNetBuilder (proj);
Assert.IsTrue (dotnet.Publish (), "first `dotnet publish` should succeed");
// NOTE: Preview API levels emit XA4211
if (!preview) {
// TODO: disabled in .NET 7 due to: https://github.com/dotnet/runtime/issues/77385
if (dotnetVersion != "net7.0")
dotnet.AssertHasNoWarnings ();
}
// Only check latest TFM, as previous will come from NuGet
if (dotnetVersion == "net8.0") {
var refDirectory = Directory.GetDirectories (Path.Combine (TestEnvironment.DotNetPreviewPacksDirectory, $"Microsoft.Android.Ref.{apiLevel}")).LastOrDefault ();
var expectedMonoAndroidRefPath = Path.Combine (refDirectory, "ref", dotnetVersion, "Mono.Android.dll");
Assert.IsTrue (dotnet.LastBuildOutput.ContainsText (expectedMonoAndroidRefPath), $"Build should be using {expectedMonoAndroidRefPath}");
var runtimeApiLevel = (apiLevel == XABuildConfig.AndroidDefaultTargetDotnetApiLevel && apiLevel < XABuildConfig.AndroidLatestStableApiLevel) ? XABuildConfig.AndroidLatestStableApiLevel : apiLevel;
var runtimeDirectory = Directory.GetDirectories (Path.Combine (TestEnvironment.DotNetPreviewPacksDirectory, $"Microsoft.Android.Runtime.{runtimeApiLevel}.{runtimeIdentifier}")).LastOrDefault ();
var expectedMonoAndroidRuntimePath = Path.Combine (runtimeDirectory, "runtimes", runtimeIdentifier, "lib", dotnetVersion, "Mono.Android.dll");
Assert.IsTrue (dotnet.LastBuildOutput.ContainsText (expectedMonoAndroidRuntimePath), $"Build should be using {expectedMonoAndroidRuntimePath}");
}
var publishDirectory = Path.Combine (FullProjectDirectory, proj.OutputPath, runtimeIdentifier, "publish");
var apk = Path.Combine (publishDirectory, $"{proj.PackageName}.apk");
var apkSigned = Path.Combine (publishDirectory, $"{proj.PackageName}-Signed.apk");
// NOTE: the unsigned .apk doesn't exist when $(AndroidPackageFormats) is `aab;apk`
if (!isRelease) {
FileAssert.Exists (apk);
}
FileAssert.Exists (apkSigned);
// NOTE: $(AndroidPackageFormats) defaults to `aab;apk` in Release
if (isRelease) {
var aab = Path.Combine (publishDirectory, $"{proj.PackageName}.aab");
var aabSigned = Path.Combine (publishDirectory, $"{proj.PackageName}-Signed.aab");
FileAssert.Exists (aab);
FileAssert.Exists (aabSigned);
}
}
[Test]
public void DefaultItems ()
{
void CreateEmptyFile (params string [] paths)
{
var path = Path.Combine (FullProjectDirectory, Path.Combine (paths));
Directory.CreateDirectory (Path.GetDirectoryName (path));
File.WriteAllText (path, contents: "");
}
var proj = new XASdkProject ();
var dotnet = CreateDotNetBuilder (proj);
// Build error -> no nested sub-directories in Resources
CreateEmptyFile ("Resources", "drawable", "foo", "bar.png");
CreateEmptyFile ("Resources", "raw", "foo", "bar.png");
// Build error -> no files/directories that start with .
CreateEmptyFile ("Resources", "raw", ".DS_Store");
CreateEmptyFile ("Assets", ".DS_Store");
CreateEmptyFile ("Assets", ".svn", "foo.txt");
// Files that should work
CreateEmptyFile ("Resources", "raw", "foo.txt");
CreateEmptyFile ("Assets", "foo", "bar.txt");
Assert.IsTrue (dotnet.Build (), "`dotnet build` should succeed");
var apkPath = Path.Combine (FullProjectDirectory, proj.OutputPath, $"{proj.PackageName}-Signed.apk");
FileAssert.Exists (apkPath);
using (var apk = ZipHelper.OpenZip (apkPath)) {
apk.AssertContainsEntry (apkPath, "res/raw/foo.txt");
apk.AssertContainsEntry (apkPath, "assets/foo/bar.txt");
}
}
[Test]
public void XamarinLegacySdk ([Values ("net6.0-android32.0", "net7.0-android33.0", "net8.0-android33.0")] string dotnetTargetFramework)
{
var proj = new XASdkProject (outputType: "Library") {
Sdk = "Xamarin.Legacy.Sdk/0.2.0-alpha2",
Sources = {
new AndroidItem.AndroidLibrary ("javaclasses.jar") {
BinaryContent = () => ResourceData.JavaSourceJarTestJar,
}
}
};
proj.AddNuGetSourcesForOlderTargetFrameworks ();
using var b = new Builder ();
var legacyTargetFrameworkVersion = "13.0";
var legacyTargetFramework = $"monoandroid{legacyTargetFrameworkVersion}";
proj.SetProperty ("TargetFramework", value: "");
proj.SetProperty ("TargetFrameworks", value: $"{dotnetTargetFramework};{legacyTargetFramework}");
var dotnet = CreateDotNetBuilder (proj);
Assert.IsTrue (dotnet.Pack (), "`dotnet pack` should succeed");
var nupkgPath = Path.Combine (FullProjectDirectory, proj.OutputPath, $"{proj.ProjectName}.1.0.0.nupkg");
FileAssert.Exists (nupkgPath);
using var nupkg = ZipHelper.OpenZip (nupkgPath);
nupkg.AssertContainsEntry (nupkgPath, $"lib/{dotnetTargetFramework}/{proj.ProjectName}.dll");
nupkg.AssertContainsEntry (nupkgPath, $"lib/{legacyTargetFramework}/{proj.ProjectName}.dll");
}
[Test]
[TestCaseSource (nameof (DotNetTargetFrameworks))]
public void MauiTargetFramework (string dotnetVersion, string platform, int apiLevel)
{
if (string.IsNullOrEmpty (platform))
Assert.Ignore ($"Test for API level {apiLevel} was skipped as it matched the default or latest stable API level.");
var targetFramework = $"{dotnetVersion}-{platform}";
var library = new XASdkProject (outputType: "Library") {
TargetFramework = targetFramework,
};
library.AddNuGetSourcesForOlderTargetFrameworks ();
var preview = IsPreviewFrameworkVersion (targetFramework);
if (preview) {
library.SetProperty ("EnablePreviewFeatures", "true");
}
library.Sources.Clear ();
library.Sources.Add (new BuildItem.Source ("Foo.cs") {
TextContent = () =>
@"public abstract partial class ViewHandler<TVirtualView, TNativeView> { }
public interface IView { }
public abstract class Foo<TVirtualView, TNativeView> : ViewHandler<TVirtualView, TNativeView>
where TVirtualView : class, IView
#if ANDROID
where TNativeView : Android.Views.View
#else
where TNativeView : class
#endif
{
}",