Search in sources :

Example 36 with GradleVersion

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

the class UpgradeAppenginePluginVersionHyperlink method execute.

@Override
protected void execute(@NotNull Project project) {
    ExternalRepository repository = ServiceManager.getService(ExternalRepository.class);
    GradleVersion latest = repository.getLatest(APPENGINE_PLUGIN_GROUP_ID, APPENGINE_PLUGIN_ARTIFACT_ID);
    if (latest == null) {
        latest = DEFAULT_APPENGINE_PLUGIN_VERSION;
    }
    myDependency.setVersion(latest.toString());
    runWriteCommandAction(project, myBuildModel::applyChanges);
    GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(project, null);
}
Also used : ExternalRepository(com.android.tools.idea.gradle.service.repo.ExternalRepository) GradleVersion(com.android.ide.common.repository.GradleVersion)

Example 37 with GradleVersion

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

the class PsLibraryDependency method setVersion.

default default void setVersion(@NotNull String version) {
    boolean modified = false;
    ArtifactDependencyModel reference = null;
    for (DependencyModel parsedDependency : getParsedModels()) {
        if (parsedDependency instanceof ArtifactDependencyModel) {
            ArtifactDependencyModel dependency = (ArtifactDependencyModel) parsedDependency;
            dependency.setVersion(version);
            if (reference == null) {
                reference = dependency;
            }
            modified = true;
        }
    }
    if (modified) {
        GradleVersion parsedVersion = GradleVersion.parse(version);
        PsArtifactDependencySpec resolvedSpec = getResolvedSpec();
        String resolvedVersion = nullToEmpty(resolvedSpec.version);
        if (parsedVersion.compareTo(resolvedVersion) != 0) {
            // Update the "resolved" spec with the new version
            resolvedSpec = new PsArtifactDependencySpec(resolvedSpec.name, resolvedSpec.group, version);
            setResolvedSpec(resolvedSpec);
        }
        setDeclaredSpec(createSpec(reference));
        setModified(true);
        getParent().fireDependencyModifiedEvent((PsDependency) this);
    }
}
Also used : DependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependencyModel) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) GradleVersion(com.android.ide.common.repository.GradleVersion)

Example 38 with GradleVersion

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

the class PsParsedDependencies method findLibraryDependency.

@Nullable
public ArtifactDependencyModel findLibraryDependency(@NotNull GradleModuleVersion moduleVersion) {
    Collection<ArtifactDependencyModel> potentialMatches = myParsedArtifactDependencies.get(createIdFrom(moduleVersion));
    if (potentialMatches.size() == 1) {
        // Only one found. Just use it.
        return getFirstItem(potentialMatches);
    }
    String version = nullToEmpty(moduleVersion.getVersion());
    Map<GradleVersion, ArtifactDependencyModel> dependenciesByVersion = Maps.newHashMap();
    for (ArtifactDependencyModel potentialMatch : potentialMatches) {
        String potentialVersion = nullToEmpty(potentialMatch.version().value());
        if (version.equals(potentialVersion)) {
            // Perfect version match. Use it.
            return potentialMatch;
        }
        if (isNotEmpty(potentialVersion)) {
            // Collect all the "parsed" dependencies with same group and name, to make a best guess later.
            GradleVersion parsedVersion = GradleVersion.tryParse(potentialVersion);
            if (parsedVersion != null) {
                dependenciesByVersion.put(parsedVersion, potentialMatch);
            }
        }
    }
    if (isNotEmpty(version) && !dependenciesByVersion.isEmpty()) {
        GradleVersion parsedVersion = GradleVersion.tryParse(version);
        if (parsedVersion != null) {
            for (GradleVersion potentialVersion : dependenciesByVersion.keySet()) {
                if (parsedVersion.compareTo(potentialVersion) > 0) {
                    return dependenciesByVersion.get(potentialVersion);
                }
            }
        }
    }
    return null;
}
Also used : ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) GradleVersion(com.android.ide.common.repository.GradleVersion) Nullable(org.jetbrains.annotations.Nullable)

Example 39 with GradleVersion

use of com.android.ide.common.repository.GradleVersion 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 40 with GradleVersion

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

the class GradleSyncState method syncEnded.

public void syncEnded() {
    LOG.info(String.format("Sync with Gradle successful for project '%1$s'.", myProject.getName()));
    addInfoToEventLog("Gradle sync completed");
    // Temporary: Clear resourcePrefix flag in case it was set to false when working with
    // an older model. TODO: Remove this when we no longer support models older than 0.10.
    //noinspection AssignmentToStaticFieldFromInstanceMethod
    LintUtils.sTryPrefixLookup = true;
    syncFinished();
    syncPublisher(() -> myMessageBus.syncPublisher(GRADLE_SYNC_TOPIC).syncSucceeded(myProject));
    GradleVersion gradleVersion = GradleVersions.getInstance().getGradleVersion(myProject);
    String gradleVersionString = gradleVersion != null ? gradleVersion.toString() : "";
    // @formatter:off
    AndroidStudioEvent.Builder event = AndroidStudioEvent.newBuilder().setCategory(GRADLE_SYNC).setKind(GRADLE_SYNC_ENDED).setGradleVersion(gradleVersionString);
    // @formatter:on
    UsageTracker.getInstance().log(event);
}
Also used : GradleVersion(com.android.ide.common.repository.GradleVersion) AndroidStudioEvent(com.google.wireless.android.sdk.stats.AndroidStudioEvent)

Aggregations

GradleVersion (com.android.ide.common.repository.GradleVersion)77 File (java.io.File)25 Test (org.junit.Test)16 Module (com.intellij.openapi.module.Module)12 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)11 Nullable (org.jetbrains.annotations.Nullable)10 Project (com.intellij.openapi.project.Project)6 NotNull (org.jetbrains.annotations.NotNull)6 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)5 IOException (java.io.IOException)5 AndroidPluginInfo (com.android.tools.idea.gradle.plugin.AndroidPluginInfo)4 FixAndroidGradlePluginVersionHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.FixAndroidGradlePluginVersionHyperlink)3 NotificationHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink)3 SyncMessage (com.android.tools.idea.gradle.project.sync.messages.SyncMessage)3 ZipFile (java.util.zip.ZipFile)3 VisibleForTesting (com.android.annotations.VisibleForTesting)2 Variant (com.android.builder.model.Variant)2 IDevice (com.android.ddmlib.IDevice)2 Revision (com.android.repository.Revision)2 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)2