Skip to content

Commit

Permalink
Add alternative approach to deal with dependency conflicts in android…
Browse files Browse the repository at this point in the history
… apps (#490)
  • Loading branch information
kageiit authored Aug 1, 2017
1 parent 05e2f9e commit 1bf4f52
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,7 +60,7 @@ class OkBuckGradlePlugin implements Plugin<Project> {
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"
Expand Down Expand Up @@ -90,7 +88,8 @@ class OkBuckGradlePlugin implements Plugin<Project> {

// 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')
Expand Down Expand Up @@ -136,7 +135,7 @@ class OkBuckGradlePlugin implements Plugin<Project> {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -34,7 +35,9 @@ class DependencyCache {
private final Set<File> created = new HashSet<>()
private final Set<ExternalDependency> requested = ConcurrentHashMap.newKeySet()

DependencyCache(Project project, File cacheDir) {
private final Map<String, ExternalDependency> forcedDeps = new HashMap<>()

DependencyCache(Project project, File cacheDir, String forcedConfiguration = null) {
this.rootProject = project.rootProject
this.cacheDir = cacheDir
this.fetchSources = rootProject.okbuck.intellij.sources
Expand All @@ -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() {
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Scope {
DependencyCache depCache

protected final Project project
protected final Set<ExternalDependency> external = [] as Set
final Set<ExternalDependency> external = [] as Set

Scope(Project project,
Collection<String> configurations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]

Expand Down

0 comments on commit 1bf4f52

Please sign in to comment.