Search in sources :

Example 6 with PsArtifactDependencySpec

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;
}
Also used : PsArtifactDependencySpec(com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec) File(java.io.File) MavenPoms.findDependenciesInPomFile(com.android.tools.idea.gradle.structure.model.pom.MavenPoms.findDependenciesInPomFile) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with PsArtifactDependencySpec

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;
}
Also used : PsArtifactDependencySpec(com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) PsParsedDependencies(com.android.tools.idea.gradle.structure.model.PsParsedDependencies) GradleVersion(com.android.ide.common.repository.GradleVersion) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with PsArtifactDependencySpec

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();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) PsArtifactDependencySpec(com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec) PsDependency(com.android.tools.idea.gradle.structure.model.PsDependency) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with PsArtifactDependencySpec

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);
}
Also used : PsArtifactDependencySpec(com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) PsParsedDependencies(com.android.tools.idea.gradle.structure.model.PsParsedDependencies)

Example 10 with PsArtifactDependencySpec

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;
}
Also used : PsArtifactDependencySpec(com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec) Element(org.jdom.Element) Document(org.jdom.Document) JDOMUtil.loadDocument(com.intellij.openapi.util.JDOMUtil.loadDocument) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsArtifactDependencySpec (com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec)16 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)5 NotNull (org.jetbrains.annotations.NotNull)5 PsParsedDependencies (com.android.tools.idea.gradle.structure.model.PsParsedDependencies)4 Nullable (org.jetbrains.annotations.Nullable)4 PsProject (com.android.tools.idea.gradle.structure.model.PsProject)3 File (java.io.File)3 PsModule (com.android.tools.idea.gradle.structure.model.PsModule)2 Module (com.intellij.openapi.module.Module)2 Project (com.intellij.openapi.project.Project)2 Element (org.jdom.Element)2 PROJECT_TYPE_APP (com.android.builder.model.AndroidProject.PROJECT_TYPE_APP)1 GradleVersion (com.android.ide.common.repository.GradleVersion)1 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)1 JarLibraryDependency (com.android.tools.idea.gradle.model.java.JarLibraryDependency)1 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 JavaModuleModel (com.android.tools.idea.gradle.project.model.JavaModuleModel)1 AbstractDependencyNode (com.android.tools.idea.gradle.structure.configurables.android.dependencies.treeview.AbstractDependencyNode)1 DependencyCellRenderer (com.android.tools.idea.gradle.structure.configurables.ui.dependencies.AbstractDeclaredDependenciesTableModel.DependencyCellRenderer)1 PsDependency (com.android.tools.idea.gradle.structure.model.PsDependency)1