Search in sources :

Example 26 with GradleCoordinate

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

the class ImportModule method getLatestVersion.

@Nullable
public GradleCoordinate getLatestVersion(String artifact) {
    int compileVersion = GradleImport.CURRENT_COMPILE_VERSION;
    AndroidVersion version = getCompileSdkVersion();
    if (version != AndroidVersion.DEFAULT) {
        compileVersion = version.getFeatureLevel();
    }
    // from version 18 (earliest version where we have all the libs in the m2 repository)
    if (compileVersion < 18) {
        compileVersion = 18;
    }
    String compileVersionString = Integer.toString(compileVersion);
    if (myImporter.getSdkLocation() != null) {
        @SuppressWarnings("UnnecessaryLocalVariable") String filter = compileVersionString;
        GradleCoordinate max = SdkMavenRepository.ANDROID.getHighestInstalledVersion(myImporter.getSdkLocation(), SUPPORT_GROUP_ID, artifact, filter, true);
        if (max != null) {
            return max;
        }
    }
    String coordinate = SUPPORT_GROUP_ID + ':' + artifact + ':' + compileVersionString + ".+";
    return GradleCoordinate.parseCoordinateString(coordinate);
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) AndroidVersion(com.android.sdklib.AndroidVersion) Nullable(com.android.annotations.Nullable)

Example 27 with GradleCoordinate

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

the class RepositoryUrlManager method getLatestVersionFromMavenMetadata.

/**
   * Parses a Maven metadata file and returns a string of the highest found version
   *
   * @param metadataFile    the files to parse
   * @param includePreviews if false, preview versions of the library will not be returned
   * @return the string representing the highest version found in the file or "0.0.0" if no versions exist in the file
   */
@Nullable
private static String getLatestVersionFromMavenMetadata(@NotNull File metadataFile, @Nullable String filterPrefix, boolean includePreviews, @NotNull FileOp fileOp) throws IOException {
    String xml = fileOp.toString(metadataFile, StandardCharsets.UTF_8);
    List<GradleCoordinate> versions = Lists.newLinkedList();
    try {
        SAXParserFactory.newInstance().newSAXParser().parse(IOUtils.toInputStream(xml), new DefaultHandler() {

            boolean inVersionTag = false;

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                if (qName.equals(TAG_VERSION)) {
                    inVersionTag = true;
                }
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
                // Get the version and compare it to the current known max version
                if (inVersionTag) {
                    inVersionTag = false;
                    String revision = new String(ch, start, length);
                    //noinspection StatementWithEmptyBody
                    if (!includePreviews && "5.2.08".equals(revision) && metadataFile.getPath().contains(PLAY_SERVICES.getArtifactId())) {
                    // This version (despite not having -rcN in its version name is actually a preview
                    // (See https://code.google.com/p/android/issues/detail?id=75292)
                    // Ignore it
                    } else if (filterPrefix == null || revision.startsWith(filterPrefix)) {
                        versions.add(GradleCoordinate.parseVersionOnly(revision));
                    }
                }
            }
        });
    } catch (Exception e) {
        LOG.warn(e);
    }
    if (versions.isEmpty()) {
        return REVISION_ANY;
    } else if (includePreviews) {
        return GRADLE_COORDINATE_ORDERING.max(versions).getRevision();
    } else {
        return versions.stream().filter(v -> !v.isPreview()).max(GRADLE_COORDINATE_ORDERING).map(GradleCoordinate::getRevision).orElse(null);
    }
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) Nullable(org.jetbrains.annotations.Nullable)

Example 28 with GradleCoordinate

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

the class RepositoryUrlManager method getLibraryRevision.

/**
   * Returns the string for the specific version number of the most recent version of the given library
   * (matching the given prefix filter, if any) in one of the Sdk repositories.
   *
   * @param groupId         the group id
   * @param artifactId      the artifact id
   * @param filterPrefix    a prefix, if any
   * @param includePreviews whether to include preview versions of libraries
   * @return
   */
@Nullable
public String getLibraryRevision(@NotNull String groupId, @NotNull String artifactId, @Nullable String filterPrefix, boolean includePreviews, @NotNull File sdkLocation, @NotNull FileOp fileOp) {
    // Try the new, combined repository first:
    File combinedRepo = FileUtils.join(sdkLocation, FD_EXTRAS, FD_M2_REPOSITORY);
    if (fileOp.isDirectory(combinedRepo)) {
        GradleCoordinate versionInCombined = MavenRepositories.getHighestInstalledVersion(groupId, artifactId, combinedRepo, filterPrefix, includePreviews, fileOp);
        if (versionInCombined != null) {
            return versionInCombined.getRevision();
        }
    }
    // Now try the "old" repositories, "google" and "android":
    SdkMavenRepository repository = SdkMavenRepository.find(sdkLocation, groupId, artifactId, fileOp);
    if (repository == null) {
        // Try the repo embedded in AS. We distribute for example the constraint layout there for now.
        List<File> paths = EmbeddedDistributionPaths.getInstance().findAndroidStudioLocalMavenRepoPaths();
        for (File path : paths) {
            if (path != null && path.isDirectory()) {
                GradleCoordinate versionInEmbedded = MavenRepositories.getHighestInstalledVersion(groupId, artifactId, path, filterPrefix, includePreviews, fileOp);
                if (versionInEmbedded != null) {
                    return versionInEmbedded.getRevision();
                }
            }
        }
        return null;
    }
    File repositoryLocation = repository.getRepositoryLocation(sdkLocation, true, fileOp);
    if (repositoryLocation == null) {
        return null;
    }
    // Try using the POM file:
    File mavenMetadataFile = MavenRepositories.getMavenMetadataFile(repositoryLocation, groupId, artifactId);
    if (fileOp.isFile(mavenMetadataFile)) {
        try {
            return getLatestVersionFromMavenMetadata(mavenMetadataFile, filterPrefix, includePreviews, fileOp);
        } catch (IOException e) {
            return null;
        }
    }
    // Just scan all the directories:
    GradleCoordinate max = repository.getHighestInstalledVersion(sdkLocation, groupId, artifactId, filterPrefix, includePreviews, fileOp);
    if (max == null) {
        return null;
    }
    return max.getRevision();
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) SdkMavenRepository(com.android.ide.common.repository.SdkMavenRepository) IOException(java.io.IOException) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with GradleCoordinate

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

the class GradleDependencyManagerTest method testDependencyCanBeCancelledByUser.

public void testDependencyCanBeCancelledByUser() throws Exception {
    loadSimpleApplication();
    List<GradleCoordinate> dependencies = Collections.singletonList(RECYCLER_VIEW_DEPENDENCY);
    GradleDependencyManager dependencyManager = GradleDependencyManager.getInstance(getProject());
    assertThat(dependencyManager.findMissingDependencies(myModules.getAppModule(), dependencies)).isNotEmpty();
    Messages.setTestDialog(new TestMessagesDialog(Messages.NO));
    boolean found = dependencyManager.ensureLibraryIsIncluded(myModules.getAppModule(), dependencies, null);
    assertThat(found).isFalse();
    assertThat(dependencyManager.findMissingDependencies(myModules.getAppModule(), dependencies)).isNotEmpty();
}
Also used : TestMessagesDialog(com.android.tools.idea.testing.TestMessagesDialog) GradleCoordinate(com.android.ide.common.repository.GradleCoordinate)

Example 30 with GradleCoordinate

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

the class DependencyManager method checkForNewMissingDependencies.

private boolean checkForNewMissingDependencies() {
    Set<String> missing = Collections.emptySet();
    if (myModule != null) {
        GradleDependencyManager manager = GradleDependencyManager.getInstance(myProject);
        List<GradleCoordinate> coordinates = toGradleCoordinatesFromIds(myPalette.getGradleCoordinateIds());
        missing = fromGradleCoordinatesToIds(manager.findMissingDependencies(myModule, coordinates));
        if (myMissingLibraries.equals(missing)) {
            return false;
        }
    }
    myMissingLibraries.clear();
    myMissingLibraries.addAll(missing);
    return true;
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) GradleDependencyManager(com.android.tools.idea.gradle.dependencies.GradleDependencyManager)

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