Skip to content

Commit 04feff1

Browse files
author
Stewart Miles
committed
Added option to display the set of Android libraries in the project.
The new menu item `Assets > Play Services Resolver > Android Resolver > Display Libraries` shows output that can be merged into a gradle file like this: allprojects { repositories { maven { url "file:////Users/unity/auth/client/unity/testapp/Assets/Firebase/m2repository" // Assets/Firebase/Editor/AppDependencies.xml:23, Assets/Firebase/Editor/AuthDependencies.xml:23 } } } dependencies { implementation 'com.google.auto.value:auto-value-annotations:1.6.3' // Assets/Firebase/Editor/AppDependencies.xml:25 implementation 'com.google.firebase:firebase-app-unity:5.6.0' // Assets/Firebase/Editor/AppDependencies.xml:23 implementation 'com.google.firebase:firebase-auth:16.2.0' // Assets/Firebase/Editor/AuthDependencies.xml:16 implementation 'com.google.firebase:firebase-auth-unity:5.6.0' // Assets/Firebase/Editor/AuthDependencies.xml:23 implementation 'com.google.firebase:firebase-common:16.1.0' // Assets/Firebase/Editor/AppDependencies.xml:16 implementation 'com.google.firebase:firebase-core:16.0.7' // Assets/Firebase/Editor/AuthDependencies.xml:18 } Bug: 127302270 Change-Id: I255098e9e715bcba78253bf396e6e8091c9dc252
1 parent 2d4951c commit 04feff1

File tree

5 files changed

+266
-58
lines changed

5 files changed

+266
-58
lines changed

README.md

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ dependencies on Android) provides solutions for each of these problems.
3636
## Android Dependency Management
3737

3838
The *Android Resolver* component of this plugin will download and integrate
39-
Android library dependencies and handle any conflicts between plugins that share
39+
Android library dependencies and handle any conflicts between plugins that share
4040
the same dependencies.
4141

42-
Without the Android Resolver, typically Unity plugins bundle their AAR and
42+
Without the Android Resolver, typically Unity plugins bundle their AAR and
4343
JAR dependencies, e.g. a Unity plugin `SomePlugin` that requires the Google
4444
Play Games Android library would redistribute the library and its transitive
4545
dependencies in the folder `SomePlugin/Android/`. When a user imports
4646
`SomeOtherPlugin` that includes the same libraries (potentially at different
4747
versions) in `SomeOtherPlugin/Android/`, the developer using `SomePlugin` and
48-
`SomeOtherPlugin` will see an error when building for Android that can be hard
48+
`SomeOtherPlugin` will see an error when building for Android that can be hard
4949
to interpret.
5050

5151
Using the Android Resolver to manage Android library dependencies:
@@ -102,7 +102,7 @@ always be the version activated in a developer's Unity project.
102102

103103
Plugin creators are encouraged to adopt this library to ease integration for
104104
their customers. For more information about integrating Play Services Resolver
105-
into your own plugin, see the [Plugin Redistribution](#plugin-redistribution)
105+
into your own plugin, see the [Plugin Redistribution](#plugin-redistribution)
106106
section of this document.
107107

108108
# Requirements
@@ -115,34 +115,34 @@ depends upon the `PluginImporter` UnityEditor API.
115115

116116
# Getting Started
117117

118-
Before you import the Play Services Resolver into your plugin project, you first
118+
Before you import the Play Services Resolver into your plugin project, you first
119119
need to consider whether you intend to *redistribute* Play Services Resolver
120120
along with your own plugin.
121121

122-
Redistributing the `Play Services Resolver` inside your own plugin will ease
122+
Redistributing the `Play Services Resolver` inside your own plugin will ease
123123
the integration process for your users, by resolving dependency conflicts
124124
between your plugin and other plugins in a user's project.
125125

126126
If you wish to redistribute the `Play Services Resolver` inside your plugin,
127-
you **must** follow these steps when importing the
127+
you **must** follow these steps when importing the
128128
`play-services-resolver-*.unitypackage`, and when exporting your own plugin
129129
package:
130130

131131
1. Import the `play-services-resolver-*.unitypackage` into your plugin
132-
project by
132+
project by
133133
[running Unity from the command line](https://docs.unity3d.com/Manual/CommandLineArguments.html), ensuring that
134134
you add the `-gvh_disable` option.
135-
1. Export your plugin by [running Unity from the command line](https://docs.unity3d.com/Manual/CommandLineArguments.html), ensuring that
135+
1. Export your plugin by [running Unity from the command line](https://docs.unity3d.com/Manual/CommandLineArguments.html), ensuring that
136136
you:
137137
- Include the contents of the `Assets/PlayServicesResolver` directory.
138138
- Add the `-gvh_disable` option.
139139

140-
You **must** specify the `-gvh_disable` option in order for the Version
140+
You **must** specify the `-gvh_disable` option in order for the Version
141141
Handler to work correctly!
142142

143143
For example, the following command will import the
144-
`play-services-resolver-1.2.46.0.unitypackage` into the project
145-
`MyPluginProject` and export the entire Assets folder to
144+
`play-services-resolver-1.2.46.0.unitypackage` into the project
145+
`MyPluginProject` and export the entire Assets folder to
146146
`MyPlugin.unitypackage`:
147147

148148
```
@@ -157,12 +157,12 @@ Unity -gvh_disable \
157157
## Background
158158

159159
The *Version Handler* component relies upon deferring the load of editor DLLs
160-
so that it can run first and determine the latest version of a plugin component
160+
so that it can run first and determine the latest version of a plugin component
161161
to activate. The build of the `Play Services Resolver` plugin has Unity asset
162162
metadata that is configured so that the editor components are not
163163
initially enabled when it's imported into a Unity project. To maintain this
164164
configuration when importing the `Play Services Resolver` .unitypackage
165-
into a Unity plugin project, you *must* specify the command line option
165+
into a Unity plugin project, you *must* specify the command line option
166166
`-gvh_disable` which will prevent the Version Handler component from running and
167167
changing the Unity asset metadata.
168168

@@ -172,7 +172,7 @@ The Android Resolver copies specified dependencies from local or remote Maven
172172
repositories into the Unity project when a user selects Android as the build
173173
target in the Unity editor.
174174

175-
1. Add the `play-services-resolver-*.unitypackage` to your plugin
175+
1. Add the `play-services-resolver-*.unitypackage` to your plugin
176176
project (assuming you are developing a plugin). If you are redistributing
177177
the Play Services Resolver with your plugin, you **must** follow the
178178
import steps in the [Getting Started](#getting-started) section!
@@ -294,6 +294,13 @@ The Android Resolver creates the
294294
of resolved dependencies in a project. This is used by the auto-resolution
295295
process to only run the expensive resolution process when necessary.
296296

297+
## Displaying Dependencies
298+
299+
It's possible to display the set of dependencies the Android Resolver
300+
would download and process in your project via the
301+
`Assets > Play Services Resolver > Android Resolver > Display Libraries` menu
302+
item.
303+
297304
# iOS Resolver Usage
298305

299306
The iOS resolver component of this plugin manages
@@ -303,7 +310,7 @@ to the Xcode project exported by Unity.
303310

304311
Dependencies for iOS are added by referring to CocoaPods.
305312

306-
1. Add the `play-services-resolver-*.unitypackage` to your plugin
313+
1. Add the `play-services-resolver-*.unitypackage` to your plugin
307314
project (assuming you are developing a plugin). If you are redistributing
308315
the Play Services Resolver with your plugin, you **must** follow the
309316
import steps in the [Getting Started](#getting-started) section!
@@ -360,7 +367,7 @@ Unity plugins can be managed by the Version Handler using the following steps:
360367
1. Optional: If your plugin is included in other Unity plugins, you should
361368
add the version number to each filename and change the GUID of each asset.
362369
This allows multiple versions of your plugin to be imported into a Unity
363-
project, with the Version Handler component activating only the most
370+
project, with the Version Handler component activating only the most
364371
recent version.
365372
1. Create a manifest text file named `MY_UNIQUE_PLUGIN_NAME_VERSION.txt`
366373
that lists all the files in your plugin relative to the project root.

source/JarResolverLib/src/Google.JarResolver/Dependency.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,34 @@ public class Dependency {
5757
/// originated.</param>
5858
public Dependency(string group, string artifact, string version, string[] packageIds=null,
5959
string[] repositories=null, string createdBy=null) {
60+
// If the dependency was added programmatically, strip out stack frames from inside the
61+
// library since the developer is likely interested in where in their code the
62+
// dependency was injected.
63+
if (createdBy == null) {
64+
var usefulFrames = new List<string>();
65+
bool filterFrames = true;
66+
// Filter all of the initial stack frames from system libraries and this plugin.
67+
foreach (var frame in System.Environment.StackTrace.Split(new char[] { '\n' })) {
68+
var frameString = frame.Trim();
69+
if (frameString.StartsWith("at ")) frameString = frameString.Split()[1];
70+
if (filterFrames && (
71+
frameString.StartsWith("System.Environment.") ||
72+
frameString.StartsWith("Google.JarResolver.") ||
73+
frameString.StartsWith("System.Reflection.") ||
74+
frameString.StartsWith("Google.VersionHandler"))) {
75+
continue;
76+
}
77+
filterFrames = false;
78+
usefulFrames.Add(frameString);
79+
}
80+
createdBy = String.Join("\n", usefulFrames.ToArray());
81+
}
6082
Group = group;
6183
Artifact = artifact;
6284
Version = version;
6385
PackageIds = packageIds;
6486
Repositories = repositories;
65-
CreatedBy = createdBy ?? System.Environment.StackTrace;
87+
CreatedBy = createdBy;
6688
}
6789

6890
/// <summary>

source/PlayServicesResolver/src/PlayServicesResolver.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,73 @@ public static void MenuDeleteResolvedLibraries() {
15441544
DeleteResolvedLibrariesSync();
15451545
}
15461546

1547+
/// <summary>
1548+
/// If dependencies is specified return the value, otherwise refresh from the project and
1549+
/// return the parsed dependencies.
1550+
/// </summary>
1551+
/// <returns>List of Android library dependencies.</returns>
1552+
private static IEnumerable<Dependency> GetOrReadDependencies(
1553+
IEnumerable<Dependency> dependencies) {
1554+
if (dependencies == null) {
1555+
xmlDependencies.ReadAll(logger);
1556+
dependencies = PlayServicesSupport.GetAllDependencies().Values;
1557+
}
1558+
return dependencies;
1559+
}
1560+
1561+
/// <summary>
1562+
/// Get the list of Android package specs referenced by the project and the sources they're
1563+
/// loaded from.
1564+
/// </summary>
1565+
/// <returns>List of package spec, source pairs.</returns>
1566+
public static IList<KeyValuePair<string, string>> GetPackageSpecs(
1567+
IEnumerable<Dependency> dependencies = null) {
1568+
return new List<KeyValuePair<string, string>>(new SortedList<string, string>(
1569+
ResolverVer1_1.DependenciesToPackageSpecs(GetOrReadDependencies(dependencies))));
1570+
}
1571+
1572+
/// <summary>
1573+
/// Get the list of Maven repo URIs required for Android libraries in this project.
1574+
/// </summary>
1575+
/// <returns>List of repo, source pairs.</returns>
1576+
public static IList<KeyValuePair<string, string>> GetRepos(
1577+
IEnumerable<Dependency> dependencies = null) {
1578+
return ResolverVer1_1.DependenciesToRepoUris(GetOrReadDependencies(dependencies));
1579+
}
1580+
1581+
/// <summary>
1582+
/// Display the set of dependncies / libraries currently included in the project.
1583+
/// This prints out the set of libraries in a form that can be easily included in a Gradle
1584+
/// script. This does not resolve dependency conflicts, it simply displays what is included
1585+
/// by plugins in the project.
1586+
/// </summary>
1587+
[MenuItem("Assets/Play Services Resolver/Android Resolver/Display Libraries")]
1588+
public static void MenuDisplayLibraries() {
1589+
xmlDependencies.ReadAll(logger);
1590+
var dependencies = PlayServicesSupport.GetAllDependencies().Values;
1591+
1592+
var lines = new List<string>();
1593+
lines.Add("allprojects {");
1594+
lines.Add(" repositories {");
1595+
lines.Add(" maven {");
1596+
foreach (var repoAndSources in GetRepos(dependencies: dependencies)) {
1597+
lines.Add(String.Format(" url \"{0}\" // {1}", repoAndSources.Key,
1598+
repoAndSources.Value));
1599+
}
1600+
lines.Add(" }");
1601+
lines.Add(" }");
1602+
lines.Add("}");
1603+
1604+
lines.Add("dependencies {");
1605+
foreach (var packageSpecAndSources in GetPackageSpecs(dependencies: dependencies)) {
1606+
lines.Add(String.Format(" implementation '{0}' // {1}", packageSpecAndSources.Key,
1607+
packageSpecAndSources.Value));
1608+
}
1609+
lines.Add("}");
1610+
1611+
Log(String.Join("\n", lines.ToArray()));
1612+
}
1613+
15471614
/// <summary>
15481615
/// Called when settings change.
15491616
/// </summary>

0 commit comments

Comments
 (0)