Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Prevent `$(AndroidUseLatestPlatformSdk)…
Browse files Browse the repository at this point in the history
…` from using Unstable APIs

Fixes #1221

Currently when a user sets `$(AndroidUseLatestPlatformSdk)` it will
automatically use the Max ApiLevel. This is not always a stable
ApiLevel. It is highly possible that it will be unstable.
Users will generally want to keep to the Max Stable in this senario.

This commit alters the code so that we use the latest Max Stable
ApiLevel when using `$(AndroidUseLatestPlatformSdk)`.
However we should still allow users to use the unstable ones if
they really want to. This can be achieved in a number of ways

1. Turn off `$(AndroidUseLatestPlatformSdk)` and set the
`$(TargetFrameworkVersion)` directly.

2. Leave `$(AndroidUseLatestPlatformSdk)` on and set the
`$(TargetFrameworkVersion)` directly.

So effectviely, just manually target the unstable Api. But it
is something the user will need to think about and do manaully.
By default we will only use to the latest stable ApiLevel.
  • Loading branch information
dellis1972 committed Jan 24, 2018
1 parent 0e1d1c8 commit 133767b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,21 @@ bool ValidateApiLevels ()

if (UseLatestAndroidPlatformSdk) {
AndroidApiLevel = GetMaxInstalledApiLevel ().ToString ();
SupportedApiLevel = GetMaxSupportedApiLevel (AndroidApiLevel);
SupportedApiLevel = GetMaxStableApiLevel ().ToString ();
int maxInstalled, maxSupported = 0;
if (int.TryParse (AndroidApiLevel, out maxInstalled) && int.TryParse (SupportedApiLevel, out maxSupported) && maxInstalled > maxSupported) {
Log.LogDebugMessage ($"API Level {AndroidApiLevel} is greater than the maximum supported API level of {SupportedApiLevel}. " +
"Support for this API will be added in a future release.");
AndroidApiLevel = SupportedApiLevel;
}
if (!string.IsNullOrWhiteSpace (TargetFrameworkVersion)) {
var userSelected = MonoAndroidHelper.SupportedVersions.GetApiLevelFromFrameworkVersion (TargetFrameworkVersion);
// overwrite using user version only if it is
// above the maxStableApi and a valid apiLevel.
if (userSelected != null && userSelected > maxSupported && userSelected <= maxInstalled) {
SupportedApiLevel = userSelected.ToString ();
}
}
TargetFrameworkVersion = GetTargetFrameworkVersionFromApiLevel ();
return TargetFrameworkVersion != null;
}
Expand Down Expand Up @@ -480,6 +488,11 @@ int GetMaxInstalledApiLevel ()
return maxApiLevel;
}

int GetMaxStableApiLevel ()
{
return MonoAndroidHelper.SupportedVersions.MaxStableVersion.ApiLevel;
}

string GetMaxSupportedApiLevel (string apiLevel)
{
int level = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,52 @@ public void IfAndroidJarDoesNotExistThrowXA5207 ()
Directory.Delete (AndroidSdkDirectory, recursive: true);
}

[Test]
public void ValidateUseLatestAndroid ()
{
var path = Path.Combine ("temp", TestName);
var referencesPath = CreateFauxReferencesDirectory (Path.Combine (path, "xbuild-frameworks"), new ApiInfo[] {
new ApiInfo () { Id = 26, Level = 26, Name = "Oreo", FrameworkVersion = "v8.0", Stable = true },
new ApiInfo () { Id = 27, Level = 27, Name = "Oreo", FrameworkVersion = "v8.1", Stable = false },
});
var proj = new XamarinAndroidApplicationProject () {
IsRelease = true,
TargetFrameworkVersion = "v8.0",
UseLatestPlatformSdk = false,
};
using (var builder = CreateApkBuilder (Path.Combine (path, proj.ProjectName))) {
builder.ThrowOnBuildFailure = false;
builder.Target = "_SetLatestTargetFrameworkVersion";
Assert.True (builder.Build (proj, parameters: new string [] {
$"TargetFrameworkRootPath={referencesPath}",
}), string.Format ("Build should have succeeded"));
Assert.IsTrue (builder.LastBuildOutput.ContainsText ("_TargetFrameworkVersion=v8.0"), "_TargetFrameworkVersion should be v8.0");

proj.TargetFrameworkVersion = "v8.1";
Assert.True (builder.Build (proj, parameters: new string [] {
$"TargetFrameworkRootPath={referencesPath}",
}), string.Format ("Build should have succeeded"));
Assert.IsTrue (builder.LastBuildOutput.ContainsText ("_TargetFrameworkVersion=v8.1"), "_TargetFrameworkVersion should be v8.1");

proj.UseLatestPlatformSdk = true;
proj.TargetFrameworkVersion = "v8.1";
Assert.True (builder.Build (proj, parameters: new string [] {
$"TargetFrameworkRootPath={referencesPath}",
}), string.Format ("Build should have succeeded"));
Assert.IsTrue (builder.LastBuildOutput.ContainsText ("_TargetFrameworkVersion=v8.1"), "_TargetFrameworkVersion should be v8.1");

proj.UseLatestPlatformSdk = true;
proj.TargetFrameworkVersion = "v6.0";
Assert.True (builder.Build (proj, parameters: new string [] {
$"TargetFrameworkRootPath={referencesPath}",
}), string.Format ("Build should have succeeded"));
Assert.IsTrue (builder.LastBuildOutput.ContainsText ("_TargetFrameworkVersion=v8.0"), "_TargetFrameworkVersion should be v8.0");


}
Directory.Delete (referencesPath, recursive: true);
}

[Test]
[NonParallelizable]
public void BuildAMassiveApp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,29 @@ protected string CreateFauxAndroidSdkDirectory (string path, string buildToolsVe
return androidSdkDirectory;
}

protected string CreateFauxReferencesDirectory (string path, string[] versions)
public struct ApiInfo {
public int Id;
public int Level;
public string Name;
public string FrameworkVersion;
public bool Stable;
}

protected string CreateFauxReferencesDirectory (string path, ApiInfo[] versions)
{

string referencesDirectory = Path.Combine (Root, path);
Directory.CreateDirectory (referencesDirectory);
Directory.CreateDirectory (Path.Combine (referencesDirectory, "v1.0"));
File.WriteAllText (Path.Combine (referencesDirectory, "v1.0", "mscorlib.dll"), "");
Directory.CreateDirectory (Path.Combine (referencesDirectory, "MonoAndroid", "v1.0"));
File.WriteAllText (Path.Combine (referencesDirectory, "MonoAndroid", "v1.0", "mscorlib.dll"), "");
foreach (var v in versions){
Directory.CreateDirectory (Path.Combine (referencesDirectory, v));
Directory.CreateDirectory (Path.Combine (referencesDirectory, "MonoAndroid", v.FrameworkVersion));
Directory.CreateDirectory (Path.Combine (referencesDirectory, "MonoAndroid", v.FrameworkVersion, "RedistList"));
File.WriteAllText (Path.Combine (referencesDirectory, "MonoAndroid", v.FrameworkVersion, "MonoAndroid.dll"), "");
File.WriteAllText (Path.Combine (referencesDirectory, "MonoAndroid", v.FrameworkVersion, "AndroidApiInfo.xml"),
$"<AndroidApiInfo>\n<Id>{v.Id}</Id>\n<Level>{v.Level}</Level>\n<Name>{v.Name}</Name>\n<Version>{v.FrameworkVersion}</Version>\n<Stable>{v.Stable}</Stable>\n</AndroidApiInfo>");
File.WriteAllText (Path.Combine (referencesDirectory, "MonoAndroid", v.FrameworkVersion, "RedistList", "FrameworkList.xml"),
$"<FileList Redist=\"MonoAndroid\" Name=\"Xamarin.Android {v.FrameworkVersion} Support\" IncludeFramework=\"v1.0\"></FileList>");
}
return referencesDirectory;
}
Expand Down

0 comments on commit 133767b

Please sign in to comment.