Skip to content

Commit 2d4951c

Browse files
author
Stewart Miles
committed
Option to delete resolved Android libraries.
Some users want to disable the Android resolver and instead manually integrate Android dependencies into their projects. This allows users to remove all libraries added by the Android resolver via a menu item or API. Bug: 127302270 Change-Id: Ic17fe35688ef3f91d75130597b3b75a8de17b439
1 parent c5335e2 commit 2d4951c

File tree

3 files changed

+88
-20
lines changed

3 files changed

+88
-20
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ element:
230230

231231
By default the Android Resolver automatically monitors the dependencies you have
232232
specified and the `Plugins/Android` folder of your Unity project. The
233-
resolution process runs when the specified dependencies are not present in your
233+
resolution process runs when the specified dependencies are not present in your
234234
project.
235235

236236
The *auto-resolution* process can be disabled via the
@@ -241,6 +241,13 @@ Manual resolution can be performed using the following menu options:
241241
* `Assets > Play Services Resolver > Android Resolver > Resolve`
242242
* `Assets > Play Services Resolver > Android Resolver > Force Resolve`
243243

244+
## Deleting libraries
245+
246+
Resolved packages are tracked via asset labels by the Android Resolver.
247+
They can easily be deleted using the
248+
`Assets > Play Services Resolver > Android Resolver > Delete Resolved Libraries`
249+
menu item.
250+
244251
## Android Manifest Variable Processing
245252

246253
Some AAR files (for example play-services-measurement) contain variables that

source/PlayServicesResolver/src/PlayServicesResolver.cs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,20 @@ public static void Resolve(Action resolutionComplete = null,
12481248
}, false);
12491249
}
12501250

1251+
/// <summary>
1252+
/// Wait for a ManualResetEvent to complete.
1253+
/// </summary>
1254+
/// <param name="eventToPoll">Event to poll until it's complete.</param>
1255+
private static void PollManualResetEvent(ManualResetEvent eventToPoll) {
1256+
// We poll from this thread to pump the update queue.
1257+
while (true) {
1258+
RunOnMainThread.TryExecuteAll();
1259+
if (eventToPoll.WaitOne(100 /* 100ms poll interval */)) {
1260+
break;
1261+
}
1262+
}
1263+
}
1264+
12511265
/// <summary>
12521266
/// Resolve dependencies synchronously.
12531267
/// </summary>
@@ -1262,17 +1276,35 @@ public static bool ResolveSync(bool forceResolution) {
12621276
successful = success;
12631277
completeEvent.Set();
12641278
}, false);
1265-
// We poll from this thread to pump the update queue if the scheduled job isn't
1266-
// executed immediately.
1267-
while (true) {
1268-
RunOnMainThread.TryExecuteAll();
1269-
if (completeEvent.WaitOne(100 /* 100ms poll interval */)) {
1270-
break;
1271-
}
1272-
}
1279+
PollManualResetEvent(completeEvent);
12731280
return successful;
12741281
}
12751282

1283+
/// <summary>
1284+
/// Delete all resolved libraries asynchronously.
1285+
/// </summary>
1286+
/// <param name="complete">Delegate called when delete is complete.</param>
1287+
public static void DeleteResolvedLibraries(System.Action complete = null) {
1288+
RunOnMainThread.Schedule(() => {
1289+
if (Resolver.AutomaticResolutionEnabled()) {
1290+
Log("Disabling auto-resolution to prevent libraries from being " +
1291+
"resolved after deletion.", level: LogLevel.Warning);
1292+
GooglePlayServices.SettingsDialog.EnableAutoResolution = false;
1293+
}
1294+
DeleteLabeledAssets();
1295+
if (complete != null) complete();
1296+
}, 0);
1297+
}
1298+
1299+
/// <summary>
1300+
/// Delete all resolved libraries synchronously.
1301+
/// </summary>
1302+
public static void DeleteResolvedLibrariesSync() {
1303+
var completeEvent = new ManualResetEvent(false);
1304+
DeleteResolvedLibraries(complete: () => { completeEvent.Set(); });
1305+
PollManualResetEvent(completeEvent);
1306+
}
1307+
12761308
/// <summary>
12771309
/// Schedule resolution of dependencies. If resolution is currently active this queues up
12781310
/// the requested resolution action to execute when the current resolution is complete.
@@ -1504,6 +1536,14 @@ public static void MenuForceResolve() {
15041536
ExecuteMenuResolve(true);
15051537
}
15061538

1539+
/// <summary>
1540+
/// Add a menu item to clear all resolved libraries.
1541+
/// </summary>
1542+
[MenuItem("Assets/Play Services Resolver/Android Resolver/Delete Resolved Libraries")]
1543+
public static void MenuDeleteResolvedLibraries() {
1544+
DeleteResolvedLibrariesSync();
1545+
}
1546+
15071547
/// <summary>
15081548
/// Called when settings change.
15091549
/// </summary>

source/PlayServicesResolver/test/resolve_async/Assets/PlayServicesResolver/Editor/TestResolveAsync.cs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,15 @@ static TestResolveAsync() {
164164
Name = "SetupDependencies",
165165
Method = (testCase, testCaseComplete) => {
166166
ClearAllDependencies();
167-
SetupDependencies(testCase, testCaseComplete);
167+
SetupDependencies();
168168
testCaseComplete(new TestCaseResult(testCase));
169169
}
170170
},
171171
new TestCase {
172172
Name = "ResolveForGradleBuildSystem",
173173
Method = (testCase, testCaseComplete) => {
174174
ClearAllDependencies();
175-
SetupDependencies(testCase, testCaseComplete);
175+
SetupDependencies();
176176
Resolve("Gradle", false, "ExpectedArtifacts/NoExport/Gradle",
177177
null, testCase, testCaseComplete);
178178
}
@@ -181,7 +181,7 @@ static TestResolveAsync() {
181181
Name = "ResolveForGradleBuildSystemSync",
182182
Method = (testCase, testCaseComplete) => {
183183
ClearAllDependencies();
184-
SetupDependencies(testCase, testCaseComplete);
184+
SetupDependencies();
185185
Resolve("Gradle", false, "ExpectedArtifacts/NoExport/Gradle",
186186
null, testCase, testCaseComplete, synchronous: true);
187187
}
@@ -190,7 +190,7 @@ static TestResolveAsync() {
190190
Name = "ResolveForInternalBuildSystem",
191191
Method = (testCase, testCaseComplete) => {
192192
ClearAllDependencies();
193-
SetupDependencies(testCase, testCaseComplete);
193+
SetupDependencies();
194194
Resolve("Internal", false,
195195
AarsWithNativeLibrariesSupported ?
196196
"ExpectedArtifacts/NoExport/InternalNativeAars" :
@@ -202,7 +202,7 @@ static TestResolveAsync() {
202202
Name = "ResolveForGradleBuildSystemAndExport",
203203
Method = (testCase, testCaseComplete) => {
204204
ClearAllDependencies();
205-
SetupDependencies(testCase, testCaseComplete);
205+
SetupDependencies();
206206
Resolve("Gradle", true, "ExpectedArtifacts/Export/Gradle",
207207
null, testCase, testCaseComplete);
208208
}
@@ -211,7 +211,7 @@ static TestResolveAsync() {
211211
Name = "ResolveAddedDependencies",
212212
Method = (testCase, testCaseComplete) => {
213213
ClearAllDependencies();
214-
SetupDependencies(testCase, testCaseComplete);
214+
SetupDependencies();
215215
UpdateAdditionalDependenciesFile(true);
216216
Resolve("Gradle", true, "ExpectedArtifacts/Export/GradleAddedDeps",
217217
null, testCase, testCaseComplete);
@@ -221,14 +221,38 @@ static TestResolveAsync() {
221221
Name = "ResolveRemovedDependencies",
222222
Method = (testCase, testCaseComplete) => {
223223
ClearAllDependencies();
224-
SetupDependencies(testCase, testCaseComplete);
224+
SetupDependencies();
225225
// Add the additional dependencies file then immediately remove it.
226226
UpdateAdditionalDependenciesFile(true);
227227
UpdateAdditionalDependenciesFile(false);
228228
Resolve("Gradle", true, "ExpectedArtifacts/Export/Gradle",
229229
null, testCase, testCaseComplete);
230230
}
231231
},
232+
new TestCase {
233+
Name = "DeleteResolvedLibraries",
234+
Method = (testCase, testCaseComplete) => {
235+
ClearAllDependencies();
236+
SetupDependencies();
237+
Resolve("Gradle", true, "ExpectedArtifacts/Export/Gradle",
238+
null, testCase, (testCaseResult) => {
239+
Google.VersionHandler.InvokeStaticMethod(
240+
AndroidResolverClass, "DeleteResolvedLibrariesSync", null);
241+
var unexpectedFilesMessage = new List<string>();
242+
var resolvedFiles = ListFiles("Assets/Plugins/Android");
243+
if (resolvedFiles.Count > 0) {
244+
unexpectedFilesMessage.Add("Libraries not deleted!");
245+
foreach (var filename in resolvedFiles.Values) {
246+
unexpectedFilesMessage.Add(filename);
247+
}
248+
}
249+
testCaseComplete(new TestCaseResult(testCase) {
250+
ErrorMessages = unexpectedFilesMessage
251+
});
252+
},
253+
synchronous: true);
254+
}
255+
}
232256
});
233257

234258
// Test resolution with Android ABI filtering.
@@ -497,10 +521,7 @@ private static void ClearAllDependencies() {
497521
/// NOTE: This is the deprecated way of adding dependencies and will likely be removed in
498522
/// future.
499523
/// </summary>
500-
/// <param name="testCase">Object executing this method.</param>
501-
/// <param name="testCaseComplete">Called when the test is complete.</param>
502-
private static void SetupDependencies(TestCase testCase,
503-
Action<TestCaseResult> testCaseComplete) {
524+
private static void SetupDependencies() {
504525
Google.VersionHandler.InvokeInstanceMethod(
505526
AndroidResolverSupport, "DependOn",
506527
new object[] { "com.google.firebase", "firebase-common", "16.0.0" });

0 commit comments

Comments
 (0)