Search in sources :

Example 1 with RepositoryUrlManager

use of com.android.tools.idea.templates.RepositoryUrlManager 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 2 with RepositoryUrlManager

use of com.android.tools.idea.templates.RepositoryUrlManager in project android by JetBrains.

the class InferSupportAnnotationsAction method checkModules.

// For Android we need to check SDK version and possibly update the gradle project file
protected boolean checkModules(@NotNull Project project, @NotNull AnalysisScope scope, @NotNull Map<Module, PsiFile> modules) {
    Set<Module> modulesWithoutAnnotations = new HashSet<>();
    Set<Module> modulesWithLowVersion = new HashSet<>();
    for (Module module : modules.keySet()) {
        AndroidModuleInfo info = AndroidModuleInfo.get(module);
        if (info != null && info.getBuildSdkVersion() != null && info.getBuildSdkVersion().getFeatureLevel() < MIN_SDK_WITH_NULLABLE) {
            modulesWithLowVersion.add(module);
        }
        GradleBuildModel buildModel = GradleBuildModel.get(module);
        if (buildModel == null) {
            Logger.getInstance(InferSupportAnnotationsAction.class).warn("Unable to find Gradle build model for module " + module.getModuleFilePath());
            continue;
        }
        boolean dependencyFound = false;
        DependenciesModel dependenciesModel = buildModel.dependencies();
        if (dependenciesModel != null) {
            for (ArtifactDependencyModel dependency : dependenciesModel.artifacts(COMPILE)) {
                String notation = dependency.compactNotation().value();
                if (notation.startsWith(SdkConstants.APPCOMPAT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.SUPPORT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.ANNOTATIONS_LIB_ARTIFACT)) {
                    dependencyFound = true;
                    break;
                }
            }
        }
        if (!dependencyFound) {
            modulesWithoutAnnotations.add(module);
        }
    }
    if (!modulesWithLowVersion.isEmpty()) {
        Messages.showErrorDialog(project, String.format("Infer Support Annotations requires the project sdk level be set to %1$d or greater.", MIN_SDK_WITH_NULLABLE), "Infer Support Annotations");
        return false;
    }
    if (modulesWithoutAnnotations.isEmpty()) {
        return true;
    }
    String moduleNames = StringUtil.join(modulesWithoutAnnotations, Module::getName, ", ");
    int count = modulesWithoutAnnotations.size();
    String message = String.format("The %1$s %2$s %3$sn't refer to the existing '%4$s' library with Android nullity annotations. \n\n" + "Would you like to add the %5$s now?", pluralize("module", count), moduleNames, count > 1 ? "do" : "does", SupportLibrary.SUPPORT_ANNOTATIONS.getArtifactId(), pluralize("dependency", count));
    if (Messages.showOkCancelDialog(project, message, "Infer Nullity Annotations", Messages.getErrorIcon()) == Messages.OK) {
        LocalHistoryAction action = LocalHistory.getInstance().startAction(ADD_DEPENDENCY);
        try {
            new WriteCommandAction(project, ADD_DEPENDENCY) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    RepositoryUrlManager manager = RepositoryUrlManager.get();
                    String annotationsLibraryCoordinate = manager.getLibraryStringCoordinate(SupportLibrary.SUPPORT_ANNOTATIONS, true);
                    for (Module module : modulesWithoutAnnotations) {
                        addDependency(module, annotationsLibraryCoordinate);
                    }
                    GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setGenerateSourcesOnSuccess(false);
                    GradleSyncInvoker.getInstance().requestProjectSync(project, request, new GradleSyncListener.Adapter() {

                        @Override
                        public void syncSucceeded(@NotNull Project project) {
                            restartAnalysis(project, scope);
                        }
                    });
                }
            }.execute();
        } finally {
            action.finish();
        }
    }
    return false;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RepositoryUrlManager(com.android.tools.idea.templates.RepositoryUrlManager) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) Project(com.intellij.openapi.project.Project) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) LocalHistoryAction(com.intellij.history.LocalHistoryAction) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) Module(com.intellij.openapi.module.Module)

Example 3 with RepositoryUrlManager

use of com.android.tools.idea.templates.RepositoryUrlManager in project android by JetBrains.

the class AndroidAddLibraryDependencyAction method findAllDependencies.

/**
   * Finds all the "extras repository" dependencies that haven't been already added to the project.
   */
@NotNull
private static ImmutableCollection<String> findAllDependencies(@NotNull GradleBuildModel buildModel) {
    HashSet<String> existingDependencies = Sets.newHashSet();
    for (ArtifactDependencyModel dependency : buildModel.dependencies().artifacts()) {
        existingDependencies.add(dependency.group().value() + ":" + dependency.name().value());
    }
    ImmutableList.Builder<String> dependenciesBuilder = ImmutableList.builder();
    RepositoryUrlManager repositoryUrlManager = RepositoryUrlManager.get();
    for (SupportLibrary library : SupportLibrary.values()) {
        // Coordinate for any version available
        GradleCoordinate libraryCoordinate = library.getGradleCoordinate("+");
        // Get from the library coordinate only the group and artifactId to check if we have already added it
        if (!existingDependencies.contains(libraryCoordinate.getId())) {
            GradleCoordinate coordinate = repositoryUrlManager.resolveDynamicCoordinate(libraryCoordinate, buildModel.getProject());
            if (coordinate != null) {
                dependenciesBuilder.add(coordinate.toString());
            }
        }
    }
    return dependenciesBuilder.build();
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) ImmutableList(com.google.common.collect.ImmutableList) RepositoryUrlManager(com.android.tools.idea.templates.RepositoryUrlManager) SupportLibrary(com.android.tools.idea.templates.SupportLibrary) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with RepositoryUrlManager

use of com.android.tools.idea.templates.RepositoryUrlManager in project android by JetBrains.

the class AndroidInferNullityAnnotationAction method checkModules.

// For Android we need to check SDK version and possibly update the gradle project file
protected boolean checkModules(@NotNull Project project, @NotNull AnalysisScope scope, @NotNull Map<Module, PsiFile> modules) {
    Set<Module> modulesWithoutAnnotations = new HashSet<>();
    Set<Module> modulesWithLowVersion = new HashSet<>();
    for (Module module : modules.keySet()) {
        AndroidModuleInfo info = AndroidModuleInfo.get(module);
        if (info != null && info.getBuildSdkVersion() != null && info.getBuildSdkVersion().getFeatureLevel() < MIN_SDK_WITH_NULLABLE) {
            modulesWithLowVersion.add(module);
        }
        GradleBuildModel buildModel = GradleBuildModel.get(module);
        if (buildModel == null) {
            LOG.warn("Unable to find Gradle build model for module " + module.getModuleFilePath());
            continue;
        }
        boolean dependencyFound = false;
        DependenciesModel dependenciesModel = buildModel.dependencies();
        if (dependenciesModel != null) {
            for (ArtifactDependencyModel dependency : dependenciesModel.artifacts(COMPILE)) {
                String notation = dependency.compactNotation().value();
                if (notation.startsWith(SdkConstants.APPCOMPAT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.SUPPORT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.ANNOTATIONS_LIB_ARTIFACT)) {
                    dependencyFound = true;
                    break;
                }
            }
        }
        if (!dependencyFound) {
            modulesWithoutAnnotations.add(module);
        }
    }
    if (!modulesWithLowVersion.isEmpty()) {
        Messages.showErrorDialog(project, String.format("Infer Nullity Annotations requires the project sdk level be set to %1$d or greater.", MIN_SDK_WITH_NULLABLE), "Infer Nullity Annotations");
        return false;
    }
    if (modulesWithoutAnnotations.isEmpty()) {
        return true;
    }
    String moduleNames = StringUtil.join(modulesWithoutAnnotations, Module::getName, ", ");
    int count = modulesWithoutAnnotations.size();
    String message = String.format("The %1$s %2$s %3$sn't refer to the existing '%4$s' library with Android nullity annotations. \n\n" + "Would you like to add the %5$s now?", pluralize("module", count), moduleNames, count > 1 ? "do" : "does", SupportLibrary.SUPPORT_ANNOTATIONS.getArtifactId(), pluralize("dependency", count));
    if (Messages.showOkCancelDialog(project, message, "Infer Nullity Annotations", Messages.getErrorIcon()) == Messages.OK) {
        LocalHistoryAction action = LocalHistory.getInstance().startAction(ADD_DEPENDENCY);
        try {
            new WriteCommandAction(project, ADD_DEPENDENCY) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    RepositoryUrlManager manager = RepositoryUrlManager.get();
                    String annotationsLibraryCoordinate = manager.getLibraryStringCoordinate(SupportLibrary.SUPPORT_ANNOTATIONS, true);
                    for (Module module : modulesWithoutAnnotations) {
                        addDependency(module, annotationsLibraryCoordinate);
                    }
                    GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setGenerateSourcesOnSuccess(false);
                    GradleSyncInvoker.getInstance().requestProjectSync(project, request, new GradleSyncListener.Adapter() {

                        @Override
                        public void syncSucceeded(@NotNull Project project) {
                            restartAnalysis(project, scope);
                        }
                    });
                }
            }.execute();
        } finally {
            action.finish();
        }
    }
    return false;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RepositoryUrlManager(com.android.tools.idea.templates.RepositoryUrlManager) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) Project(com.intellij.openapi.project.Project) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) LocalHistoryAction(com.intellij.history.LocalHistoryAction) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) Module(com.intellij.openapi.module.Module)

Example 5 with RepositoryUrlManager

use of com.android.tools.idea.templates.RepositoryUrlManager in project android by JetBrains.

the class NewModuleWizardState method updateDependencies.

/**
   * Updates the dependencies stored in the parameters map, to include support libraries required by the extra features selected.
   */
public void updateDependencies() {
    @SuppressWarnings("unchecked") SetMultimap<String, String> dependencies = (SetMultimap<String, String>) get(ATTR_DEPENDENCIES_MULTIMAP);
    if (dependencies == null) {
        dependencies = HashMultimap.create();
    }
    RepositoryUrlManager urlManager = RepositoryUrlManager.get();
    // Support Library
    Object fragmentsExtra = get(ATTR_FRAGMENTS_EXTRA);
    Object navigationDrawerExtra = get(ATTR_NAVIGATION_DRAWER_EXTRA);
    if ((fragmentsExtra != null && Boolean.parseBoolean(fragmentsExtra.toString())) || (navigationDrawerExtra != null && Boolean.parseBoolean(navigationDrawerExtra.toString()))) {
        dependencies.put(SdkConstants.GRADLE_COMPILE_CONFIGURATION, urlManager.getLibraryStringCoordinate(SupportLibrary.SUPPORT_V4, true));
    }
    // AppCompat Library
    Object actionBarExtra = get(ATTR_ACTION_BAR_EXTRA);
    if (actionBarExtra != null && Boolean.parseBoolean(actionBarExtra.toString())) {
        dependencies.put(SdkConstants.GRADLE_COMPILE_CONFIGURATION, urlManager.getLibraryStringCoordinate(SupportLibrary.APP_COMPAT_V7, true));
    }
    // GridLayout Library
    Object gridLayoutExtra = get(ATTR_GRID_LAYOUT_EXTRA);
    if (gridLayoutExtra != null && Boolean.parseBoolean(gridLayoutExtra.toString())) {
        dependencies.put(SdkConstants.GRADLE_COMPILE_CONFIGURATION, urlManager.getLibraryStringCoordinate(SupportLibrary.GRID_LAYOUT_V7, true));
    }
    put(ATTR_DEPENDENCIES_MULTIMAP, dependencies);
}
Also used : SetMultimap(com.google.common.collect.SetMultimap) RepositoryUrlManager(com.android.tools.idea.templates.RepositoryUrlManager)

Aggregations

RepositoryUrlManager (com.android.tools.idea.templates.RepositoryUrlManager)5 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)4 NotNull (org.jetbrains.annotations.NotNull)4 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)3 GradleCoordinate (com.android.ide.common.repository.GradleCoordinate)2 DependenciesModel (com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)2 AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)2 LocalHistoryAction (com.intellij.history.LocalHistoryAction)2 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 Module (com.intellij.openapi.module.Module)2 Project (com.intellij.openapi.project.Project)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 SupportLibrary (com.android.tools.idea.templates.SupportLibrary)1 ImmutableList (com.google.common.collect.ImmutableList)1 SetMultimap (com.google.common.collect.SetMultimap)1