Search in sources :

Example 11 with GradleCoordinate

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

the class RepositoryUrlManager method resolveDynamicSdkDependencies.

/**
   * Resolves multiple dynamic dependencies on artifacts distributed in the SDK.
   *
   * <p>This method doesn't check any remote repositories, just the already downloaded SDK "extras" repositories.
   */
public List<GradleCoordinate> resolveDynamicSdkDependencies(@NotNull Multimap<String, GradleCoordinate> dependencies, @Nullable String supportLibVersionFilter, @NotNull AndroidSdkData sdk, @NotNull FileOp fileOp) {
    List<GradleCoordinate> result = Lists.newArrayListWithCapacity(dependencies.size());
    String supportFilter = findExistingExplicitVersion(dependencies.values());
    if (supportFilter != null) {
        supportLibVersionFilter = supportFilter;
    }
    for (String key : dependencies.keySet()) {
        GradleCoordinate highest = Collections.max(dependencies.get(key), COMPARE_PLUS_LOWER);
        if (highest.getGroupId() == null || highest.getArtifactId() == null) {
            return null;
        }
        // For test consistency, don't depend on installed SDK state while testing
        if (myForceRepositoryChecksInTests || !ApplicationManager.getApplication().isUnitTestMode()) {
            // If this coordinate points to an artifact in one of our repositories, check to see if there is a static version
            // that we can add instead of a plus revision.
            String filter = highest.getRevision();
            if (filter.endsWith("+")) {
                filter = filter.length() > 1 ? filter.substring(0, filter.length() - 1) : null;
                boolean includePreviews = false;
                if (filter == null && ImportModule.SUPPORT_GROUP_ID.equals(highest.getGroupId())) {
                    filter = supportLibVersionFilter;
                    includePreviews = true;
                }
                String version = getLibraryRevision(highest.getGroupId(), highest.getArtifactId(), filter, includePreviews, sdk.getLocation(), fileOp);
                if (version == null && filter != null) {
                    // No library found at the support lib version filter level, so look for any match
                    version = getLibraryRevision(highest.getGroupId(), highest.getArtifactId(), null, includePreviews, sdk.getLocation(), fileOp);
                }
                if (version == null && !includePreviews) {
                    // Still no library found, check preview versions
                    version = getLibraryRevision(highest.getGroupId(), highest.getArtifactId(), null, true, sdk.getLocation(), fileOp);
                }
                if (version != null) {
                    String libraryCoordinate = highest.getId() + ":" + version;
                    GradleCoordinate available = GradleCoordinate.parseCoordinateString(libraryCoordinate);
                    if (available != null) {
                        File archiveFile = getArchiveForCoordinate(available, sdk.getLocation(), fileOp);
                        if (((archiveFile != null && fileOp.exists(archiveFile)) || // Not a known library hardcoded in RepositoryUrlManager?
                        SupportLibrary.forGradleCoordinate(available) == null) && COMPARE_PLUS_LOWER.compare(available, highest) >= 0) {
                            highest = available;
                        }
                    }
                }
            }
        }
        result.add(highest);
    }
    return result;
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) File(java.io.File)

Example 12 with GradleCoordinate

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

the class RepositoryUrlManager method resolveDynamicCoordinateVersion.

@Nullable
@VisibleForTesting
String resolveDynamicCoordinateVersion(@NotNull GradleCoordinate coordinate, @Nullable Project project, @NotNull AndroidSdkHandler sdkHandler) {
    if (coordinate.getGroupId() == null || coordinate.getArtifactId() == null) {
        return null;
    }
    String filter = coordinate.getRevision();
    if (!filter.endsWith("+")) {
        // Already resolved. That was easy.
        return filter;
    }
    filter = filter.substring(0, filter.length() - 1);
    File sdkLocation = sdkHandler.getLocation();
    if (sdkLocation != null) {
        // If this coordinate points to an artifact in one of our repositories, mark it will a comment if they don't
        // have that repository available.
        String libraryCoordinate = getLibraryRevision(coordinate.getGroupId(), coordinate.getArtifactId(), filter, false, sdkLocation, sdkHandler.getFileOp());
        if (libraryCoordinate != null) {
            return libraryCoordinate;
        }
        // If that didn't yield any matches, try again, this time allowing preview platforms.
        // This is necessary if the artifact filter includes enough of a version where there are
        // only preview matches.
        libraryCoordinate = getLibraryRevision(coordinate.getGroupId(), coordinate.getArtifactId(), filter, true, sdkLocation, sdkHandler.getFileOp());
        if (libraryCoordinate != null) {
            return libraryCoordinate;
        }
    }
    // Regular Gradle dependency? Look in Gradle cache
    GradleVersion versionFound = GradleLocalCache.getInstance().findLatestArtifactVersion(coordinate, project, filter);
    if (versionFound != null) {
        return versionFound.toString();
    }
    // Maybe it's available for download as an SDK component
    RemotePackage sdkPackage = SdkMavenRepository.findLatestRemoteVersion(coordinate, sdkHandler, new StudioLoggerProgressIndicator(getClass()));
    if (sdkPackage != null) {
        GradleCoordinate found = SdkMavenRepository.getCoordinateFromSdkPath(sdkPackage.getPath());
        if (found != null) {
            return found.getRevision();
        }
    }
    // Perform network lookup to resolve current best version, if possible
    if (project != null) {
        LintClient client = new LintIdeClient(project);
        Revision latest = GradleDetector.getLatestVersionFromRemoteRepo(client, coordinate, coordinate.isPreview());
        if (latest != null) {
            String version = latest.toShortString();
            if (version.startsWith(filter)) {
                return version;
            }
        }
    }
    return null;
}
Also used : StudioLoggerProgressIndicator(com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator) GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) Revision(com.android.repository.Revision) LintClient(com.android.tools.lint.client.api.LintClient) LintIdeClient(com.android.tools.idea.lint.LintIdeClient) GradleVersion(com.android.ide.common.repository.GradleVersion) File(java.io.File) RemotePackage(com.android.repository.api.RemotePackage) VisibleForTesting(com.android.annotations.VisibleForTesting) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with GradleCoordinate

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

the class GradleBuildSystemService method addDependency.

/**
   * @param artifact The dependency artifact without version.
   *                 This method will add the ":+" to the given artifact.
   *                 For Guava, for example: the artifact coordinate will not include the version:
   *                 com.google.guava:guava
   *                 and this method will add "+" as the version of the dependency to add.
   */
@Override
public void addDependency(@NotNull Module module, @NotNull String artifact) {
    GradleDependencyManager manager = GradleDependencyManager.getInstance(module.getProject());
    GradleCoordinate coordinate = GradleCoordinate.parseCoordinateString(artifact + ":+");
    manager.ensureLibraryIsIncluded(module, Collections.singletonList(coordinate), null);
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) GradleDependencyManager(com.android.tools.idea.gradle.dependencies.GradleDependencyManager)

Example 14 with GradleCoordinate

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

the class ModuleDependenciesPanel method installRepositoryIfNeeded.

private String installRepositoryIfNeeded(String coordinateText) {
    GradleCoordinate gradleCoordinate = GradleCoordinate.parseCoordinateString(coordinateText);
    // Only allowed to click ok when the string is valid.
    assert gradleCoordinate != null;
    SupportLibrary supportLibrary = SupportLibrary.forGradleCoordinate(gradleCoordinate);
    if (!REVISION_ANY.equals(gradleCoordinate.getRevision()) || supportLibrary == null) {
        // No installation needed, or it's not a local repository.
        return coordinateText;
    }
    String message = "Library " + gradleCoordinate.getArtifactId() + " is not installed. Install repository?";
    if (Messages.showYesNoDialog(myProject, message, "Install Repository", Messages.getQuestionIcon()) != Messages.YES) {
        // User cancelled installation.
        return null;
    }
    List<String> requested = Lists.newArrayList();
    SdkMavenRepository repository;
    if (coordinateText.startsWith("com.android.support")) {
        repository = SdkMavenRepository.ANDROID;
    } else if (coordinateText.startsWith("com.google.android")) {
        repository = SdkMavenRepository.GOOGLE;
    } else {
        // EXTRAS_REPOSITORY.containsKey() should have returned false.
        assert false;
        return coordinateText + ':' + REVISION_ANY;
    }
    requested.add(repository.getPackageId());
    ModelWizardDialog dialog = SdkQuickfixUtils.createDialogForPaths(myProject, requested);
    if (dialog != null) {
        dialog.setTitle("Install Missing Components");
        if (dialog.showAndGet()) {
            return RepositoryUrlManager.get().getLibraryStringCoordinate(supportLibrary, true);
        }
    }
    // Installation wizard didn't complete - skip adding the dependency.
    return null;
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) SdkMavenRepository(com.android.ide.common.repository.SdkMavenRepository) SupportLibrary(com.android.tools.idea.templates.SupportLibrary) ModelWizardDialog(com.android.tools.idea.wizard.model.ModelWizardDialog)

Example 15 with GradleCoordinate

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

the class AndroidAddLibraryDependencyAction method addDependency.

/**
   * Adds the given dependency to the project.
   *
   * @param project
   * @param buildModel
   * @param coordinateString
   */
private static void addDependency(@NotNull final Project project, @NotNull final GradleBuildModel buildModel, @NotNull String coordinateString) {
    GradleCoordinate coordinate = GradleCoordinate.parseCoordinateString(coordinateString);
    if (coordinate == null || coordinate.getArtifactId() == null) {
        return;
    }
    final ArtifactDependencySpec newDependency = new ArtifactDependencySpec(coordinate.getArtifactId(), coordinate.getGroupId(), coordinate.getRevision());
    WriteCommandAction.runWriteCommandAction(project, new Runnable() {

        @Override
        public void run() {
            buildModel.dependencies().addArtifact(CommonConfigurationNames.COMPILE, newDependency);
            buildModel.applyChanges();
        }
    });
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) ArtifactDependencySpec(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec)

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