From 1bf4f52ee0e81d9cfc7db808ff3f7d276e2a8e06 Mon Sep 17 00:00:00 2001 From: Gautam Korlam Date: Tue, 1 Aug 2017 16:55:35 -0700 Subject: [PATCH] Add alternative approach to deal with dependency conflicts in android apps (#490) --- .../com/uber/okbuck/OkBuckGradlePlugin.groovy | 9 ++++----- .../core/dependency/DependencyCache.groovy | 17 +++++++++++++++-- .../uber/okbuck/core/model/base/Scope.groovy | 2 +- .../core/dependency/ExternalDependency.java | 4 ++-- .../com/uber/okbuck/core/util/ProjectUtil.java | 4 ---- dependencies.gradle | 2 +- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/buildSrc/src/main/groovy/com/uber/okbuck/OkBuckGradlePlugin.groovy b/buildSrc/src/main/groovy/com/uber/okbuck/OkBuckGradlePlugin.groovy index 999ce4daa..c286ac348 100644 --- a/buildSrc/src/main/groovy/com/uber/okbuck/OkBuckGradlePlugin.groovy +++ b/buildSrc/src/main/groovy/com/uber/okbuck/OkBuckGradlePlugin.groovy @@ -26,8 +26,6 @@ import org.gradle.api.Project import org.gradle.api.Task import org.gradle.api.artifacts.Configuration -import java.nio.file.Paths - // Dependency Tree // // okbuck @@ -62,7 +60,7 @@ class OkBuckGradlePlugin implements Plugin { public static final String TRANSFORM = "transform" public static final String RETROLAMBDA = "retrolambda" public static final String SCALA = "scala" - public static final String CONFIGURATION_EXTERNAL = "externalOkbuck" + public static final String FORCED_OKBUCK = "forcedOkbuck" public static final String OKBUCK_DEFS = ".okbuck/defs/DEFS" public static final String OKBUCK_STATE_DIR = ".okbuck/state" @@ -90,7 +88,8 @@ class OkBuckGradlePlugin implements Plugin { // Create configurations project.configurations.maybeCreate(TransformUtil.CONFIGURATION_TRANSFORM) - Configuration externalOkbuck = project.configurations.maybeCreate(CONFIGURATION_EXTERNAL) + Configuration forced = project.configurations.maybeCreate(FORCED_OKBUCK) + forced.transitive = false // Create tasks Task setupOkbuck = project.task('setupOkbuck') @@ -136,7 +135,7 @@ class OkBuckGradlePlugin implements Plugin { } File cacheDir = DependencyUtils.createCacheDir(project, DEFAULT_CACHE_PATH, EXTERNAL_DEP_BUCK_FILE) - depCache = new DependencyCache(project, cacheDir) + depCache = new DependencyCache(project, cacheDir, FORCED_OKBUCK) // Fetch Lint deps if needed if (!lint.disabled && lint.version != null) { diff --git a/buildSrc/src/main/groovy/com/uber/okbuck/core/dependency/DependencyCache.groovy b/buildSrc/src/main/groovy/com/uber/okbuck/core/dependency/DependencyCache.groovy index b06dc01d4..89d44eed0 100644 --- a/buildSrc/src/main/groovy/com/uber/okbuck/core/dependency/DependencyCache.groovy +++ b/buildSrc/src/main/groovy/com/uber/okbuck/core/dependency/DependencyCache.groovy @@ -1,6 +1,7 @@ package com.uber.okbuck.core.dependency import com.uber.okbuck.OkBuckGradlePlugin +import com.uber.okbuck.core.model.base.Scope import com.uber.okbuck.core.model.base.Store import com.uber.okbuck.core.util.FileUtil import org.apache.commons.io.IOUtils @@ -34,7 +35,9 @@ class DependencyCache { private final Set created = new HashSet<>() private final Set requested = ConcurrentHashMap.newKeySet() - DependencyCache(Project project, File cacheDir) { + private final Map forcedDeps = new HashMap<>() + + DependencyCache(Project project, File cacheDir, String forcedConfiguration = null) { this.rootProject = project.rootProject this.cacheDir = cacheDir this.fetchSources = rootProject.okbuck.intellij.sources @@ -43,6 +46,13 @@ class DependencyCache { processors = new Store(new File("${OkBuckGradlePlugin.OKBUCK_STATE_DIR}/PROCESSORS")) lintJars = new Store(new File("${OkBuckGradlePlugin.OKBUCK_STATE_DIR}/LINT_JARS")) proguardConfigs = new Store(new File("${OkBuckGradlePlugin.OKBUCK_STATE_DIR}/PROGUARD_CONFIGS")) + + if (forcedConfiguration) { + new Scope(project, Collections.singleton(forcedConfiguration)).external.each { + get(it) + forcedDeps.put(it.group + ":" + it.name, it) + } + } } void finalizeDeps() { @@ -70,7 +80,10 @@ class DependencyCache { } } - String get(ExternalDependency dependency, boolean resolveOnly = false) { + String get(ExternalDependency externalDependency, boolean resolveOnly = false) { + ExternalDependency dependency = + forcedDeps.getOrDefault(externalDependency.group + ":" + externalDependency.name, externalDependency) + File cachedCopy = new File(cacheDir, dependency.getCacheName(!resolveOnly)) String key = FileUtil.getRelativePath(rootProject.projectDir, cachedCopy) createLink(Paths.get(key), dependency.depFile.toPath()) diff --git a/buildSrc/src/main/groovy/com/uber/okbuck/core/model/base/Scope.groovy b/buildSrc/src/main/groovy/com/uber/okbuck/core/model/base/Scope.groovy index fddb046b8..0c3907cd8 100644 --- a/buildSrc/src/main/groovy/com/uber/okbuck/core/model/base/Scope.groovy +++ b/buildSrc/src/main/groovy/com/uber/okbuck/core/model/base/Scope.groovy @@ -27,7 +27,7 @@ class Scope { DependencyCache depCache protected final Project project - protected final Set external = [] as Set + final Set external = [] as Set Scope(Project project, Collection configurations, diff --git a/buildSrc/src/main/java/com/uber/okbuck/core/dependency/ExternalDependency.java b/buildSrc/src/main/java/com/uber/okbuck/core/dependency/ExternalDependency.java index 3a5510b53..c5f7e0b9b 100644 --- a/buildSrc/src/main/java/com/uber/okbuck/core/dependency/ExternalDependency.java +++ b/buildSrc/src/main/java/com/uber/okbuck/core/dependency/ExternalDependency.java @@ -41,11 +41,11 @@ public boolean equals(Object o) { @Override public int hashCode() { - int result = version != null ? version.hashCode() : 0; + int result = (isLocal ? 1 : 0); + result = 31 * result + (version != null ? version.hashCode() : 0); result = 31 * result + (depFile != null ? depFile.hashCode() : 0); result = 31 * result + (group != null ? group.hashCode() : 0); result = 31 * result + (name != null ? name.hashCode() : 0); - result = 31 * result + (isLocal ? 1 : 0); return result; } diff --git a/buildSrc/src/main/java/com/uber/okbuck/core/util/ProjectUtil.java b/buildSrc/src/main/java/com/uber/okbuck/core/util/ProjectUtil.java index bf9bd4a36..ec4389035 100644 --- a/buildSrc/src/main/java/com/uber/okbuck/core/util/ProjectUtil.java +++ b/buildSrc/src/main/java/com/uber/okbuck/core/util/ProjectUtil.java @@ -55,10 +55,6 @@ public static Target getTargetForOutput(Project targetProject, File output) { return getTargetCache(targetProject).getTargetForOutput(targetProject, output); } - public static OkBuckExtension getExtension(Project project) { - return project.getRootProject().getExtensions().getByType(OkBuckExtension.class); - } - static OkBuckGradlePlugin getPlugin(Project project) { return project.getRootProject().getPlugins().getPlugin(OkBuckGradlePlugin.class); } diff --git a/dependencies.gradle b/dependencies.gradle index 8000824c9..6751baaa0 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -71,7 +71,7 @@ def test = [ junit : 'junit:junit:4.12', kotlinTest : "org.jetbrains.kotlin:kotlin-test-junit:${versions.kotlinVersion}", mockito : 'org.mockito:mockito-core:1.10.19', - robolectric : 'org.robolectric:robolectric:3.4-rc2', + robolectric : 'org.robolectric:robolectric:3.4', assertj : 'org.assertj:assertj-core:3.8.0' ]