Skip to content

Commit 55ba747

Browse files
dellis1972jonpryor
authored andcommitted
[Xamarin.Android.Build.Tasks] Add XA1006, XA1007, XA1008 warnings. (#2050)
Fixes: #1768 Google has the [following guidance][0] for these three values: > `minSdkVersion` (lowest possible) <= `targetSdkVersion` && > `targetSdkVersion` == `compileSdkVersion` (latest SDK) This commit adds a new `<CheckGoogleSdkRequirements/>` Task which is responsible for checking these values. It will be enabled by default but the task can be disabled via the `$(AndroidEnableGooglePlayStoreChecks)` property. When `$(AndroidEnableGooglePlayStoreChecks)` is True (the default), then the following warnings can be generated: * `XA1006`: `compileSdkVersion`/`$(TargetSdkVersion)` is greater-than `targetSdkVersion`. * `XA1007`: `minSdkVersion` is greater-than `targetSdkVersion`. * `XA1008`: `compileSdk`/`$(TargetSdkVersion)` is less-than `targetSdkVersion`. [0]: https://medium.com/androiddevelopers/picking-your-compilesdkversion-minsdkversion-targetsdkversion-a098a0341ebd
1 parent 241d941 commit 55ba747

14 files changed

+218
-0
lines changed

Documentation/guides/BuildProcess.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,14 @@ when packaing Release applications.
778778
779779
Added in Xamarin.Android 9.2.
780780
781+
- **AndroidEnableGooglePlayStoreChecks** &ndash; A bool property
782+
which allows developers to disable the following Google Play
783+
Store checks, XA1004, XA1005 and XA1006. This is useful for
784+
developers who are not targeting the Google Play Store and do
785+
not wish to run those checks.
786+
787+
Added in Xamarin.Android 9.4.
788+
781789
### Binding Project Build Properties
782790
783791
The following MSBuild properties are used with

Documentation/guides/messages/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
+ [XA1003](xa1003.md): '{zip}' does not exist. Please rebuild the project.
5454
+ [XA1004](xa1004.md): There was an error opening {filename}. The file is probably corrupt. Try deleting it and building again.
5555
+ [XA1005](xa1005.md): Attempting naive type name fixup for element with ID '{id}' and type '{managedType}'
56+
+ [XA1006](xa1006.md): Your application is running on a version of Android ({compileSdk}) that is more recent than your targetSdkVersion specifies ({targetSdk}). Set your targetSdkVersion to the highest version of Android available to match your TargetFrameworkVersion ({compileSdk}).
57+
+ [XA1007](xa1007.md): The minSdkVersion ({minSdk}) is greater than targetSdkVersion. Please change the value such that minSdkVersion is less than or equal to targetSdkVersion ({targetSdk}).
58+
+ [XA1008](xa1008.md): The TargetFrameworkVersion ({compileSdk}) should not be lower than targetSdkVersion ({targetSdk})
5659

5760
### XA2xxx Linker
5861

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Compiler Warning XA1006
2+
3+
You are building against a version of Android (compileSdk) that
4+
is more recent than your targetSdkVersion specifies (targetSdk).
5+
6+
Set your targetSdkVersion to the highest version of Android available
7+
to match your TargetFrameworkVersion (compileSdk).
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Compiler Warning XA1007
2+
3+
The minSdkVersion (minSdk) is greater than targetSdkVersion.
4+
5+
Please change the value such that minSdkVersion is less than
6+
or equal to targetSdkVersion (targetSdk).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Compiler Warning XA1008
2+
3+
The TargetFrameworkVersion (compileSdk) must not be lower
4+
than targetSdkVersion (targetSdk).
5+
6+
You should either, increase the `$(TargetFrameworkVersion)`
7+
of your project. Or decrease the `android:targetSdkVersion`
8+
in your `AndroidManifest.xml` to correct this issue.

Novell/MonoDroid.FSharp.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Project>
2+
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.FSharp.targets" />
3+
</Project>
4+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
3+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Project>
2+
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.Common.targets" />
3+
</Project>
4+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.Build.Utilities;
4+
using Microsoft.Build.Framework;
5+
using System.IO;
6+
using System.Linq;
7+
8+
using Java.Interop.Tools.Cecil;
9+
using Xamarin.Android.Tools;
10+
11+
namespace Xamarin.Android.Tasks
12+
{
13+
public class CheckGoogleSdkRequirements : Task
14+
{
15+
[Required]
16+
public string TargetFrameworkVersion { get; set; }
17+
18+
[Required]
19+
public string ManifestFile { get; set; }
20+
21+
public override bool Execute ()
22+
{
23+
ManifestDocument manifest = new ManifestDocument (ManifestFile, this.Log);
24+
25+
var compileSdk = MonoAndroidHelper.SupportedVersions.GetApiLevelFromFrameworkVersion (TargetFrameworkVersion);
26+
27+
if (!int.TryParse (manifest.GetMinimumSdk (), out int minSdk)) {
28+
minSdk = 1;
29+
}
30+
if (!int.TryParse (manifest.GetTargetSdk (), out int targetSdk)) {
31+
targetSdk = compileSdk.Value;
32+
}
33+
34+
//We should throw a warning if the targetSdkVersion is lower than compileSdkVersion(TargetFrameworkVersion).
35+
if (targetSdk < compileSdk) {
36+
Log.LogCodedWarning ("XA1006",
37+
$"You are building against version of Android ({compileSdk}) that is more recent than your targetSdkVersion specifies ({targetSdk}). Set your targetSdkVersion to the highest version of Android available to match your TargetFrameworkVersion ({compileSdk}).");
38+
}
39+
//We should throw an warning if the compileSdkVersion(TargetFrameworkVersion) is lower than targetSdkVersion.
40+
if (compileSdk < targetSdk) {
41+
Log.LogCodedWarning ("XA1008",
42+
$"The TargetFrameworkVersion ({compileSdk}) must not be lower than targetSdkVersion ({targetSdk}). You should either, increase the `$(TargetFrameworkVersion)` of your project. Or decrease the `android:targetSdkVersion` in your `AndroidManifest.xml` to correct this issue.");
43+
}
44+
//We should throw an warning if the minSdkVersion is greater than targetSdkVers1ion.
45+
if (minSdk > targetSdk) {
46+
Log.LogCodedWarning ("XA1007",
47+
$"The minSdkVersion ({minSdk}) is greater than targetSdkVersion. Please change the value such that minSdkVersion is less than or equal to targetSdkVersion ({targetSdk}).");
48+
}
49+
50+
return !Log.HasLoggedErrors;
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using Microsoft.Build.Framework;
2+
using NUnit.Framework;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.IO;
6+
using Xamarin.Android.Tasks;
7+
using Xamarin.Android.Tools;
8+
using Xamarin.ProjectTools;
9+
10+
namespace Xamarin.Android.Build.Tests {
11+
[TestFixture]
12+
public class CheckGoogleSdkRequirementsTests : BaseTest {
13+
List<BuildErrorEventArgs> errors;
14+
List<BuildWarningEventArgs> warnings;
15+
MockBuildEngine engine;
16+
17+
[SetUp]
18+
public void Setup ()
19+
{
20+
var path = Path.Combine ("temp", TestName);
21+
engine = new MockBuildEngine (TestContext.Out, errors = new List<BuildErrorEventArgs> (), warnings = new List<BuildWarningEventArgs> ());
22+
var referencePath = CreateFauxReferencesDirectory (Path.Combine (path, "references"), new [] {
23+
new ApiInfo { Id = "27", Level = 27, Name = "Oreo", FrameworkVersion = "v8.1", Stable = true },
24+
new ApiInfo { Id = "28", Level = 28, Name = "Pie", FrameworkVersion = "v9.0", Stable = true },
25+
});
26+
MonoAndroidHelper.RefreshSupportedVersions (new [] {
27+
Path.Combine (referencePath, "MonoAndroid"),
28+
});
29+
}
30+
31+
[TearDown]
32+
public void TearDown ()
33+
{
34+
var path = Path.Combine ("temp", TestName);
35+
Directory.Delete (Path.Combine (Root, path), recursive: true);
36+
}
37+
38+
string CreateManiestFile (int minSDk, int targetSdk) {
39+
var manifest = Path.Combine (Root, "temp", TestName, "AndroidManifest.xml");
40+
File.WriteAllText (manifest, string.Format (@"<manifest xmlns:android='http://schemas.android.com/apk/res/android' android:versionCode='1' android:versionName='1.0' package='Foo.Foo'>
41+
<uses-sdk android:minSdkVersion = '{0}' android:targetSdkVersion = '{1}' />
42+
</manifest>
43+
", minSDk, targetSdk));
44+
return manifest;
45+
}
46+
47+
[Test]
48+
public void CheckManifestIsOK ()
49+
{
50+
var task = new CheckGoogleSdkRequirements () {
51+
BuildEngine = engine,
52+
TargetFrameworkVersion = "v9.0",
53+
ManifestFile = CreateManiestFile (10, 28),
54+
};
55+
Assert.True (task.Execute (), "Task should have succeeded.");
56+
Assert.AreEqual (0, errors.Count, "There should be 0 errors reported.");
57+
Assert.AreEqual (0, warnings.Count, "There should be 0 warnings reported.");
58+
}
59+
60+
[Test]
61+
public void CheckManifestTargetSdkLowerThanCompileSdk ()
62+
{
63+
var task = new CheckGoogleSdkRequirements () {
64+
BuildEngine = engine,
65+
TargetFrameworkVersion = "v9.0",
66+
ManifestFile = CreateManiestFile (10, 27),
67+
};
68+
Assert.True (task.Execute (), "Task should have succeeded.");
69+
Assert.AreEqual (0, errors.Count, "There should be 0 errors reported.");
70+
Assert.AreEqual (1, warnings.Count, "There should be 1 warning reported.");
71+
}
72+
73+
[Test]
74+
public void CheckManifestCompileSdkLowerThanTargetSdk ()
75+
{
76+
var task = new CheckGoogleSdkRequirements () {
77+
BuildEngine = engine,
78+
TargetFrameworkVersion = "v8.1",
79+
ManifestFile = CreateManiestFile (10, 28),
80+
};
81+
Assert.True (task.Execute (), "Task should have succeeded.");
82+
Assert.AreEqual (0, errors.Count, "There should be 1 error reported.");
83+
Assert.AreEqual (1, warnings.Count, "There should be 0 warnings reported.");
84+
}
85+
86+
[Test]
87+
public void CheckManifestMinSdkLowerThanTargetSdk ()
88+
{
89+
var task = new CheckGoogleSdkRequirements () {
90+
BuildEngine = engine,
91+
TargetFrameworkVersion = "v8.1",
92+
ManifestFile = CreateManiestFile (28, 27),
93+
};
94+
Assert.True (task.Execute (), "Task should have succeeded.");
95+
Assert.AreEqual (0, errors.Count, "There should be 0 error reported.");
96+
Assert.AreEqual (1, warnings.Count, "There should be 1 warnings reported.");
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)