Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public class GenerateJavaStubs : AndroidTask

public string SupportedOSPlatformVersion { get; set; }

public string LibraryMinSdk { get; set; } = string.Empty;

public ITaskItem[] Environments { get; set; }

[Output]
Expand Down Expand Up @@ -345,6 +347,7 @@ void Run (DirectoryAssemblyResolver res, bool useMarshalMethods)
SdkDir = AndroidSdkDir,
TargetSdkVersion = AndroidSdkPlatform,
MinSdkVersion = minSdkVersion,
LibraryMinSdkVersion = LibraryMinSdk,
Debug = Debug,
MultiDex = MultiDex,
NeedsInternet = NeedsInternet,
Expand Down
27 changes: 20 additions & 7 deletions src/Xamarin.Android.Build.Tasks/Tasks/GetImportedLibraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
Expand All @@ -22,7 +23,7 @@ public class GetImportedLibraries : AndroidTask
[Required]
public string TargetDirectory { get; set; }

public string CacheFile { get; set;}
public string CacheFile { get; set;}

[Output]
public ITaskItem [] Jars { get; set; }
Expand All @@ -33,6 +34,9 @@ public class GetImportedLibraries : AndroidTask
[Output]
public ITaskItem [] ManifestDocuments { get; set; }

[Output]
public string LibraryMinSdk { get; set; } = string.Empty;

public override bool RunTask ()
{
if (!Directory.Exists (TargetDirectory)) {
Expand All @@ -43,6 +47,7 @@ public override bool RunTask ()
var manifestDocuments = new List<ITaskItem> ();
var nativeLibraries = new List<ITaskItem> ();
var jarFiles = new List<ITaskItem> ();
int libraryMinSdk = -1;
foreach (var file in Directory.EnumerateFiles (TargetDirectory, "*", SearchOption.AllDirectories)) {
if (file.EndsWith (".so", StringComparison.OrdinalIgnoreCase)) {
if (AndroidRidAbiHelper.GetNativeLibraryAbi (file) != null)
Expand All @@ -55,11 +60,23 @@ public override bool RunTask ()
var directory = Path.GetFileName (Path.GetDirectoryName (file));
if (IgnoredManifestDirectories.Contains (directory))
continue;
manifestDocuments.Add (new TaskItem (file));
var item = new TaskItem (file);
XDocument doc = XDocument.Load (file);
var minAttr = doc.Root.Element ("uses-sdk")?.Attribute (ManifestDocument.AndroidXmlNamespace + "minSdkVersion");
if (minAttr != null) {
if (int.TryParse (minAttr.Value, out int minSDK))
libraryMinSdk = Math.Max (libraryMinSdk, minSDK);
}
manifestDocuments.Add (item);
}
}
}

XAttribute minSdk = null;
if (libraryMinSdk != -1) {
LibraryMinSdk = libraryMinSdk.ToString ();
minSdk = new XAttribute ("libraryMinSdk", LibraryMinSdk);
}
ManifestDocuments = manifestDocuments.ToArray ();
NativeLibraries = nativeLibraries.ToArray ();
Jars = jarFiles.ToArray ();
Expand All @@ -68,17 +85,13 @@ public override bool RunTask ()
var document = new XDocument (
new XDeclaration ("1.0", "UTF-8", null),
new XElement ("Paths",
new XElement ("ManifestDocuments", ManifestDocuments.Select(e => new XElement ("ManifestDocument", e.ItemSpec))),
new XElement ("ManifestDocuments", minSdk, ManifestDocuments.Select(e => new XElement ("ManifestDocument", e.ItemSpec))),
new XElement ("NativeLibraries", NativeLibraries.Select(e => new XElement ("NativeLibrary", e.ItemSpec))),
new XElement ("Jars", Jars.Select(e => new XElement ("Jar", e.ItemSpec)))
));
document.SaveIfChanged (CacheFile);
}

Log.LogDebugTaskItems (" NativeLibraries: ", NativeLibraries);
Log.LogDebugTaskItems (" Jars: ", Jars);
Log.LogDebugTaskItems (" ManifestDocuments: ", ManifestDocuments);

return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//
//
// ReadImportedLibrariesCache.cs
//
//
// Author:
// Dean Ellis <dean.ellis@xamarin.com>
//
//
// Copyright (c) 2013 Xamarin Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -39,7 +39,7 @@ public class ReadImportedLibrariesCache : AndroidTask
public override string TaskPrefix => "RIL";

[Required]
public string CacheFile { get; set;}
public string CacheFile { get; set;}

[Output]
public ITaskItem [] Jars { get; set; }
Expand All @@ -50,6 +50,9 @@ public class ReadImportedLibrariesCache : AndroidTask
[Output]
public ITaskItem [] ManifestDocuments { get; set; }

[Output]
public string LibraryMinSdk { get; set; }

public override bool RunTask ()
{
if (!File.Exists (CacheFile)) {
Expand All @@ -60,10 +63,11 @@ public override bool RunTask ()
Jars = doc.GetPathsAsTaskItems ("Jars", "Jar");
NativeLibraries = doc.GetPathsAsTaskItems ("NativeLibraries", "NativeLibrary");
ManifestDocuments = doc.GetPathsAsTaskItems ("ManifestDocuments", "ManifestDocument");

Log.LogDebugTaskItems (" NativeLibraries: ", NativeLibraries);
Log.LogDebugTaskItems (" Jars: ", Jars);
Log.LogDebugTaskItems (" ManifestDocuments: ", ManifestDocuments);
var minSdk = doc.Element ("Paths")?.Element ("ManifestDocuments")?.Attribute ("libraryMinSdk");
LibraryMinSdk = string.Empty;
if (minSdk != null)
if (int.TryParse (minSdk.Value, out int minSDK))
LibraryMinSdk = minSdk.Value;

return !Log.HasLoggedErrors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,33 @@ public void ModifyManifest ([Values (true, false)] bool isRelease)
}
}

[Test]
public void EmptyUsesSdkElementBuildsOK ([Values (false, true)] bool addReference)
{
var proj = new XamarinAndroidApplicationProject () {
IsRelease = true,
};
if (addReference)
proj.PackageReferences.Add (KnownPackages.AndroidXSecurityCrypto);
proj.AndroidManifest = @"<?xml version=""1.0"" encoding=""utf-8""?>
<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" xmlns:tools=""http://schemas.android.com/tools"" android:versionCode=""1"" android:versionName=""1.0"" package=""foo.foo"">
<uses-sdk />
<application android:label=""foo"">
</application>
</manifest>";
using (var b = CreateApkBuilder ($"temp/{TestName}", cleanupAfterSuccessfulBuild: true, cleanupOnDispose: false)) {
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
var manifestFile = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, "android", "AndroidManifest.xml");
var doc = XDocument.Load (manifestFile);
var ns = XNamespace.Get ("http://schemas.android.com/apk/res/android");
var usesSdk = doc.Element ("manifest")?.Element ("uses-sdk");
Assert.IsNotNull (usesSdk, "Should have found a uses-sdk element.");
var expected = addReference ? "23" : "19";
Assert.AreEqual (expected, usesSdk.Attribute (ns + "minSdkVersion")?.Value, $"minSdkVersion should have been '{expected}' but was '{usesSdk.Attribute ("minSdkVersion")?.Value}'");

}
}

[Test]
public void MergeLibraryManifest ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@ public static class KnownPackages
}
}
};
public static Package AndroidXSecurityCrypto = new Package {
Id = "Xamarin.AndroidX.Security.SecurityCrypto",
Version = "1.0.0.9",
TargetFramework = "MonoAndroid90",
References = {
new BuildItem.Reference("Xamarin.AndroidX.Security.SecurityCrypto") {
MetadataValues = "HintPath=..\\packages\\Xamarin.AndroidX.Security.SecurityCrypto.1.0.0.9\\lib\\MonoAndroid90\\Xamarin.AndroidX.Security.SecurityCrypto.dll"
}
}
};
public static Package XamarinGoogleAndroidMaterial = new Package {
Id = "Xamarin.Google.Android.Material",
Version = "1.0.0.1",
Expand Down
16 changes: 13 additions & 3 deletions src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ internal class ManifestDocument
public string SdkDir { get; set; }
public string TargetSdkVersion { get; set; }
public string MinSdkVersion { get; set; }
public string LibraryMinSdkVersion { get; set; } = string.Empty;
public bool Debug { get; set; }
public bool MultiDex { get; set; }
public bool NeedsInternet { get; set; }
Expand Down Expand Up @@ -119,13 +120,17 @@ public string VersionCode {

public string GetMinimumSdk () {
int defaultMinSdkVersion = MonoAndroidHelper.SupportedVersions.MinStableVersion.ApiLevel;
Console.WriteLine ($"DEBUG GetMinimumSdk: {defaultMinSdkVersion}");
var minAttr = doc.Root.Element ("uses-sdk")?.Attribute (androidNs + "minSdkVersion");
if (minAttr == null) {
int minSdkVersion;
Console.WriteLine ($"DEBUG GetMinimumSdk: Parsing {MinSdkVersionName}");
if (!int.TryParse (MinSdkVersionName, out minSdkVersion))
minSdkVersion = defaultMinSdkVersion;
Console.WriteLine ($"DEBUG GetMinimumSdk: Using {Math.Min (minSdkVersion, defaultMinSdkVersion)}");
return Math.Min (minSdkVersion, defaultMinSdkVersion).ToString ();
}
Console.WriteLine ($"DEBUG GetMinimumSdk: Found {minAttr.Value}");
return minAttr.Value;
}

Expand Down Expand Up @@ -309,10 +314,15 @@ public IList<string> Merge (TaskLoggingHelper log, TypeDefinitionCache cache, Li

if (uses.Attribute (androidNs + "minSdkVersion") == null) {
int minSdkVersion;
if (!int.TryParse (MinSdkVersionName, out minSdkVersion))
if (!int.TryParse (MinSdkVersionName, out minSdkVersion)) {
minSdkVersion = XABuildConfig.NDKMinimumApiAvailable;
minSdkVersion = Math.Min (minSdkVersion, XABuildConfig.NDKMinimumApiAvailable);
uses.SetAttributeValue (androidNs + "minSdkVersion", minSdkVersion.ToString ());
}
if (!string.IsNullOrEmpty (LibraryMinSdkVersion) && int.TryParse (LibraryMinSdkVersion, out minSdkVersion)) {
uses.SetAttributeValue (androidNs + "minSdkVersion", minSdkVersion.ToString ());
} else {
minSdkVersion = Math.Min (minSdkVersion, XABuildConfig.NDKMinimumApiAvailable);
uses.SetAttributeValue (androidNs + "minSdkVersion", minSdkVersion.ToString ());
}
}

string targetSdkVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,7 @@ because xbuild doesn't support framework reference assemblies.
LinkingEnabled="$(_LinkingEnabled)"
HaveMultipleRIDs="$(_HaveMultipleRIDs)"
IntermediateOutputDirectory="$(IntermediateOutputPath)"
LibraryMinSdk="$(_LibraryMinSdk)"
Environments="@(AndroidEnvironment);@(LibraryEnvironments)">
<Output TaskParameter="GeneratedBinaryTypeMaps" ItemName="_AndroidTypeMapping" Condition=" '$(_InstantRunEnabled)' == 'True' " />
</GenerateJavaStubs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ This file is used by all project types, including binding projects.
<Output TaskParameter="NativeLibraries" ItemName="ExtractedNativeLibraryImports" />
<Output TaskParameter="NativeLibraries" ItemName="AndroidNativeLibrary" />
<Output TaskParameter="ManifestDocuments" ItemName="ExtractedManifestDocuments" />
<Output TaskParameter="LibraryMinSdk" PropertyName="_LibraryMinSdk" />
</ReadImportedLibrariesCache>
</Target>

Expand Down