Skip to content

Commit 74e3668

Browse files
committed
Add Net Standard Support
1 parent b16ee55 commit 74e3668

File tree

11 files changed

+452
-160
lines changed

11 files changed

+452
-160
lines changed

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

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,133 @@ public void CheckIncludedNativeLibraries ()
181181
}
182182
}
183183
}
184+
185+
[Test]
186+
public void NetStandardReferenceTest ()
187+
{
188+
var netStandardProject = new DotNetStandard () {
189+
Language = XamarinAndroidProjectLanguage.CSharp,
190+
ProjectName = "XamFormsSample",
191+
ProjectGuid = Guid.NewGuid ().ToString (),
192+
Sdk = "Microsoft.NET.Sdk",
193+
TargetFramework = "netstandard1.4",
194+
PackageTargetFallback = "portable-net45+win8+wpa81+wp8",
195+
Packages = {
196+
KnownPackages.XamarinFormsPCL_2_3_4_231,
197+
new Package () {
198+
Id = "System.IO.Packaging",
199+
Version = "4.4.0",
200+
},
201+
// Uncomment when https://bugzilla.xamarin.com/show_bug.cgi?id=59313 gets fixed
202+
// new Package () {
203+
// Id = "Newtonsoft.Json",
204+
// Version = "10.0.3"
205+
// },
206+
},
207+
OtherBuildItems = {
208+
new BuildItem ("None") {
209+
Remove = () => "**\\*.xaml",
210+
},
211+
new BuildItem ("Compile") {
212+
Update = () => "**\\*.xaml.cs",
213+
DependentUpon = () => "%(Filename)"
214+
},
215+
new BuildItem ("EmbeddedResource") {
216+
Include = () => "**\\*.xaml",
217+
SubType = () => "Designer",
218+
Generator = () => "MSBuild:UpdateDesignTimeXaml",
219+
},
220+
},
221+
Sources = {
222+
new BuildItem.Source ("App.xaml.cs") {
223+
TextContent = () => @"using System;
224+
using System.Collections.Generic;
225+
using System.Linq;
226+
using System.Text;
227+
//using Newtonsoft.Json;
228+
229+
using Xamarin.Forms;
230+
231+
namespace XamFormsSample
232+
{
233+
public partial class App : Application
234+
{
235+
public App()
236+
{
237+
// Uncomment when https://bugzilla.xamarin.com/show_bug.cgi?id=59313 gets fixed
238+
// JsonConvert.DeserializeObject<string>(""test"");
239+
InitializeComponent();
240+
}
241+
242+
protected override void OnStart()
243+
{
244+
// Handle when your app starts
245+
}
246+
247+
protected override void OnSleep()
248+
{
249+
// Handle when your app sleeps
250+
}
251+
252+
protected override void OnResume()
253+
{
254+
// Handle when your app resumes
255+
}
256+
}
257+
}",
258+
},
259+
new BuildItem.Source ("App.xaml") {
260+
TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8"" ?>
261+
<Application xmlns=""http://xamarin.com/schemas/2014/forms""
262+
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
263+
x:Class=""XamFormsSample.App"">
264+
<Application.Resources>
265+
<!-- Application resource dictionary -->
266+
</Application.Resources>
267+
</Application>",
268+
},
269+
},
270+
};
271+
272+
var app = new XamarinAndroidApplicationProject () {
273+
ProjectName = "App1",
274+
IsRelease = false,
275+
UseLatestPlatformSdk = true,
276+
References = {
277+
new BuildItem.Reference ("Mono.Android.Export"),
278+
new BuildItem.ProjectReference ($"..\\{netStandardProject.ProjectName}\\{netStandardProject.ProjectName}.csproj",
279+
netStandardProject.ProjectName, netStandardProject.ProjectGuid),
280+
},
281+
Packages = {
282+
KnownPackages.SupportDesign_25_4_0_1,
283+
KnownPackages.SupportV7CardView_24_2_1,
284+
KnownPackages.AndroidSupportV4_25_4_0_1,
285+
KnownPackages.SupportCoreUtils_25_4_0_1,
286+
KnownPackages.SupportMediaCompat_25_4_0_1,
287+
KnownPackages.SupportFragment_25_4_0_1,
288+
KnownPackages.SupportCoreUI_25_4_0_1,
289+
KnownPackages.SupportCompat_25_4_0_1,
290+
KnownPackages.SupportV7AppCompat_25_4_0_1,
291+
KnownPackages.XamarinForms_2_3_4_231,
292+
}
293+
};
294+
app.SetProperty (KnownProperties.AndroidSupportedAbis, "x86;armeabi-v7a");
295+
var path = Path.Combine ("temp", TestContext.CurrentContext.Test.Name);
296+
using (var builder = CreateDllBuilder (Path.Combine (path, netStandardProject.ProjectName))) {
297+
builder.RequiresMSBuild = true;
298+
builder.Target = "Restore";
299+
Assert.IsTrue (builder.Build (netStandardProject), "XamFormsSample Nuget packages should have been restored.");
300+
builder.Target = "Build";
301+
Assert.IsTrue (builder.Build (netStandardProject), "XamFormsSample should have built.");
302+
using (var ab = CreateApkBuilder (Path.Combine (path, app.ProjectName))) {
303+
ab.RequiresMSBuild = true;
304+
Assert.IsTrue (ab.Build (app), "App should have built.");
305+
if (!HasDevices)
306+
Assert.Ignore ("Skipping Installation. No devices available.");
307+
ab.Target = "Install";
308+
Assert.IsTrue (ab.Build (app), "App should have installed.");
309+
}
310+
}
311+
}
184312
}
185313
}

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownPackages.cs

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static class KnownPackages
6666
new BuildItem.Reference ("Xamarin.Android.Support.v13") {
6767
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.v13.20.0.0.4\\lib\\MonoAndroid32\\Xamarin.Android.Support.v13.dll" }
6868
}
69-
};
69+
};
7070
public static Package AndroidSupportV13Beta = new Package () {
7171
Id = "Xamarin.Android.Support.v13",
7272
Version = "21.0.0.0-beta1",
@@ -93,20 +93,20 @@ public static class KnownPackages
9393
new BuildItem.Reference ("Xamarin.Android.Wearable") {
9494
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Wear.1.0.0-preview7\\lib\\MonoAndroid10\\Xamarin.Android.Wearable.dll" }
9595
}
96-
};
96+
};
9797
public static Package SupportV7RecyclerView = new Package {
9898
Id = "Xamarin.Android.Support.v7.RecyclerView",
99-
Version="21.0.0.0-beta1",
100-
TargetFramework="MonoAndroid523",
99+
Version = "21.0.0.0-beta1",
100+
TargetFramework = "MonoAndroid523",
101101
References = {
102102
new BuildItem.Reference ("Xamarin.Android.Support.V7.RecyclerView") {
103103
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.v7.RecyclerView.21.0.0.0-beta1\\lib\\MonoAndroid\\Xamarin.Android.Support.v7.RecyclerView.dll" }
104104
}
105-
};
105+
};
106106
public static Package SupportV7CardView = new Package {
107107
Id = "Xamarin.Android.Support.v7.Cardview",
108-
Version="21.0.3.0",
109-
TargetFramework="MonoAndroid523",
108+
Version = "21.0.3.0",
109+
TargetFramework = "MonoAndroid523",
110110
References = {
111111
new BuildItem.Reference ("Xamarin.Android.Support.v7.CardView") {
112112
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.v7.CardView.21.0.3.0\\lib\\MonoAndroid403\\Xamarin.Android.Support.v7.CardView.dll" }
@@ -121,6 +121,15 @@ public static class KnownPackages
121121
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.v7.CardView.24.2.1\\lib\\MonoAndroid70\\Xamarin.Android.Support.v7.CardView.dll" }
122122
}
123123
};
124+
public static Package SupportV7CardView_25_4_0_1 = new Package {
125+
Id = "Xamarin.Android.Support.v7.Cardview",
126+
Version = "25.4.0.1",
127+
TargetFramework = "MonoAndroid70",
128+
References = {
129+
new BuildItem.Reference ("Xamarin.Android.Support.v7.CardView") {
130+
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.v7.CardView.25.4.0.1\\lib\\MonoAndroid70\\Xamarin.Android.Support.v7.CardView.dll" }
131+
}
132+
};
124133
public static Package SupportV7AppCompat_21_0_3_0 = new Package {
125134
Id = "Xamarin.Android.Support.v7.AppCompat",
126135
Version = "21.0.3.0",
@@ -220,6 +229,15 @@ public static class KnownPackages
220229
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.v7.Palette.22.1.1.1\\lib\\MonoAndroid403\\Xamarin.Android.Support.v7.Palette.dll" }
221230
}
222231
};
232+
public static Package SupportDesign_25_4_0_1 = new Package {
233+
Id = "Xamarin.Android.Support.Design",
234+
Version = "25.4.0.1",
235+
TargetFramework = "MonoAndroid70",
236+
References = {
237+
new BuildItem.Reference ("Xamarin.Android.Support.Design") {
238+
MetadataValues = "HintPath=..\\packages\\Xamarin.Android.Support.Design.25.4.0.1\\lib\\MonoAndroid70\\Xamarin.Android.Support.Design.dll" }
239+
}
240+
};
223241
public static Package GooglePlayServices_22_0_0_2 = new Package {
224242
Id = "Xamarin.GooglePlayServices",
225243
Version = "22.0.0.2",
@@ -265,6 +283,41 @@ public static class KnownPackages
265283
},
266284
}
267285
};
286+
public static Package XamarinFormsPCL_2_3_4_231 = new Package {
287+
Id = "Xamarin.Forms",
288+
Version = "2.3.4.231",
289+
TargetFramework = "portable-net45+win+wp80+MonoAndroid10+xamarinios10+MonoTouch10",
290+
References = {
291+
new BuildItem.Reference ("Xamarin.Forms.Core") {
292+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\\Xamarin.Forms.Core.dll"
293+
},
294+
new BuildItem.Reference ("Xamarin.Forms.Xaml") {
295+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\\Xamarin.Forms.Xaml.dll"
296+
},
297+
}
298+
};
299+
public static Package XamarinForms_2_3_4_231 = new Package {
300+
Id = "Xamarin.Forms",
301+
Version = "2.3.4.231",
302+
TargetFramework = "MonoAndroid44",
303+
References = {
304+
new BuildItem.Reference ("Xamarin.Forms.Platform.Android") {
305+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\MonoAndroid10\\Xamarin.Forms.Platform.Android.dll"
306+
},
307+
new BuildItem.Reference ("FormsViewGroup") {
308+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\MonoAndroid10\\FormsViewGroup.dll"
309+
},
310+
new BuildItem.Reference ("Xamarin.Forms.Core") {
311+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\MonoAndroid10\\Xamarin.Forms.Core.dll"
312+
},
313+
new BuildItem.Reference ("Xamarin.Forms.Xaml") {
314+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\MonoAndroid10\\Xamarin.Forms.Xaml.dll"
315+
},
316+
new BuildItem.Reference ("Xamarin.Forms.Platform") {
317+
MetadataValues = "HintPath=..\\packages\\Xamarin.Forms.2.3.4.231\\lib\\MonoAndroid10\\Xamarin.Forms.Platform.dll"
318+
},
319+
}
320+
};
268321
public static Package CocosSharp_PCL_Shared_1_5_0_0 = new Package {
269322
Id = "CocosSharp.PCL.Shared",
270323
Version = "1.5.0.0",

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinAndroidProject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Xamarin.ProjectTools
99
{
10-
public abstract class XamarinAndroidProject : XamarinProject
10+
public abstract class XamarinAndroidProject : DotNetXamarinProject
1111
{
1212
protected XamarinAndroidProject (string debugConfigurationName = "Debug", string releaseConfigurationName = "Release")
1313
: base (debugConfigurationName, releaseConfigurationName)

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/BuildItem.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,29 @@ public BuildItem (string buildAction, string include)
7878
{
7979
}
8080

81-
public BuildItem (string buildAction, Func<string> include)
81+
public BuildItem (string buildAction, Func<string> include = null)
8282
{
8383
BuildAction = buildAction;
8484
Include = include;
8585
Metadata = new Dictionary<string, string> ();
8686
Timestamp = DateTimeOffset.UtcNow;
8787
Encoding = Encoding.UTF8;
8888
Attributes = FileAttributes.Normal;
89+
Generator = null;
90+
Remove = null;
91+
SubType = null;
92+
Update = null;
93+
DependentUpon = null;
8994
}
9095

9196
public DateTimeOffset? Timestamp { get; set; }
9297
public string BuildAction { get; set; }
9398
public Func<string> Include { get; set; }
99+
public Func<string> Remove { get; set; }
100+
public Func<string> Update { get; set; }
101+
public Func<string> SubType { get; set; }
102+
public Func<string> Generator { get; set; }
103+
public Func<string> DependentUpon { get; set; }
94104
public IDictionary<string,string> Metadata { get; private set; }
95105
public Func<string> TextContent { get; set; }
96106
public Func<byte[]> BinaryContent { get; set; }

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/Builder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ public class Builder : IDisposable
2323
public TimeSpan LastBuildTime { get; protected set; }
2424
public string BuildLogFile { get; set; }
2525
public bool ThrowOnBuildFailure { get; set; }
26+
public bool RequiresMSBuild { get; set; }
2627

2728
string GetUnixBuildExe ()
2829
{
2930
RunningMSBuild = false;
3031
var tooldir = Directory.Exists (fixed_osx_xbuild_path) ? fixed_osx_xbuild_path : fixed_linux_xbuild_path;
3132
string path = Path.Combine (tooldir, xbuildapp);
32-
if (!string.IsNullOrEmpty (Environment.GetEnvironmentVariable ("USE_MSBUILD"))) {
33+
if (!string.IsNullOrEmpty (Environment.GetEnvironmentVariable ("USE_MSBUILD")) || RequiresMSBuild) {
3334
path = Path.Combine (tooldir, msbuildapp);
3435
RunningMSBuild = true;
3536
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Xamarin.ProjectTools
7+
{
8+
public class DotNetStandard : XamarinProject
9+
{
10+
11+
public override string ProjectTypeGuid {
12+
get {
13+
return string.Empty;
14+
}
15+
}
16+
17+
public DotNetStandard ()
18+
{
19+
Sources = new List<BuildItem> ();
20+
OtherBuildItems = new List<BuildItem> ();
21+
SetProperty (CommonProperties, "DebugType", "full");
22+
ItemGroupList.Add (Sources);
23+
}
24+
public string PackageTargetFallback {
25+
get { return GetProperty ("PackageTargetFallback"); }
26+
set { SetProperty ("PackageTargetFallback", value); }
27+
}
28+
public string TargetFramework {
29+
get { return GetProperty ("TargetFramework"); }
30+
set { SetProperty ("TargetFramework", value); }
31+
}
32+
public string Sdk { get; set; }
33+
34+
public IList<BuildItem> OtherBuildItems { get; private set; }
35+
public IList<BuildItem> Sources { get; private set; }
36+
37+
public override string SaveProject ()
38+
{
39+
var sb = new StringBuilder ();
40+
sb.AppendLine ("\t<PropertyGroup>");
41+
foreach (var pg in PropertyGroups) {
42+
if (!pg.Properties.Any ())
43+
continue;
44+
foreach (var p in pg.Properties) {
45+
var conditon = string.IsNullOrEmpty (p.Condition) ? "" : $" Conditon=\"{p.Condition}\"";
46+
sb.AppendLine ($"\t\t<{p.Name}{conditon}>{p.Value ()}</{p.Name}>");
47+
}
48+
}
49+
sb.AppendLine ("\t</PropertyGroup>");
50+
sb.AppendLine ("\t<ItemGroup>");
51+
foreach (var pr in Packages) {
52+
sb.AppendLine ($"\t\t<PackageReference Include=\"{pr.Id}\" Version=\"{pr.Version}\"/>");
53+
}
54+
sb.AppendLine ("\t</ItemGroup>");
55+
sb.AppendLine ("\t<ItemGroup>");
56+
foreach (var bi in OtherBuildItems) {
57+
sb.Append ($"\t\t<{bi.BuildAction} ");
58+
if (bi.Include != null) sb.Append ($"Include=\"{bi.Include ()}\" ");
59+
if (bi.Update != null) sb.Append ($"Update=\"{bi.Update ()}\" ");
60+
if (bi.Remove != null) sb.Append ($"Remove=\"{bi.Remove ()}\" ");
61+
if (bi.Generator != null) sb.Append ($"Generator=\"{bi.Generator ()}\" ");
62+
if (bi.DependentUpon != null) sb.Append ($"DependentUpon=\"{bi.DependentUpon ()}\" ");
63+
if (bi.SubType != null) sb.Append ($"SubType=\"{bi.SubType ()}\" ");
64+
if (bi.Metadata.Any ()) {
65+
sb.AppendLine ($"\t\t/>");
66+
} else {
67+
sb.AppendLine ($">");
68+
foreach (var kvp in bi.Metadata) {
69+
sb.AppendLine ($"\t\t\t<{kvp.Key}>{kvp.Value}</{kvp.Key}>");
70+
}
71+
sb.AppendLine ($"\t\t</{bi.BuildAction}>");
72+
}
73+
}
74+
sb.AppendLine ("\t</ItemGroup>");
75+
return $"<Project Sdk=\"{Sdk}\">\r\n{sb.ToString ()}\r\n</Project>";
76+
}
77+
78+
public override void NuGetRestore (string directory, string packagesDirectory = null)
79+
{
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)