forked from dotnet/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[One .NET]
dotnet new
project and item templates (dotnet#5348)
Context: https://docs.microsoft.com/dotnet/core/tutorials/cli-templates-create-template-pack Context: https://github.com/dotnet/templating/wiki Context: dotnet/designs#120 This implements basic Android templates that are contained in a `Microsoft.Android.Templates.nupkg` file. We can define a template pack in `WorkloadManifest.json`: "Microsoft.Android.Templates": { "kind": "template", "version": "@TEMPLATE_PACK_VERSION@" } This allows the workload to locate the `*.nupkg` file in: C:\Program Files\dotnet\template-packs /usr/local/share/dotnet/template-packs Our .NET 6 preview installers simply have to place the `*.nupkg` files in the right place for them to be picked up by `dotnet new`. Some example project templates: dotnet new android --output MyAndroidApp --packageName com.mycompany.myandroidapp dotnet new androidlib --output MyAndroidLibrary dotnet new android-bindinglib --output MyJavaBinding And item templates: dotnet new android-activity --name LoginActivity --namespace MyAndroidApp dotnet new android-layout --name MyLayout --output Resources/layout Note that the `android-bindinglib` template is not a special project type. It has additional help files for writing bindings as we have in the current Xamarin.Android templates. I also updated the `XASdkTests` to `dotnet new` each template and `dotnet build` the resulting output. Finally, the `<GenerateWixFile/>` task (68be8d8) will now add `<Directory/>` entries such as: <Directory Id="packs" Name="packs" FileSource="C:\Users\myuser\android-toolchain\dotnet\packs"> and: <Directory Id="templatepacks" Name="template-packs" FileSource="C:\Users\myuser\android-toolchain\dotnet\template-packs"> <Component Id="SC983E605827BDDA589CA4DFF082E29CDFEC2CD059B4AF3C01421CC95568A8D74"> <File Id="SC983E605827BDDA589CA4DFF082E29CDFEC2CD059B4AF3C01421CC95568A8D74" Name="Microsoft.Android.Templates.11.0.100-ci.dotnet-new.259.nupkg" KeyPath="yes" /> </Component> </Directory> In the latter case, MSI has weird rules around the `Id` value. Consequently, file hashes were used.
- Loading branch information
1 parent
a74fe4a
commit e346f84
Showing
48 changed files
with
512 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project> | ||
<Import Project="..\..\build-tools\scripts\XAVersionInfo.targets" /> | ||
<PropertyGroup> | ||
<BeforePack> | ||
_GetDefaultPackageVersion; | ||
$(BeforePack); | ||
</BeforePack> | ||
</PropertyGroup> | ||
<Target Name="_GetDefaultPackageVersion" | ||
DependsOnTargets="GetXAVersionInfo" > | ||
<PropertyGroup> | ||
<PackageVersion>$(AndroidPackVersionLong)+sha.$(XAVersionHash)</PackageVersion> | ||
</PropertyGroup> | ||
</Target> | ||
</Project> |
23 changes: 23 additions & 0 deletions
23
src/Microsoft.Android.Templates/Microsoft.Android.Templates.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<PackageType>Template</PackageType> | ||
<PackageId>Microsoft.Android.Templates</PackageId> | ||
<Title>.NET Android Templates</Title> | ||
<Authors>Microsoft</Authors> | ||
<Description>Templates for Android platforms.</Description> | ||
<IncludeContentInPack>true</IncludeContentInPack> | ||
<IncludeBuildOutput>false</IncludeBuildOutput> | ||
<ContentTargetFolders>content</ContentTargetFolders> | ||
<OutputPath>..\..\bin\Build$(Configuration)\nupkgs\</OutputPath> | ||
</PropertyGroup> | ||
|
||
<Import Project="..\..\Configuration.props" /> | ||
|
||
<ItemGroup> | ||
<Content Include="**\*" Exclude="**\bin\**;**\obj\**" /> | ||
<Compile Remove="**\*" /> | ||
</ItemGroup> | ||
|
||
</Project> |
25 changes: 25 additions & 0 deletions
25
src/Microsoft.Android.Templates/android-activity/.template.config/template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/template", | ||
"author": "Microsoft", | ||
"classifications": [ "Android" ], | ||
"name": "Android Activity template", | ||
"description": "An Android Activity class", | ||
"tags": { | ||
"language": "C#", | ||
"type": "item" | ||
}, | ||
"identity": "Microsoft.Android.AndroidActivity", | ||
"shortName": "android-activity", | ||
"sourceName": "Activity1", | ||
"primaryOutputs": [ | ||
{ "path": "Activity1.cs" } | ||
], | ||
"defaultName": "Activity1", | ||
"symbols": { | ||
"namespace": { | ||
"description": "namespace for the generated code", | ||
"replaces": "AndroidApp1", | ||
"type": "parameter" | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Microsoft.Android.Templates/android-activity/Activity1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Android.App; | ||
using Android.OS; | ||
using Android.Runtime; | ||
using Android.Widget; | ||
|
||
namespace AndroidApp1 | ||
{ | ||
[Activity(Label = "@string/app_name", MainLauncher = true)] | ||
public class Activity1 : Activity | ||
{ | ||
protected override void OnCreate(Bundle savedInstanceState) | ||
{ | ||
base.OnCreate(savedInstanceState); | ||
|
||
// Create your application here | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Microsoft.Android.Templates/android-bindinglib/.template.config/template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/template", | ||
"author": "Microsoft", | ||
"classifications": [ "Android" ], | ||
"identity": "Microsoft.Android.AndroidBinding", | ||
"name": "Android Java Library Binding", | ||
"description": "A project for creating an Android class library that binds to a native Java library", | ||
"shortName": "android-bindinglib", | ||
"tags": { | ||
"language": "C#", | ||
"type": "project" | ||
}, | ||
"sourceName": "AndroidBinding1", | ||
"preferNameDirectory": true, | ||
"primaryOutputs": [ | ||
{ "path": "AndroidBinding1.csproj" } | ||
], | ||
"defaultName": "AndroidBinding1" | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Microsoft.Android.Templates/android-bindinglib/Additions/AboutAdditions.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
Additions allow you to add arbitrary C# to the generated classes | ||
before they are compiled. This can be helpful for providing convenience | ||
methods or adding pure C# classes. | ||
|
||
== Adding Methods to Generated Classes == | ||
|
||
Let's say the library being bound has a Rectangle class with a constructor | ||
that takes an x and y position, and a width and length size. It will look like | ||
this: | ||
|
||
public partial class Rectangle | ||
{ | ||
public Rectangle (int x, int y, int width, int height) | ||
{ | ||
// JNI bindings | ||
} | ||
} | ||
|
||
Imagine we want to add a constructor to this class that takes a Point and | ||
Size structure instead of 4 ints. We can add a new file called Rectangle.cs | ||
with a partial class containing our new method: | ||
|
||
public partial class Rectangle | ||
{ | ||
public Rectangle (Point location, Size size) : | ||
this (location.X, location.Y, size.Width, size.Height) | ||
{ | ||
} | ||
} | ||
|
||
At compile time, the additions class will be added to the generated class | ||
and the final assembly will a Rectangle class with both constructors. | ||
|
||
|
||
== Adding C# Classes == | ||
|
||
Another thing that can be done is adding fully C# managed classes to the | ||
generated library. In the above example, let's assume that there isn't a | ||
Point class available in Java or our library. The one we create doesn't need | ||
to interact with Java, so we'll create it like a normal class in C#. | ||
|
||
By adding a Point.cs file with this class, it will end up in the binding library: | ||
|
||
public class Point | ||
{ | ||
public int X { get; set; } | ||
public int Y { get; set; } | ||
} |
6 changes: 6 additions & 0 deletions
6
src/Microsoft.Android.Templates/android-bindinglib/AndroidBinding1.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0-android</TargetFramework> | ||
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidBinding1</RootNamespace> | ||
</PropertyGroup> | ||
</Project> |
14 changes: 14 additions & 0 deletions
14
src/Microsoft.Android.Templates/android-bindinglib/Transforms/EnumFields.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<enum-field-mappings> | ||
<!-- | ||
This example converts the constants Fragment_id, Fragment_name, | ||
and Fragment_tag from android.support.v4.app.FragmentActivity.FragmentTag | ||
to an enum called Android.Support.V4.App.FragmentTagType with values | ||
Id, Name, and Tag. | ||
<mapping jni-class="android/support/v4/app/FragmentActivity$FragmentTag" clr-enum-type="Android.Support.V4.App.FragmentTagType"> | ||
<field jni-name="Fragment_name" clr-name="Name" value="0" /> | ||
<field jni-name="Fragment_id" clr-name="Id" value="1" /> | ||
<field jni-name="Fragment_tag" clr-name="Tag" value="2" /> | ||
</mapping> | ||
--> | ||
</enum-field-mappings> |
13 changes: 13 additions & 0 deletions
13
src/Microsoft.Android.Templates/android-bindinglib/Transforms/EnumMethods.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<enum-method-mappings> | ||
<!-- | ||
This example changes the Java method: | ||
android.support.v4.app.Fragment.SavedState.writeToParcel (int flags) | ||
to be: | ||
android.support.v4.app.Fragment.SavedState.writeToParcel (Android.OS.ParcelableWriteFlags flags) | ||
when bound in C#. | ||
<mapping jni-class="android/support/v4/app/Fragment.SavedState"> | ||
<method jni-name="writeToParcel" parameter="flags" clr-enum-type="Android.OS.ParcelableWriteFlags" /> | ||
</mapping> | ||
--> | ||
</enum-method-mappings> |
9 changes: 9 additions & 0 deletions
9
src/Microsoft.Android.Templates/android-bindinglib/Transforms/Metadata.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<metadata> | ||
<!-- | ||
This sample removes the class: android.support.v4.content.AsyncTaskLoader.LoadTask: | ||
<remove-node path="/api/package[@name='android.support.v4.content']/class[@name='AsyncTaskLoader.LoadTask']" /> | ||
This sample removes the method: android.support.v4.content.CursorLoader.loadInBackground: | ||
<remove-node path="/api/package[@name='android.support.v4.content']/class[@name='CursorLoader']/method[@name='loadInBackground']" /> | ||
--> | ||
</metadata> |
Oops, something went wrong.