@@ -2090,6 +2090,17 @@ public static IList<KeyValuePair<string, string>> GetPackageSpecs(
2090
2090
GradleResolver . DependenciesToPackageSpecs ( GetOrReadDependencies ( dependencies ) ) ) ) ;
2091
2091
}
2092
2092
2093
+ /// <summary>
2094
+ /// Get the list of Android package specs referenced by the project and the sources they're
2095
+ /// loaded from along with their corresponding dependency versions.
2096
+ /// </summary>
2097
+ /// <returns>Dictionary of the form {<packageSpec>: [sourceString, dependencyVersion]}.</returns>
2098
+ public static IList < KeyValuePair < string , List < string > > > GetPackageSpecsWithVersions (
2099
+ IEnumerable < Dependency > dependencies = null ) {
2100
+ return new List < KeyValuePair < string , List < string > > > ( new SortedList < string , List < string > > (
2101
+ GradleResolver . DependenciesToPackageSpecsWithVersions ( GetOrReadDependencies ( dependencies ) ) ) ) ;
2102
+ }
2103
+
2093
2104
/// <summary>
2094
2105
/// Get the list of Maven repo URIs required for Android libraries in this project.
2095
2106
/// </summary>
@@ -2203,33 +2214,32 @@ internal static IList<string> GradleDependenciesLines(
2203
2214
// we want to activate only the highest version but still include the
2204
2215
// other versions as commented out dependency lines.
2205
2216
var dependenciesMaxVersions = new Dictionary < string , string > ( ) ;
2217
+ string dependencyMaxVersion ;
2206
2218
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 )
2219
+ if ( dependenciesMaxVersions . TryGetValue ( dependency . VersionlessKey , out dependencyMaxVersion ) ) {
2220
+ if ( versionComparer . Compare ( dependency . Version , dependencyMaxVersion ) < 0 ) {
2212
2221
dependenciesMaxVersions [ dependency . VersionlessKey ] = dependency . Version ;
2222
+ }
2223
+ }
2224
+ else {
2225
+ dependenciesMaxVersions [ dependency . VersionlessKey ] = dependency . Version ;
2213
2226
}
2214
2227
}
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 ] ;
2228
+ foreach ( var packageSpecAndSourcesWithVersions in GetPackageSpecsWithVersions ( dependencies : dependencies ) ) {
2229
+ var packageSpecString = packageSpecAndSourcesWithVersions . Key ;
2230
+ var packageSourcesString = packageSpecAndSourcesWithVersions . Value [ 0 ] ;
2231
+ var packageVersionlessKey = packageSpecAndSourcesWithVersions . Value [ 1 ] ;
2232
+ var packageVersion = packageSpecAndSourcesWithVersions . Value [ 2 ] ;
2233
+
2224
2234
string line = " " ;
2225
2235
// If this is not the highest version of this dependency, add a line
2226
2236
// but comment it out by adding leading slashes.
2227
- if ( dependenciesMaxVersions [ dependency . VersionlessKey ] != dependency . Version )
2237
+ if ( dependenciesMaxVersions [ packageVersionlessKey ] != packageVersion )
2228
2238
line += "// " ;
2229
2239
2230
2240
line += String . Format (
2231
- "{0} '{1}' // {2}" , includeStatement , packageSpecAndSources . Key ,
2232
- packageSpecAndSources . Value , dependency . Version ) ;
2241
+ "{0} '{1}' // {2}" , includeStatement , packageSpecString ,
2242
+ packageSourcesString ) ;
2233
2243
lines . Add ( line ) ;
2234
2244
}
2235
2245
if ( includeDependenciesBlock ) lines . Add ( "}" ) ;
0 commit comments