Skip to content

Commit a66013b

Browse files
[One .NET] implement $(SupportedPlatformOSVersion) for minSdkVersion (#6111)
Context: #6107 (comment) In .NET 6, we are wanting `AndroidManifest.xml` in project templates to no longer contain: <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" /> This is because we can define these values such as: <TargetFramework>net6.0-android30</TargetFramework> <SupportedPlatformOSVersion>21.0</SupportedPlatformOSVersion> There is not really a reason to put this information in two places. `$(SupportedPlatformOSVersion)` is also required for enabling C# compiler warnings such as: MainActivity.cs(28,4): warning CA1416: This call site is reachable on: 'Android' 21.0 and later. 'View.AccessibilityTraversalAfter.get' is only supported on: 'android' 22.0 and later. This is an example of calling an API from 22, with a `$(SupportedPlatformOSVersion)` for 21. If `<uses-sdk/>` is still *used* in `AndroidManifest.xml` in a project, it will be preferred as the final source of truth. One other improvement is that `$(SupportedPlatformOSVersion)` is expected to include `.0`, or you get the warning: UnnamedProject.AssemblyInfo.cs(21,12): warning CA1418: Version '21' is not valid for platform 'Android'. Use a version with 2-4 parts for this platform. Android unique in that this value is always an integer. In order for users to be able to specify `21`, I added a check in `Microsoft.Android.Sdk.DefaultProperties.targets` so that we append `.0` when needed. I added some tests around this scenario.
1 parent d2a31d1 commit a66013b

File tree

17 files changed

+102
-38
lines changed

17 files changed

+102
-38
lines changed

src/Microsoft.Android.Templates/android-bindinglib/.template.config/template.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,14 @@
1515
"primaryOutputs": [
1616
{ "path": "AndroidBinding1.csproj" }
1717
],
18+
"symbols": {
19+
"supportedOSVersion": {
20+
"type": "parameter",
21+
"description": "Overrides $(SupportedOSPlatformVersion) in the project",
22+
"datatype": "string",
23+
"replaces": "SUPPORTED_OS_PLATFORM_VERSION",
24+
"defaultValue": "21"
25+
}
26+
},
1827
"defaultName": "AndroidBinding1"
1928
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net6.0-android</TargetFramework>
4+
<SupportedOSPlatformVersion>SUPPORTED_OS_PLATFORM_VERSION</SupportedOSPlatformVersion>
45
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidBinding1</RootNamespace>
56
</PropertyGroup>
67
</Project>

src/Microsoft.Android.Templates/android/.template.config/template.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
"description": "Overrides the package name in the AndroidManifest.xml",
2929
"datatype": "string",
3030
"replaces": "com.companyname.AndroidApp1"
31+
},
32+
"supportedOSVersion": {
33+
"type": "parameter",
34+
"description": "Overrides $(SupportedOSPlatformVersion) in the project",
35+
"datatype": "string",
36+
"replaces": "SUPPORTED_OS_PLATFORM_VERSION",
37+
"defaultValue": "21"
3138
}
3239
},
3340
"defaultName": "AndroidApp1"

src/Microsoft.Android.Templates/android/AndroidApp1.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net6.0-android</TargetFramework>
4+
<SupportedOSPlatformVersion>SUPPORTED_OS_PLATFORM_VERSION</SupportedOSPlatformVersion>
45
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidApp1</RootNamespace>
56
<OutputType>Exe</OutputType>
67
</PropertyGroup>

src/Microsoft.Android.Templates/android/AndroidManifest.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
android:versionCode="1"
44
android:versionName="1.0"
55
package="com.companyname.AndroidApp1">
6-
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
76
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true">
87
</application>
98
</manifest>

src/Microsoft.Android.Templates/androidlib/.template.config/template.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,14 @@
1515
"primaryOutputs": [
1616
{ "path": "AndroidLib1.csproj" }
1717
],
18+
"symbols": {
19+
"supportedOSVersion": {
20+
"type": "parameter",
21+
"description": "Overrides $(SupportedOSPlatformVersion) in the project",
22+
"datatype": "string",
23+
"replaces": "SUPPORTED_OS_PLATFORM_VERSION",
24+
"defaultValue": "21"
25+
}
26+
},
1827
"defaultName": "AndroidLib1"
1928
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net6.0-android</TargetFramework>
4+
<SupportedOSPlatformVersion>SUPPORTED_OS_PLATFORM_VERSION</SupportedOSPlatformVersion>
45
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidLib1</RootNamespace>
56
</PropertyGroup>
67
</Project>

src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Designer.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Copyright (C) 2016 Xamarin. All rights reserved.
9999
FrameworkDirectories="$(_XATargetFrameworkDirectories);$(_XATargetFrameworkDirectories)Facades"
100100
AcwMapFile="$(_AcwMapFile)"
101101
SupportedAbis="$(_BuildTargetAbis)"
102+
SupportedOSPlatformVersion="$(SupportedOSPlatformVersion)"
102103
InstantRunEnabled="$(_InstantRunEnabled)">
103104
</GenerateJavaStubs>
104105
<ManifestMerger

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
-->
2424
<_GetChildProjectCopyToPublishDirectoryItems>false</_GetChildProjectCopyToPublishDirectoryItems>
2525
<UseMonoRuntime Condition=" '$(UseMonoRuntime)' == '' ">true</UseMonoRuntime>
26+
<!-- $(SupportedOSPlatformVersion) must be '21.0', but we should support integer values like '21' -->
27+
<SupportedOSPlatformVersion Condition=" '$(SupportedOSPlatformVersion)' != '' and !$(SupportedOSPlatformVersion.Contains('.')) ">$(SupportedOSPlatformVersion).0</SupportedOSPlatformVersion>
2628

2729
<!-- Bindings properties -->
2830
<!-- jar2xml is not supported -->

src/Xamarin.Android.Build.Tasks/Tasks/Aapt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ protected string GenerateCommandLineCommands (string ManifestFile, string curren
263263
Directory.CreateDirectory (manifestDir);
264264
manifestFile = Path.Combine (manifestDir, Path.GetFileName (ManifestFile));
265265
ManifestDocument manifest = new ManifestDocument (ManifestFile);
266-
manifest.SdkVersion = AndroidSdkPlatform;
266+
manifest.TargetSdkVersion = AndroidSdkPlatform;
267267
if (!string.IsNullOrEmpty (VersionCodePattern)) {
268268
try {
269269
manifest.CalculateVersionCode (currentAbi, VersionCodePattern, VersionCodeProperties);

0 commit comments

Comments
 (0)