Skip to content

Commit 1453304

Browse files
committed
activating highest version in duplicate dependencies.
Bug: 136026456 Fixes: googlesamples#243 Change-Id: I98ee266f77d23077cbfd4471ae3e1dff74008a93
1 parent 30b4f92 commit 1453304

File tree

5 files changed

+246
-9
lines changed

5 files changed

+246
-9
lines changed

source/AndroidResolver/src/PlayServicesResolver.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace GooglePlayServices {
1818
using System;
1919
using System.Collections.Generic;
2020
using System.IO;
21+
using System.Linq;
2122
using System.Text.RegularExpressions;
2223
using System.Threading;
2324
using System.Xml;
@@ -2191,15 +2192,45 @@ internal static IList<string> GradleDependenciesLines(
21912192
// https://docs.gradle.org/3.4/release-notes.html#the-java-library-plugin
21922193
// https://developer.android.com/studio/releases/gradle-plugin#3-0-0
21932194
var version = GradleVersion;
2195+
var versionComparer = new Dependency.VersionComparer();
21942196
var includeStatement =
21952197
!String.IsNullOrEmpty(version) &&
2196-
(new Dependency.VersionComparer()).Compare("3.4", version) >= 0 ?
2198+
versionComparer.Compare("3.4", version) >= 0 ?
21972199
"implementation" : "compile";
21982200
if (includeDependenciesBlock) lines.Add("dependencies {");
2199-
foreach (var packageSpecAndSources in GetPackageSpecs(dependencies: dependencies)) {
2200-
lines.Add(String.Format(
2201-
" {0} '{1}' // {2}", includeStatement, packageSpecAndSources.Key,
2202-
packageSpecAndSources.Value));
2201+
// Build a map of dependencies with the max version of that dependency.
2202+
// If different packages have different versions of the same dependency,
2203+
// we want to activate only the highest version but still include the
2204+
// other versions as commented out dependency lines.
2205+
var dependenciesMaxVersions = new Dictionary<string, string>();
2206+
foreach( var dependency in dependencies) {
2207+
if(!dependenciesMaxVersions.ContainsKey(dependency.VersionlessKey))
2208+
dependenciesMaxVersions[dependency.VersionlessKey] = dependency.Version;
2209+
else {
2210+
var compareWithVersion = dependenciesMaxVersions[dependency.VersionlessKey];
2211+
if(versionComparer.Compare(dependency.Version, compareWithVersion) < 0)
2212+
dependenciesMaxVersions[dependency.VersionlessKey] = dependency.Version;
2213+
}
2214+
}
2215+
List<Dependency> dependenciesList = dependencies.OrderBy(Dependency=>Dependency.Key).ToList();
2216+
foreach( var dependency in dependenciesList) {
2217+
// Passing the entire list to GetPackageSpecs returns results
2218+
// in an arbitrary order because it takes an IEnumerable.
2219+
// Hence we cannot pass the entire list as is. We pass an element
2220+
// at a time to ensure the correspondence with results.
2221+
List<Dependency> singleItemList = new List<Dependency>();
2222+
singleItemList.Add(dependency);
2223+
var packageSpecAndSources = GetPackageSpecs(singleItemList)[0];
2224+
string line = " ";
2225+
// If this is not the highest version of this dependency, add a line
2226+
// but comment it out by adding leading slashes.
2227+
if(dependenciesMaxVersions[dependency.VersionlessKey] != dependency.Version)
2228+
line += "// ";
2229+
2230+
line += String.Format(
2231+
"{0} '{1}' // {2}", includeStatement, packageSpecAndSources.Key,
2232+
packageSpecAndSources.Value, dependency.Version);
2233+
lines.Add(line);
22032234
}
22042235
if (includeDependenciesBlock) lines.Add("}");
22052236
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<dependencies>
2+
<androidPackages>
3+
<androidPackage spec="com.google.android.gms:play-services-base:12.0.1" />
4+
</androidPackages>
5+
</dependencies>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
2+
3+
buildscript {
4+
repositories {
5+
jcenter()
6+
google()
7+
}
8+
9+
dependencies {
10+
classpath 'com.android.tools.build:gradle:3.0.1'
11+
}
12+
}
13+
14+
allprojects {
15+
repositories {
16+
flatDir {
17+
dirs 'libs'
18+
}
19+
}
20+
}
21+
22+
// Android Resolver Repos Start
23+
([rootProject] + (rootProject.subprojects as List)).each { project ->
24+
project.repositories {
25+
def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/")
26+
maven {
27+
url "https://maven.google.com"
28+
}
29+
maven {
30+
url "file:///my/nonexistant/test/repo" // Assets/ExternalDependencyManager/Editor/TestDependencies.xml:17
31+
}
32+
maven {
33+
url (unityProjectPath + "/project_relative_path/repo") // Assets/ExternalDependencyManager/Editor/TestDependencies.xml:17
34+
}
35+
maven {
36+
url (unityProjectPath + "/Assets/GeneratedLocalRepo/Firebase/m2repository") // Assets/ExternalDependencyManager/Editor/TestDependencies.xml:10
37+
}
38+
mavenLocal()
39+
jcenter()
40+
mavenCentral()
41+
}
42+
}
43+
// Android Resolver Repos End
44+
apply plugin: 'com.android.application'
45+
46+
dependencies {
47+
implementation fileTree(dir: 'libs', include: ['*.jar'])
48+
// Android Resolver Dependencies Start
49+
compile 'com.android.support:support-annotations:26.1.0' // Assets/ExternalDependencyManager/Editor/TestDependencies.xml:4
50+
// compile 'com.google.android.gms:play-services-base:12.0.1' // Assets/ExternalDependencyManager/Editor/TestAdditionalDuplicateDependencies.xml:3
51+
compile 'com.google.android.gms:play-services-base:15.0.1' // Assets/ExternalDependencyManager/Editor/TestAdditionalDependencies.xml:3
52+
compile 'com.google.firebase:firebase-app-unity:5.1.1' // Assets/ExternalDependencyManager/Editor/TestDependencies.xml:10
53+
compile 'com.google.firebase:firebase-common:16.0.0' // Google.AndroidResolverIntegrationTests.SetupDependencies
54+
compile 'org.test.psr:classifier:1.0.1:foo@aar' // Assets/ExternalDependencyManager/Editor/TestDependencies.xml:12
55+
// Android Resolver Dependencies End
56+
**DEPS**}
57+
58+
// Android Resolver Exclusions Start
59+
android {
60+
packagingOptions {
61+
exclude ('lib/unsupported/libFirebaseCppApp-5.1.1.so')
62+
}
63+
}
64+
// Android Resolver Exclusions End
65+
android {
66+
compileSdkVersion **APIVERSION**
67+
buildToolsVersion '**BUILDTOOLS**'
68+
69+
defaultConfig {
70+
minSdkVersion **MINSDKVERSION**
71+
targetSdkVersion **TARGETSDKVERSION**
72+
applicationId '**APPLICATIONID**'
73+
ndk {
74+
abiFilters **ABIFILTERS**
75+
}
76+
versionCode **VERSIONCODE**
77+
versionName '**VERSIONNAME**'
78+
}
79+
80+
lintOptions {
81+
abortOnError false
82+
}
83+
84+
aaptOptions {
85+
noCompress '.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**
86+
}**SIGN**
87+
88+
buildTypes {
89+
debug {
90+
minifyEnabled **MINIFY_DEBUG**
91+
useProguard **PROGUARD_DEBUG**
92+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD**
93+
jniDebuggable true
94+
}
95+
release {
96+
minifyEnabled **MINIFY_RELEASE**
97+
useProguard **PROGUARD_RELEASE**
98+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD****SIGNCONFIG**
99+
}
100+
}**PACKAGING_OPTIONS****SPLITS**
101+
**BUILT_APK_LOCATION**
102+
}**SPLITS_VERSION_CODE****SOURCE_BUILD_SETUP**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
2+
3+
buildscript {
4+
repositories {
5+
jcenter()
6+
google()
7+
}
8+
9+
dependencies {
10+
classpath 'com.android.tools.build:gradle:3.0.1'
11+
}
12+
}
13+
14+
allprojects {
15+
repositories {
16+
flatDir {
17+
dirs 'libs'
18+
}
19+
}
20+
}
21+
22+
apply plugin: 'com.android.application'
23+
24+
dependencies {
25+
implementation fileTree(dir: 'libs', include: ['*.jar'])
26+
**DEPS**}
27+
28+
android {
29+
compileSdkVersion **APIVERSION**
30+
buildToolsVersion '**BUILDTOOLS**'
31+
32+
defaultConfig {
33+
minSdkVersion **MINSDKVERSION**
34+
targetSdkVersion **TARGETSDKVERSION**
35+
applicationId '**APPLICATIONID**'
36+
ndk {
37+
abiFilters **ABIFILTERS**
38+
}
39+
versionCode **VERSIONCODE**
40+
versionName '**VERSIONNAME**'
41+
}
42+
43+
lintOptions {
44+
abortOnError false
45+
}
46+
47+
aaptOptions {
48+
noCompress '.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**
49+
}**SIGN**
50+
51+
buildTypes {
52+
debug {
53+
minifyEnabled **MINIFY_DEBUG**
54+
useProguard **PROGUARD_DEBUG**
55+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD**
56+
jniDebuggable true
57+
}
58+
release {
59+
minifyEnabled **MINIFY_RELEASE**
60+
useProguard **PROGUARD_RELEASE**
61+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD****SIGNCONFIG**
62+
}
63+
}**PACKAGING_OPTIONS****SPLITS**
64+
**BUILT_APK_LOCATION**
65+
}**SPLITS_VERSION_CODE****SOURCE_BUILD_SETUP**

source/AndroidResolver/test/src/AndroidResolverIntegrationTests.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public class AndroidResolverIntegrationTests {
4444
/// </summary>
4545
private const string ADDITIONAL_DEPENDENCIES_FILENAME = "TestAdditionalDependencies";
4646

47+
/// <summary>
48+
/// The name of the file, without extension, that will serve as a template for dynamically
49+
/// adding additional dependencies with a duplicate package with a different version.
50+
/// </summary>
51+
private const string ADDITIONAL_DUPLICATE_DEPENDENCIES_FILENAME = "TestAdditionalDuplicateDependencies";
52+
4753
/// <summary>
4854
/// Disabled application Gradle template file.
4955
/// </summary>
@@ -129,6 +135,29 @@ public static void ConfigureTestCases() {
129135
});
130136
}
131137
},
138+
new IntegrationTester.TestCase {
139+
Name = "ResolveForGradleBuildSystemWithDuplicatePackages",
140+
Method = (testCase, testCaseComplete) => {
141+
ClearAllDependencies();
142+
SetupDependencies();
143+
// Add 2 additional dependency files (each file contains a single package
144+
// but with different versions).
145+
UpdateAdditionalDependenciesFile(true, ADDITIONAL_DEPENDENCIES_FILENAME);
146+
UpdateAdditionalDependenciesFile(true, ADDITIONAL_DUPLICATE_DEPENDENCIES_FILENAME);
147+
148+
ResolveWithGradleTemplate(
149+
GRADLE_TEMPLATE_DISABLED,
150+
"ExpectedArtifacts/NoExport/GradleTemplateDuplicatePackages",
151+
testCase, testCaseComplete,
152+
otherExpectedFiles: new [] {
153+
"Assets/GeneratedLocalRepo/Firebase/m2repository/com/google/" +
154+
"firebase/firebase-app-unity/5.1.1/firebase-app-unity-5.1.1.aar" },
155+
filesToIgnore: new HashSet<string> {
156+
Path.GetFileName(GRADLE_TEMPLATE_LIBRARY_DISABLED),
157+
Path.GetFileName(GRADLE_TEMPLATE_PROPERTIES_DISABLED)
158+
});
159+
}
160+
},
132161
new IntegrationTester.TestCase {
133162
Name = "ResolverForGradleBuildSystemWithTemplateUsingJetifier",
134163
Method = (testCase, testCaseComplete) => {
@@ -474,7 +503,8 @@ private static void ClearAllDependencies() {
474503
GooglePlayServices.SettingsDialog.PatchPropertiesTemplateGradle = false;
475504

476505
PlayServicesSupport.ResetDependencies();
477-
UpdateAdditionalDependenciesFile(false);
506+
UpdateAdditionalDependenciesFile(false, ADDITIONAL_DEPENDENCIES_FILENAME);
507+
UpdateAdditionalDependenciesFile(false, ADDITIONAL_DUPLICATE_DEPENDENCIES_FILENAME);
478508
}
479509

480510
/// <summary>
@@ -565,14 +595,18 @@ private static void CompareKeyValuePairLists(
565595
/// </summary>
566596
/// <param name="addDependencyFile">If true, will copy the template file to an XML file if it
567597
/// doesn't exist. If false, delete the XML file if it exists.</param>
568-
private static void UpdateAdditionalDependenciesFile(bool addDependencyFile) {
598+
/// <param name="filename">Name of the template file (without extension) to
599+
/// create an XML from. </param>
600+
private static void UpdateAdditionalDependenciesFile(
601+
bool addDependencyFile,
602+
string filename=ADDITIONAL_DEPENDENCIES_FILENAME) {
569603
string currentDirectory = Directory.GetCurrentDirectory();
570604
string editorPath = Path.Combine(currentDirectory,
571605
"Assets/ExternalDependencyManager/Editor/");
572606

573-
string templateFilePath = Path.Combine(editorPath, ADDITIONAL_DEPENDENCIES_FILENAME +
607+
string templateFilePath = Path.Combine(editorPath, filename+
574608
".template");
575-
string xmlFilePath = Path.Combine(editorPath, ADDITIONAL_DEPENDENCIES_FILENAME + ".xml");
609+
string xmlFilePath = Path.Combine(editorPath, filename+ ".xml");
576610
if (addDependencyFile && !File.Exists(xmlFilePath)) {
577611
if (!File.Exists(templateFilePath)) {
578612
UnityEngine.Debug.LogError("Could not find file: " + templateFilePath);

0 commit comments

Comments
 (0)