Skip to content

Commit 1ed601d

Browse files
committed
[Xamarin.Android.Tools.AndroidSdk] Provide more logging
Context: dotnet/android#7073 Context: https://github.com/xamarin/xamarin-android/blob/fdfc4c44ba65fcff9caf809bcf2d1f1a6837b1e3/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/AndroidDependenciesTests.cs#L19-L50 Trying to figure out how xamarin-android's `AndroidDependenciesTests.InstallAndroidDependenciesTest()` *passes*; it creates an empty "SDK" directory, then builds with the `InstallAndroidDependencies` target, and on main this works, *even though* it implies that it's creating an `AndroidSdkInfo` instance with an *empty* SDK directory: if (Directory.Exists (sdkPath)) Directory.Delete (sdkPath, true); Directory.CreateDirectory (sdkPath); // `sdkPath` is not otherwise populated Yet it *passes* `ValidateAndroidSdkLocation()`: ValidateAndroidSdkLocation: `/Users/runner/work/1/a/TestRelease/06-09_22.00.22/temp/InstallAndroidDependenciesTest/android-sdk`, result=True I do not understand *how* this can be the case, as `ValidateAndroidSdkLocation()` wants a `platform-tools/adb` program, *which should not exist*, yet it validates I am thus very confused. Expand the log messages provided by `AndroidSdkBase` & co. so that we also log "where" the `loc` parameter is coming from, via a new `locator` parameter (similar to the `locator` parameter in `JdkInfo`), and update the "file check" logic so that we log the path of the detected files. This way, hopefully, I can verify that it *is* finding an `adb`, and *where that file is located*.
1 parent 20f6112 commit 1ed601d

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

src/Xamarin.Android.Tools.AndroidSdk/Sdks/AndroidSdkBase.cs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public string[] AllAndroidSdks {
2626
var dirs = new List<string?> ();
2727
dirs.Add (AndroidSdkPath);
2828
dirs.AddRange (GetAllAvailableAndroidSdks ());
29-
allAndroidSdks = dirs.Where (d => ValidateAndroidSdkLocation (d))
29+
allAndroidSdks = dirs.Where (d => ValidateAndroidSdkLocation ("AllAndroidSdks", d))
3030
.Select (d => d!)
3131
.Distinct ()
3232
.ToArray ();
@@ -102,15 +102,15 @@ public virtual void Initialize (string? androidSdkPath = null, string? androidNd
102102
NdkStack = GetExecutablePath (AndroidNdkPath, NdkStack);
103103
}
104104

105-
static string? GetValidPath (Func<string?, bool> pathValidator, string? ctorParam, Func<string?> getPreferredPath, Func<IEnumerable<string>> getAllPaths)
105+
static string? GetValidPath (Func<string, string?, bool> pathValidator, string? ctorParam, Func<string?> getPreferredPath, Func<IEnumerable<string>> getAllPaths)
106106
{
107-
if (pathValidator (ctorParam))
107+
if (pathValidator ("constructor param", ctorParam))
108108
return ctorParam;
109109
ctorParam = getPreferredPath ();
110-
if (pathValidator (ctorParam))
110+
if (pathValidator ("preferred path", ctorParam))
111111
return ctorParam;
112112
foreach (var path in getAllPaths ()) {
113-
if (pathValidator (path)) {
113+
if (pathValidator ("all paths", path)) {
114114
return path;
115115
}
116116
}
@@ -119,18 +119,18 @@ public virtual void Initialize (string? androidSdkPath = null, string? androidNd
119119

120120
string? GetValidNdkPath (string? ctorParam)
121121
{
122-
if (ValidateAndroidNdkLocation (ctorParam))
122+
if (ValidateAndroidNdkLocation ("constructor param", ctorParam))
123123
return ctorParam;
124124
if (AndroidSdkPath != null) {
125125
string bundle = FindBestNDK (AndroidSdkPath);
126-
if (Directory.Exists (bundle) && ValidateAndroidNdkLocation (bundle))
126+
if (Directory.Exists (bundle) && ValidateAndroidNdkLocation ("within Android SDK", bundle))
127127
return bundle;
128128
}
129129
ctorParam = PreferedAndroidNdkPath;
130-
if (ValidateAndroidNdkLocation (ctorParam))
130+
if (ValidateAndroidNdkLocation ("preferred path", ctorParam))
131131
return ctorParam;
132132
foreach (var path in GetAllAvailableAndroidNdks ()) {
133-
if (ValidateAndroidNdkLocation (path))
133+
if (ValidateAndroidNdkLocation ("all paths", path))
134134
return path;
135135
}
136136
return null;
@@ -255,31 +255,47 @@ IEnumerable<string> GetJavaSdkPaths ()
255255
/// <summary>
256256
/// Checks that a value is the location of an Android SDK.
257257
/// </summary>
258-
public bool ValidateAndroidSdkLocation ([NotNullWhen (true)] string? loc)
258+
public bool ValidateAndroidSdkLocation (string locator, [NotNullWhen (true)] string? loc)
259259
{
260-
bool result = !string.IsNullOrEmpty (loc) && ProcessUtils.FindExecutablesInDirectory (Path.Combine (loc, "platform-tools"), Adb).Any ();
261-
Logger (TraceLevel.Verbose, $"{nameof (ValidateAndroidSdkLocation)}: `{loc}`, result={result}");
260+
bool result = !string.IsNullOrEmpty (loc);
261+
if (result) {
262+
bool foundAdb = false;
263+
foreach (var p in ProcessUtils.FindExecutablesInDirectory (Path.Combine (loc!, "platform-tools"), Adb)) {
264+
Logger (TraceLevel.Verbose, $"{nameof (ValidateAndroidSdkLocation)}: for locator={locator}, path=`{loc}`, found adb `{p}`");
265+
foundAdb = true;
266+
}
267+
result = foundAdb;
268+
}
269+
Logger (TraceLevel.Verbose, $"{nameof (ValidateAndroidSdkLocation)}: for locator={locator}, path=`{loc}`, result={result}");
262270
return result;
263271
}
264272

265273
/// <summary>
266274
/// Checks that a value is the location of a Java SDK.
267275
/// </summary>
268-
public virtual bool ValidateJavaSdkLocation ([NotNullWhen (true)] string? loc)
276+
public virtual bool ValidateJavaSdkLocation (string locator, [NotNullWhen (true)] string? loc)
269277
{
270-
bool result = !string.IsNullOrEmpty (loc) && ProcessUtils.FindExecutablesInDirectory (Path.Combine (loc, "bin"), JarSigner).Any ();
271-
Logger (TraceLevel.Verbose, $"{nameof (ValidateJavaSdkLocation)}: `{loc}`, result={result}");
278+
bool result = !string.IsNullOrEmpty (loc);
279+
if (result) {
280+
bool foundSigner = false;
281+
foreach (var p in ProcessUtils.FindExecutablesInDirectory (Path.Combine (loc!, "bin"), JarSigner)) {
282+
Logger (TraceLevel.Verbose, $"{nameof (ValidateJavaSdkLocation)}: for locator={locator}, path=`{loc}`, found jarsigner `{p}`");
283+
foundSigner = true;
284+
}
285+
result = foundSigner;
286+
}
287+
Logger (TraceLevel.Verbose, $"{nameof (ValidateJavaSdkLocation)}: locator={locator}, path=`{loc}`, result={result}");
272288
return result;
273289
}
274290

275291
/// <summary>
276292
/// Checks that a value is the location of an Android SDK.
277293
/// </summary>
278-
public bool ValidateAndroidNdkLocation ([NotNullWhen (true)] string? loc)
294+
public bool ValidateAndroidNdkLocation (string locator, [NotNullWhen (true)] string? loc)
279295
{
280296
bool result = !string.IsNullOrEmpty (loc) &&
281297
ProcessUtils.FindExecutablesInDirectory (loc!, NdkStack).Any ();
282-
Logger (TraceLevel.Verbose, $"{nameof (ValidateAndroidNdkLocation)}: `{loc}`, result={result}");
298+
Logger (TraceLevel.Verbose, $"{nameof (ValidateAndroidNdkLocation)}: for locator={locator}, path=`{loc}`, result={result}");
283299
return result;
284300
}
285301

src/Xamarin.Android.Tools.AndroidSdk/Sdks/AndroidSdkUnix.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public override string? PreferedAndroidSdkPath {
5151
if (androidEl != null) {
5252
var path = (string?)androidEl.Attribute ("path");
5353

54-
if (ValidateAndroidSdkLocation (path))
54+
if (ValidateAndroidSdkLocation ("preferred path", path))
5555
return path;
5656
}
5757
return null;
@@ -66,7 +66,7 @@ public override string? PreferedAndroidNdkPath {
6666
if (androidEl != null) {
6767
var path = (string?)androidEl.Attribute ("path");
6868

69-
if (ValidateAndroidNdkLocation (path))
69+
if (ValidateAndroidNdkLocation ("preferred path", path))
7070
return path;
7171
}
7272
return null;
@@ -81,7 +81,7 @@ public override string? PreferedJavaSdkPath {
8181
if (javaEl != null) {
8282
var path = (string?)javaEl.Attribute ("path");
8383

84-
if (ValidateJavaSdkLocation (path))
84+
if (ValidateJavaSdkLocation ("preferred path", path))
8585
return path;
8686
}
8787
return null;

src/Xamarin.Android.Tools.AndroidSdk/Sdks/AndroidSdkWindows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ protected override IEnumerable<string> GetAllAvailableAndroidNdks ()
145145
foreach (var basePath in new string [] {xamarin_private, android_default, vs_default, vs_default32bit, vs_2017_default, cdrive_default})
146146
if (Directory.Exists (basePath))
147147
foreach (var dir in Directory.GetDirectories (basePath, "android-ndk-r*"))
148-
if (ValidateAndroidNdkLocation (dir))
148+
if (ValidateAndroidNdkLocation ("Windows known NDK path", dir))
149149
yield return dir;
150150

151151
foreach (var dir in base.GetAllAvailableAndroidNdks ()) {

0 commit comments

Comments
 (0)