Skip to content

Commit b5c7035

Browse files
Stewart MilesGerrit Code Review
authored andcommitted
Merge "Updating README and adding sample dependency file."
2 parents 546ae92 + 1e2ccb5 commit b5c7035

File tree

3 files changed

+221
-76
lines changed

3 files changed

+221
-76
lines changed

README.md

Lines changed: 78 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@ the Google Repository SDK components. These are found in the "extras" section.
5454

5555
# Packaging
5656

57-
The plugin consists of a C# DLL that contains the logic to resolve the dependencies
58-
and the logic to resolve dependencies and copy them into Unity projects.
59-
This includes removing older versions of the client libraries.
57+
The plugin consists of several C# DLLs that contain
58+
the logic to resolve the dependencies for both Android and iOS (using CocoaPods),
59+
the logic to resolve dependencies and copy them into Unity projects, and logic
60+
to remove older versions of the client libraries as well as older versions of the
61+
JarResolver DLLs.
6062

61-
There also are 2 C# files. The first, PlayServicesResolver.cs,
62-
creates an AssetPostprocessor instance that is used to trigger background
63-
resolution of the dependencies. It also adds a menu item
6463
(Assets/Google Play Services/Resolve Client Jars). In order to support
6564
Unity version 4.x, this class also converts the aar file
6665
to a java plugin project. The second C# file is SampleDependencies.cs
@@ -76,19 +75,45 @@ plugin).
7675
2. Copy the SampleDependencies.cs file to another name specific to your plugin
7776
and add the dependencies your plugin needs.
7877

78+
Reflection is used to access the resolver in order to behave correctly when the
79+
project is being loaded into Unity and there is no specific order of class
80+
initialization.
81+
82+
For Android dependencies first create and instance of the resolver object:
7983
```
80-
instance.DependOn(group, artifact, version);
84+
// Setup the resolver using reflection as the module may not be
85+
// available at compile time.
86+
Type playServicesSupport = Google.VersionHandler.FindClass(
87+
"Google.JarResolver", "Google.JarResolver.PlayServicesSupport");
88+
if (playServicesSupport == null) {
89+
return;
90+
}
91+
svcSupport = svcSupport ?? Google.VersionHandler.InvokeStaticMethod(
92+
playServicesSupport, "CreateInstance",
93+
new object[] {
94+
"GooglePlayGames",
95+
EditorPrefs.GetString("AndroidSdkRoot"),
96+
"ProjectSettings"
97+
});
8198
```
8299

83-
This declares that the this application now depends on the specified artifact.
84-
Where:
85-
86-
* group: indicates the artifact group, e.g. "com.android.support" or
87-
"com.google.android.gms"
88-
* artifact: indicates the artifact, e.g. "appcompat-v7" or
89-
"play-services-appinvite"
90-
* version: indicates the version. The version value supports both specific
91-
versions such as 8.1.0, and also the trailing '+' indicating "or greater" for
100+
Then add dependencies. For example to depend on
101+
play-services-games version 9.6.0, you need to specify the package, artifact,
102+
and version as well as the packageId from the SDK manager in case a updated
103+
version needs to be downloaded from the SDK Manager in order to build.
104+
```
105+
Google.VersionHandler.InvokeInstanceMethod(
106+
svcSupport, "DependOn",
107+
new object[] {
108+
"com.google.android.gms",
109+
"play-services-games",
110+
"9.6.0" },
111+
namedArgs: new Dictionary<string, object>() {
112+
{"packageIds", new string[] { "extra-google-m2repository" } }
113+
});
114+
```
115+
The version value supports both specific versions such as 8.1.0,
116+
and also the trailing '+' indicating "or greater" for
92117
the portion of the number preceding the period. For example 8.1.+ would match
93118
8.1.2, but not 8.2. The string "8+" would resolve to any version greater or
94119
equal to 8.0. The meta version 'LATEST' is also supported meaning the greatest
@@ -101,6 +126,39 @@ are processed by the Android Gradle plugin. Unfortunately, Unity does not perfo
101126
the same processing, so this plugin handles known cases of this variable substition
102127
by exploding the aar and replacing ${applicationId} with the bundleID.
103128

129+
130+
# iOS Dependency Management
131+
iOS dependencies are identified using Cocoapods. Cocoapods is run as a post build
132+
process step. The libraries are downloaded and injected into the XCode project
133+
file directly, rather than creating a separate xcworkspace.
134+
135+
To add a dependency you first need an instance of the resolver. Reflection is
136+
used to safely handle race conditions when Unity is loading the project and the
137+
order of class initialization is not known.
138+
```
139+
Type iosResolver = Google.VersionHandler.FindClass(
140+
"Google.IOSResolver", "Google.IOSResolver");
141+
if (iosResolver == null) {
142+
return;
143+
}
144+
```
145+
146+
Dependencies for iOS are added by referring to CocoaPods. The libraries and
147+
frameworks are added to the Unity project, so they will automatically be included.
148+
149+
This example add the GooglePlayGames pod, version 5.0 or greater,
150+
disabling bitcode generation.
151+
152+
```
153+
Google.VersionHandler.InvokeStaticMethod(
154+
iosResolver, "AddPod",
155+
new object[] { "GooglePlayGames" },
156+
namedArgs: new Dictionary<string, object>() {
157+
{ "version", "5.0+" },
158+
{ "bitcodeEnabled", false },
159+
});
160+
```
161+
104162
# Disabling automatic resolution
105163

106164
Automatic resolution can be disabled in the Settings dialog,
@@ -132,9 +190,11 @@ non-concrete version constraints (i.e. have a + in the version).
132190
3. If there
133191
4. If there are still possible versions to check, add the dependency to the end
134192
of the unresolved list for re-processing with a new version candidate.
135-
5. If there are no possible versions to resolve both the candidate and the
193+
5. If there are no possible versions, then the SDK Manager is used to download
194+
and updated versions of the libraries based on the packageId.
195+
6. If there still are no possible versions to resolve both the candidate and the
136196
unresolved dependencies, then either fail resolution with an exception, or use
137-
the greatest version value (depending on the useLatest flag passed to resolve).
197+
the greatest version value.
138198
3. When a candidate version is selected, the pom file is read for that version and
139199
the
140200

@@ -143,4 +203,3 @@ the unresolved.
143203
3. Process transitive dependencies
144204
5. for each candidate artifact, read the pom file for dependencies and add them to
145205
the unresolved list.
146-
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// <copyright file="GPGSDependencies.cs" company="Google Inc.">
2+
// Copyright (C) 2015 Google Inc. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using System;
18+
using System.Collections.Generic;
19+
using UnityEditor;
20+
21+
/// Sample dependencies file. Change the class name and dependencies as required by your project, then
22+
/// save the file in a folder named Editor (which can be a sub-folder of your plugin).
23+
/// There can be multiple dependency files like this one per project, the resolver will combine them and process all
24+
/// of them at once.
25+
[InitializeOnLoad]
26+
public class SampleDependencies : AssetPostprocessor {
27+
#if UNITY_ANDROID
28+
/// <summary>Instance of the PlayServicesSupport resolver</summary>
29+
public static object svcSupport;
30+
#endif // UNITY_ANDROID
31+
32+
/// Initializes static members of the class.
33+
static SampleDependencies() {
34+
35+
//
36+
//
37+
// NOTE:
38+
//
39+
// UNCOMMENT THIS CALL TO MAKE THE DEPENDENCIES BE REGISTERED.
40+
// THIS FILE IS ONLY A SAMPLE!!
41+
//
42+
// RegisterDependencies();
43+
//
44+
}
45+
46+
47+
/// <summary>
48+
/// Registers the dependencies needed by this plugin.
49+
/// </summary>
50+
public static void RegisterDependencies() {
51+
#if UNITY_ANDROID
52+
RegisterAndroidDependencies();
53+
#elif UNITY_IOS
54+
RegisterIOSDependencies();
55+
#endif
56+
}
57+
58+
/// <summary>
59+
/// Registers the android dependencies.
60+
/// </summary>
61+
public static void RegisterAndroidDependencies() {
62+
63+
// Setup the resolver using reflection as the module may not be
64+
// available at compile time.
65+
Type playServicesSupport = Google.VersionHandler.FindClass(
66+
"Google.JarResolver", "Google.JarResolver.PlayServicesSupport");
67+
if (playServicesSupport == null) {
68+
return;
69+
}
70+
svcSupport = svcSupport ?? Google.VersionHandler.InvokeStaticMethod(
71+
playServicesSupport, "CreateInstance",
72+
new object[] {
73+
"GooglePlayGames",
74+
EditorPrefs.GetString("AndroidSdkRoot"),
75+
"ProjectSettings"
76+
});
77+
78+
// For example to depend on play-services-games version 9.6.0 you need to specify the
79+
// package, artifact, and version as well as the packageId from the SDK manager in case
80+
// a newer version needs to be downloaded to build.
81+
82+
Google.VersionHandler.InvokeInstanceMethod(
83+
svcSupport, "DependOn",
84+
new object[] {
85+
"com.google.android.gms",
86+
"play-services-games",
87+
"9.6.0" },
88+
namedArgs: new Dictionary<string, object>() {
89+
{"packageIds", new string[] { "extra-google-m2repository" } }
90+
});
91+
92+
// This example gets the com.android.support.support-v4 library, version 23.1 or greater.
93+
// notice it is in a different package than the play-services libraries.
94+
95+
Google.VersionHandler.InvokeInstanceMethod(
96+
svcSupport, "DependOn",
97+
new object[] { "com.android.support", "support-v4", "23.1+" },
98+
namedArgs: new Dictionary<string, object>() {
99+
{"packageIds", new string[] { "extra-android-m2repository" } }
100+
});
101+
}
102+
103+
/// <summary>
104+
/// Registers the IOS dependencies.
105+
/// </summary>
106+
public static void RegisterIOSDependencies() {
107+
108+
// Setup the resolver using reflection as the module may not be
109+
// available at compile time.
110+
Type iosResolver = Google.VersionHandler.FindClass(
111+
"Google.IOSResolver", "Google.IOSResolver");
112+
if (iosResolver == null) {
113+
return;
114+
}
115+
116+
// Dependencies for iOS are added by referring to CocoaPods. The libraries and frameworkds are
117+
// and added to the Unity project, so they will automatically be included.
118+
//
119+
// This example add the GooglePlayGames pod, version 5.0 or greater, disabling bitcode generation.
120+
121+
Google.VersionHandler.InvokeStaticMethod(
122+
iosResolver, "AddPod",
123+
new object[] { "GooglePlayGames" },
124+
namedArgs: new Dictionary<string, object>() {
125+
{ "version", "5.0+" },
126+
{ "bitcodeEnabled", false },
127+
});
128+
}
129+
130+
// Handle delayed loading of the dependency resolvers.
131+
private static void OnPostprocessAllAssets(
132+
string[] importedAssets, string[] deletedAssets,
133+
string[] movedAssets, string[] movedFromPath) {
134+
foreach (string asset in importedAssets) {
135+
if (asset.Contains("IOSResolver") ||
136+
asset.Contains("JarResolver")) {
137+
RegisterDependencies();
138+
break;
139+
}
140+
}
141+
}
142+
}
143+

source/PlayServicesResolver/src/SampleDependencies.cs

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)