Search in sources :

Example 6 with GradleCoordinate

use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.

the class GradleDependencyManager method updateDependencies.

private static void updateDependencies(@NotNull GradleBuildModel buildModel, @NotNull Module module, @NotNull List<GradleCoordinate> coordinates) {
    ModuleRootModificationUtil.updateModel(module, model -> {
        DependenciesModel dependenciesModel = buildModel.dependencies();
        for (GradleCoordinate gc : coordinates) {
            List<ArtifactDependencyModel> artifacts = Lists.newArrayList(dependenciesModel.artifacts());
            for (ArtifactDependencyModel m : artifacts) {
                if (gc.getGroupId() != null && gc.getGroupId().equals(m.group().value()) && gc.getArtifactId() != null && gc.getArtifactId().equals(m.name().value()) && !gc.getRevision().equals(m.version().value())) {
                    dependenciesModel.remove(m);
                    dependenciesModel.addArtifact(m.configurationName(), gc.toString());
                }
            }
        }
        buildModel.applyChanges();
    });
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)

Example 7 with GradleCoordinate

use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.

the class GradleDependencyManager method findMissingDependencies.

/**
   * Returns the dependencies that are NOT included in the specified module.
   * Note: the version of the dependency is disregarded.
   *
   * @param module the module to check dependencies in
   * @param dependencies the dependencies of interest.
   * @return a list of the dependencies NOT included in the module
   */
@NotNull
public List<GradleCoordinate> findMissingDependencies(@NotNull Module module, @NotNull Iterable<GradleCoordinate> dependencies) {
    AndroidModuleModel gradleModel = AndroidModuleModel.get(module);
    GradleBuildModel buildModel = GradleBuildModel.get(module);
    if (gradleModel == null && buildModel == null) {
        return Collections.emptyList();
    }
    RepositoryUrlManager manager = RepositoryUrlManager.get();
    List<GradleCoordinate> missingLibraries = Lists.newArrayList();
    for (GradleCoordinate coordinate : dependencies) {
        GradleCoordinate resolvedCoordinate = manager.resolveDynamicCoordinate(coordinate, null);
        if (resolvedCoordinate == null) {
            // We don't have anything installed, but we can keep trying with the unresolved coordinate if we have enough info
            if (coordinate.getArtifactId() == null || coordinate.getGroupId() == null) {
                // TODO Should this be an error ?
                continue;
            }
        } else {
            coordinate = resolvedCoordinate;
        }
        boolean dependencyFound = false;
        // First look in the model returned by Gradle.
        if (gradleModel != null && GradleUtil.dependsOn(gradleModel, String.format("%s:%s", coordinate.getGroupId(), coordinate.getArtifactId()))) {
            // GradleUtil.dependsOn method only checks the android library dependencies.
            // TODO: Consider updating it to also check for java library dependencies.
            dependencyFound = true;
        } else if (buildModel != null) {
            // Now, check in the model obtained from the gradle files.
            for (ArtifactDependencyModel dependency : buildModel.dependencies().artifacts(COMPILE)) {
                if (Objects.equal(coordinate.getGroupId(), dependency.group().value()) && Objects.equal(coordinate.getArtifactId(), dependency.name().value())) {
                    dependencyFound = true;
                    break;
                }
            }
        }
        if (!dependencyFound) {
            missingLibraries.add(coordinate);
        }
    }
    return missingLibraries;
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) RepositoryUrlManager(com.android.tools.idea.templates.RepositoryUrlManager) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with GradleCoordinate

use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.

the class GradleDependencyManager method userWantToAddDependencies.

private static boolean userWantToAddDependencies(@NotNull Module module, @NotNull Collection<GradleCoordinate> missing) {
    String libraryNames = StringUtil.join(missing, GradleCoordinate::getArtifactId, ", ");
    String message = String.format("This operation requires the %1$s %2$s. \n\nWould you like to add %3$s %1$s now?", pluralize("library", missing.size()), libraryNames, pluralize("this", missing.size()));
    Project project = module.getProject();
    return Messages.showOkCancelDialog(project, message, "Add Project Dependency", Messages.getErrorIcon()) == Messages.OK;
}
Also used : Project(com.intellij.openapi.project.Project) GradleCoordinate(com.android.ide.common.repository.GradleCoordinate)

Example 9 with GradleCoordinate

use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.

the class EclipseImportModule method initDependencies.

@Override
protected void initDependencies() {
    super.initDependencies();
    for (String reference : myProject.getInferredLibraries()) {
        if (reference.equals(APPCOMPAT_ARTIFACT)) {
            GradleCoordinate dependency = getAppCompatDependency();
            if (dependency != null) {
                myDependencies.add(dependency);
                myImporter.getSummary().reportReplacedLib(reference, Collections.singletonList(dependency));
            }
        } else if (reference.equals(SUPPORT_ARTIFACT)) {
            GradleCoordinate dependency = getSupportLibDependency();
            if (dependency != null) {
                myDependencies.add(dependency);
                myImporter.getSummary().reportReplacedLib(reference, Collections.singletonList(dependency));
            }
        } else if (reference.equals(GRIDLAYOUT_ARTIFACT)) {
            GradleCoordinate dependency = getGridLayoutDependency();
            if (dependency != null) {
                myDependencies.add(dependency);
                myImporter.getSummary().reportReplacedLib(reference, Collections.singletonList(dependency));
            }
        } else if (reference.equals(MEDIA_ROUTER_ARTIFACT)) {
            GradleCoordinate dependency = getMediaRouterDependency();
            if (dependency != null) {
                myDependencies.add(dependency);
                myImporter.getSummary().reportReplacedLib(reference, Collections.singletonList(dependency));
            }
        }
    }
    for (File jar : myProject.getJarPaths()) {
        if (myImporter.isReplaceJars()) {
            GradleCoordinate dependency = guessDependency(jar);
            if (dependency != null) {
                myDependencies.add(dependency);
                myImporter.getSummary().reportReplacedJar(jar, dependency);
                continue;
            }
        }
        myJarDependencies.add(getJarOutputRelativePath(jar));
    }
    for (File jar : myProject.getTestJarPaths()) {
        if (myImporter.isReplaceJars()) {
            GradleCoordinate dependency = guessDependency(jar);
            if (dependency != null) {
                myTestDependencies.add(dependency);
                myImporter.getSummary().reportReplacedJar(jar, dependency);
                continue;
            }
        }
        // Test jars unconditionally get copied into the libs/ folder
        myTestJarDependencies.add(getTestJarOutputRelativePath(jar));
    }
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) File(java.io.File)

Example 10 with GradleCoordinate

use of com.android.ide.common.repository.GradleCoordinate in project android by JetBrains.

the class GradleFilePsiMerger method mergeDependencies.

private static void mergeDependencies(@NotNull PsiElement fromRoot, @NotNull PsiElement toRoot, @NotNull Project project, @Nullable String supportLibVersionFilter) {
    Map<String, Multimap<String, GradleCoordinate>> dependencies = Maps.newHashMap();
    final List<String> unparsedDependencies = Lists.newArrayList();
    // Load existing dependencies into the map for the existing build.gradle
    pullDependenciesIntoMap(toRoot, dependencies, null);
    // Load dependencies into the map for the new build.gradle
    pullDependenciesIntoMap(fromRoot, dependencies, unparsedDependencies);
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    RepositoryUrlManager urlManager = RepositoryUrlManager.get();
    ImmutableList<String> configurations = CONFIGURATION_ORDERING.immutableSortedCopy(dependencies.keySet());
    AndroidSdkData sdk = AndroidSdks.getInstance().tryToChooseAndroidSdk();
    if (sdk != null) {
        for (String configurationName : configurations) {
            List<GradleCoordinate> resolved = urlManager.resolveDynamicSdkDependencies(dependencies.get(configurationName), supportLibVersionFilter, sdk, FileOpUtils.create());
            for (GradleCoordinate dependency : resolved) {
                PsiElement dependencyElement = factory.createStatementFromText(String.format("%s '%s'\n", configurationName, dependency.toString()));
                toRoot.addBefore(dependencyElement, toRoot.getLastChild());
            }
        }
    }
    for (String dependency : unparsedDependencies) {
        PsiElement dependencyElement = factory.createStatementFromText(dependency);
        toRoot.addBefore(dependencyElement, toRoot.getLastChild());
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GradleCoordinate (com.android.ide.common.repository.GradleCoordinate)35 File (java.io.File)9 GradleDependencyManager (com.android.tools.idea.gradle.dependencies.GradleDependencyManager)5 NotNull (org.jetbrains.annotations.NotNull)5 RemotePackage (com.android.repository.api.RemotePackage)4 AndroidSdkHandler (com.android.sdklib.repository.AndroidSdkHandler)3 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)3 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)3 StudioLoggerProgressIndicator (com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator)3 Project (com.intellij.openapi.project.Project)3 IOException (java.io.IOException)3 Nullable (org.jetbrains.annotations.Nullable)3 Nullable (com.android.annotations.Nullable)2 SdkMavenRepository (com.android.ide.common.repository.SdkMavenRepository)2 RepoManager (com.android.repository.api.RepoManager)2 RepoPackage (com.android.repository.api.RepoPackage)2 RepositoryPackages (com.android.repository.impl.meta.RepositoryPackages)2 FakeRepoManager (com.android.repository.testframework.FakeRepoManager)2 DependenciesModel (com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2