use of com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec in project android by JetBrains.
the class PsAndroidDependencyCollection method getOrCreateDependency.
@NotNull
private PsAndroidDependency getOrCreateDependency(@NotNull PsArtifactDependencySpec resolvedSpec, @NotNull Library library, @NotNull PsAndroidArtifact artifact, @Nullable ArtifactDependencyModel parsedModel) {
String compactNotation = resolvedSpec.toString();
PsLibraryAndroidDependency dependency = myLibraryDependenciesBySpec.get(compactNotation);
if (dependency == null) {
dependency = new PsLibraryAndroidDependency(myParent, resolvedSpec, artifact, library, parsedModel);
myLibraryDependenciesBySpec.put(compactNotation, dependency);
File libraryPath = null;
if (library instanceof AndroidLibrary) {
libraryPath = ((AndroidLibrary) library).getBundle();
} else if (library instanceof JavaLibrary) {
libraryPath = ((JavaLibrary) library).getJarFile();
}
List<PsArtifactDependencySpec> pomDependencies = Collections.emptyList();
if (libraryPath != null) {
pomDependencies = findDependenciesInPomFile(libraryPath);
}
dependency.setDependenciesFromPomFile(pomDependencies);
} else {
if (parsedModel != null) {
dependency.addParsedModel(parsedModel);
}
}
return dependency;
}
use of com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec in project android by JetBrains.
the class PsAndroidDependencyCollection method addLibrary.
@Nullable
private PsAndroidDependency addLibrary(@NotNull Library library, @NotNull PsAndroidArtifact artifact) {
PsParsedDependencies parsedDependencies = myParent.getParsedDependencies();
MavenCoordinates coordinates = library.getResolvedCoordinates();
if (coordinates != null) {
PsArtifactDependencySpec spec = PsArtifactDependencySpec.create(coordinates);
ArtifactDependencyModel matchingParsedDependency = parsedDependencies.findLibraryDependency(coordinates, artifact::contains);
if (matchingParsedDependency != null) {
String parsedVersionValue = matchingParsedDependency.version().value();
if (parsedVersionValue != null) {
// The dependency has a version in the build.gradle file.
// "tryParse" just in case the build.file has an invalid version.
GradleVersion parsedVersion = GradleVersion.tryParse(parsedVersionValue);
GradleVersion versionFromGradle = GradleVersion.parse(coordinates.getVersion());
if (parsedVersion != null && compare(parsedVersion, versionFromGradle) == 0) {
// Match.
return addLibrary(library, spec, artifact, matchingParsedDependency);
} else {
// Version mismatch. This can happen when the project specifies an artifact version but Gradle uses a different version
// from a transitive dependency.
// Example:
// 1. Module 'app' depends on module 'lib'
// 2. Module 'app' depends on Guava 18.0
// 3. Module 'lib' depends on Guava 19.0
// Gradle will force module 'app' to use Guava 19.0
// This is a case that may look as a version mismatch:
//
// testCompile 'junit:junit:4.11+'
// androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
//
// Here 'espresso' brings junit 4.12, but there is no mismatch with junit 4.11, because they are in different artifacts.
PsLibraryAndroidDependency potentialDuplicate = null;
for (PsLibraryAndroidDependency dependency : myLibraryDependenciesBySpec.values()) {
if (dependency.getParsedModels().contains(matchingParsedDependency)) {
potentialDuplicate = dependency;
break;
}
}
if (potentialDuplicate != null) {
// TODO match ArtifactDependencyModel#configurationName with potentialDuplicate.getContainers().artifact
}
// Create the dependency model that will be displayed in the "Dependencies" table.
addLibrary(library, spec, artifact, matchingParsedDependency);
// Create a dependency model for the transitive dependency, so it can be displayed in the "Variants" tool window.
return addLibrary(library, spec, artifact, null);
}
}
} else {
// This dependency was not declared, it could be a transitive one.
return addLibrary(library, spec, artifact, null);
}
}
return null;
}
use of com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec in project android by JetBrains.
the class PsLibraryAndroidDependency method getTransitiveDependencies.
@Override
@NotNull
public ImmutableCollection<PsDependency> getTransitiveDependencies() {
PsAndroidModule module = getParent();
ImmutableSet.Builder<PsDependency> transitive = ImmutableSet.builder();
for (String dependency : myTransitiveDependencies) {
PsAndroidDependency found = module.findLibraryDependency(dependency);
if (found != null) {
transitive.add(found);
}
}
for (PsArtifactDependencySpec dependency : myPomDependencies) {
PsLibraryAndroidDependency found = module.findLibraryDependency(dependency);
if (found != null) {
transitive.add(found);
}
}
return transitive.build();
}
use of com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec in project android by JetBrains.
the class PsJavaModule method addLibraryDependency.
public void addLibraryDependency(@NotNull String library, @NotNull List<String> scopesNames) {
// Update/reset the "parsed" model.
addLibraryDependencyToParsedModel(scopesNames, library);
// Reset dependencies.
myDependencyCollection = null;
PsJavaDependencyCollection dependencyCollection = getOrCreateDependencyCollection();
PsArtifactDependencySpec spec = PsArtifactDependencySpec.create(library);
assert spec != null;
PsParsedDependencies parsedDependencies = getParsedDependencies();
List<ArtifactDependencyModel> matchingParsedDependencies = parsedDependencies.findLibraryDependencies(spec, null);
for (ArtifactDependencyModel parsedDependency : matchingParsedDependencies) {
dependencyCollection.addLibraryDependency(spec, parsedDependency);
}
fireLibraryDependencyAddedEvent(spec);
setModified(true);
}
use of com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec in project android by JetBrains.
the class MavenPoms method findDependenciesInPomFile.
@NotNull
public static List<PsArtifactDependencySpec> findDependenciesInPomFile(@NotNull File libraryPath) {
File pomFilePath = LibraryFilePaths.getInstance().findPomPathForLibrary(libraryPath);
if (pomFilePath == null) {
return Collections.emptyList();
}
List<PsArtifactDependencySpec> dependencies = Lists.newArrayList();
try {
Document document = loadDocument(pomFilePath);
Element rootElement = document.getRootElement();
if (rootElement != null) {
Element dependenciesElement = null;
for (Element childElement : rootElement.getChildren()) {
if ("dependencies".equals(childElement.getName())) {
dependenciesElement = childElement;
break;
}
}
if (dependenciesElement != null) {
for (Element childElement : dependenciesElement.getChildren()) {
if ("dependency".equals(childElement.getName())) {
PsArtifactDependencySpec spec = createSpec(childElement);
if (spec != null) {
dependencies.add(spec);
}
}
}
}
}
} catch (Exception e) {
String msg = String.format("Failed to obtain dependencies in POM file for library '%1$s", libraryPath.getName());
LOG.warn(msg, e);
}
return dependencies;
}
Aggregations