Search in sources :

Example 1 with PsParsedDependencies

use of com.android.tools.idea.gradle.structure.model.PsParsedDependencies in project android by JetBrains.

the class PsJavaDependencyCollection method addDependencies.

private void addDependencies() {
    PsParsedDependencies parsedDependencies = myParent.getParsedDependencies();
    JavaModuleModel gradleModel = myParent.getGradleModel();
    for (JarLibraryDependency libraryDependency : gradleModel.getJarLibraryDependencies()) {
        GradleModuleVersion moduleVersion = libraryDependency.getModuleVersion();
        if (moduleVersion != null) {
            PsArtifactDependencySpec spec = PsArtifactDependencySpec.create(moduleVersion);
            ArtifactDependencyModel parsed = parsedDependencies.findLibraryDependency(moduleVersion);
            PsLibraryJavaDependency dependency = new PsLibraryJavaDependency(myParent, spec, libraryDependency, parsed);
            myLibraryDependenciesBySpec.put(spec.toString(), dependency);
        }
    }
}
Also used : JavaModuleModel(com.android.tools.idea.gradle.project.model.JavaModuleModel) JarLibraryDependency(com.android.tools.idea.gradle.model.java.JarLibraryDependency) 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) GradleModuleVersion(org.gradle.tooling.model.GradleModuleVersion)

Example 2 with PsParsedDependencies

use of com.android.tools.idea.gradle.structure.model.PsParsedDependencies in project android by JetBrains.

the class PsAndroidModule method addLibraryDependency.

public void addLibraryDependency(@NotNull String library, @NotNull PsNewDependencyScopes newScopes, @NotNull List<String> scopesNames) {
    // Update/reset the "parsed" model.
    addLibraryDependencyToParsedModel(scopesNames, library);
    // Reset dependencies.
    myDependencyCollection = null;
    PsAndroidDependencyCollection dependencyCollection = getOrCreateDependencyCollection();
    List<PsAndroidArtifact> targetArtifacts = Lists.newArrayList();
    forEachVariant(variant -> variant.forEachArtifact(artifact -> {
        if (newScopes.contains(artifact)) {
            targetArtifacts.add(artifact);
        }
    }));
    assert !targetArtifacts.isEmpty();
    PsArtifactDependencySpec spec = PsArtifactDependencySpec.create(library);
    assert spec != null;
    PsParsedDependencies parsedDependencies = getParsedDependencies();
    for (PsAndroidArtifact artifact : targetArtifacts) {
        List<ArtifactDependencyModel> matchingParsedDependencies = parsedDependencies.findLibraryDependencies(spec, artifact::contains);
        for (ArtifactDependencyModel parsedDependency : matchingParsedDependencies) {
            dependencyCollection.addLibraryDependency(spec, artifact, parsedDependency);
        }
    }
    fireLibraryDependencyAddedEvent(spec);
    setModified(true);
}
Also used : ArtifactRepository(com.android.tools.idea.gradle.structure.model.repositories.search.ArtifactRepository) PsModule(com.android.tools.idea.gradle.structure.model.PsModule) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) PROJECT_TYPE_APP(com.android.builder.model.AndroidProject.PROJECT_TYPE_APP) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Lists(com.google.common.collect.Lists) AndroidIcons(icons.AndroidIcons) PsParsedDependencies(com.android.tools.idea.gradle.structure.model.PsParsedDependencies) AndroidSdkRepositories(com.android.tools.idea.gradle.structure.model.repositories.search.AndroidSdkRepositories) PsProject(com.android.tools.idea.gradle.structure.model.PsProject) PsArtifactDependencySpec(com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) PsNewDependencyScopes(com.android.tools.idea.gradle.structure.model.android.dependency.PsNewDependencyScopes) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) javax.swing(javax.swing) 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 3 with PsParsedDependencies

use of com.android.tools.idea.gradle.structure.model.PsParsedDependencies 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 4 with PsParsedDependencies

use of com.android.tools.idea.gradle.structure.model.PsParsedDependencies in project android by JetBrains.

the class PsAndroidDependencyCollection method addModule.

private void addModule(@NotNull String gradlePath, @NotNull PsAndroidArtifact artifact, @Nullable String projectVariant) {
    PsParsedDependencies parsedDependencies = myParent.getParsedDependencies();
    ModuleDependencyModel matchingParsedDependency = parsedDependencies.findModuleDependency(gradlePath, artifact::contains);
    Module resolvedModule = null;
    PsModule module = myParent.getParent().findModuleByGradlePath(gradlePath);
    if (module != null) {
        resolvedModule = module.getResolvedModel();
    }
    PsModuleAndroidDependency dependency = findElement(gradlePath, PsModuleAndroidDependency.class);
    if (dependency == null) {
        dependency = new PsModuleAndroidDependency(myParent, gradlePath, artifact, projectVariant, resolvedModule, matchingParsedDependency);
        myModuleDependenciesByGradlePath.put(gradlePath, dependency);
    }
    updateDependency(dependency, artifact, matchingParsedDependency);
}
Also used : PsModule(com.android.tools.idea.gradle.structure.model.PsModule) ModuleDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ModuleDependencyModel) PsParsedDependencies(com.android.tools.idea.gradle.structure.model.PsParsedDependencies) PsModule(com.android.tools.idea.gradle.structure.model.PsModule) Module(com.intellij.openapi.module.Module)

Example 5 with PsParsedDependencies

use of com.android.tools.idea.gradle.structure.model.PsParsedDependencies 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)

Aggregations

PsParsedDependencies (com.android.tools.idea.gradle.structure.model.PsParsedDependencies)5 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)4 PsArtifactDependencySpec (com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec)4 PsModule (com.android.tools.idea.gradle.structure.model.PsModule)2 Module (com.intellij.openapi.module.Module)2 Nullable (org.jetbrains.annotations.Nullable)2 PROJECT_TYPE_APP (com.android.builder.model.AndroidProject.PROJECT_TYPE_APP)1 GradleVersion (com.android.ide.common.repository.GradleVersion)1 ModuleDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ModuleDependencyModel)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 PsProject (com.android.tools.idea.gradle.structure.model.PsProject)1 PsNewDependencyScopes (com.android.tools.idea.gradle.structure.model.android.dependency.PsNewDependencyScopes)1 AndroidSdkRepositories (com.android.tools.idea.gradle.structure.model.repositories.search.AndroidSdkRepositories)1 ArtifactRepository (com.android.tools.idea.gradle.structure.model.repositories.search.ArtifactRepository)1 Lists (com.google.common.collect.Lists)1 AndroidIcons (icons.AndroidIcons)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1