Search in sources :

Example 6 with GradleWrapper

use of com.android.tools.idea.gradle.util.GradleWrapper in project android by JetBrains.

the class InstantRunConfigurable method updateProjectToInstantRunTools.

/** Update versions relevant for Instant Run, and trigger a Gradle sync if successful */
public static boolean updateProjectToInstantRunTools(@NotNull Project project, @Nullable GradleSyncListener listener) {
    String pluginVersion = MINIMUM_GRADLE_PLUGIN_VERSION_STRING;
    // Pick max version of "recommended Gradle plugin" and "minimum required for instant run"
    if (GradleVersion.parse(GRADLE_PLUGIN_RECOMMENDED_VERSION).compareTo(MINIMUM_GRADLE_PLUGIN_VERSION) > 0) {
        pluginVersion = GRADLE_PLUGIN_RECOMMENDED_VERSION;
    }
    // Update plugin version
    AndroidPluginVersionUpdater updater = AndroidPluginVersionUpdater.getInstance(project);
    UpdateResult result = updater.updatePluginVersion(GradleVersion.parse(pluginVersion), GradleVersion.parse(GRADLE_LATEST_VERSION));
    if (result.isPluginVersionUpdated() && result.versionUpdateSuccess()) {
        // Should be at least 23.0.2
        String buildToolsVersion = "23.0.2";
        AndroidSdkHandler sdk = AndroidSdks.getInstance().tryToChooseSdkHandler();
        BuildToolInfo latestBuildTool = sdk.getLatestBuildTool(new StudioLoggerProgressIndicator(InstantRunConfigurable.class), false);
        if (latestBuildTool != null) {
            Revision revision = latestBuildTool.getRevision();
            if (revision.compareTo(Revision.parseRevision(buildToolsVersion)) > 0) {
                buildToolsVersion = revision.toShortString();
            }
        }
        // Also update build files to set build tools version 23.0.2
        GradleUtil.setBuildToolsVersion(project, buildToolsVersion);
        // Also update Gradle wrapper version
        GradleWrapper gradleWrapper = GradleWrapper.find(project);
        if (gradleWrapper != null) {
            gradleWrapper.updateDistributionUrlAndDisplayFailure(GRADLE_LATEST_VERSION);
        }
        // Request a sync
        GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setRunInBackground(false);
        GradleSyncInvoker.getInstance().requestProjectSync(project, request, listener);
        return true;
    } else {
        return false;
    }
}
Also used : StudioLoggerProgressIndicator(com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator) GradleSyncInvoker(com.android.tools.idea.gradle.project.sync.GradleSyncInvoker) Revision(com.android.repository.Revision) BuildToolInfo(com.android.sdklib.BuildToolInfo) AndroidSdkHandler(com.android.sdklib.repository.AndroidSdkHandler) AndroidPluginVersionUpdater(com.android.tools.idea.gradle.plugin.AndroidPluginVersionUpdater) UpdateResult(com.android.tools.idea.gradle.plugin.AndroidPluginVersionUpdater.UpdateResult) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper)

Example 7 with GradleWrapper

use of com.android.tools.idea.gradle.util.GradleWrapper in project android by JetBrains.

the class GradleDslMethodNotFoundErrorHandler method isUsingWrapper.

private static boolean isUsingWrapper(@NotNull Project project) {
    GradleProjectSettings gradleSettings = GradleProjectSettingsFinder.getInstance().findGradleProjectSettings(project);
    GradleWrapper gradleWrapper = GradleWrapper.find(project);
    DistributionType distributionType = gradleSettings != null ? gradleSettings.getDistributionType() : null;
    return (distributionType == null || distributionType == DistributionType.DEFAULT_WRAPPED) && gradleWrapper != null;
}
Also used : GradleProjectSettings(org.jetbrains.plugins.gradle.settings.GradleProjectSettings) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper) DistributionType(org.jetbrains.plugins.gradle.settings.DistributionType)

Example 8 with GradleWrapper

use of com.android.tools.idea.gradle.util.GradleWrapper in project android by JetBrains.

the class AndroidProjectConfigurable method reset.

@Override
public void reset() {
    myProjectProperties.clear();
    if (myGradleBuildFile == null) {
        return;
    }
    for (BuildFileKey key : PROJECT_PROPERTIES) {
        Object value = myGradleBuildFile.getValue(key);
        if (value != null) {
            myProjectProperties.put(key, value);
        }
    }
    try {
        GradleWrapper gradleWrapper = GradleWrapper.find(myProject);
        if (gradleWrapper != null) {
            String wrapperVersion = gradleWrapper.getGradleVersion();
            myProjectProperties.put(BuildFileKey.GRADLE_WRAPPER_VERSION, wrapperVersion);
        }
    } catch (Exception e) {
        LOG.warn("Error while saving Gradle wrapper properties", e);
    }
    myKeyValuePane.setCurrentBuildFileObject(myProjectProperties);
    myKeyValuePane.updateUiFromCurrentObject();
}
Also used : BuildFileKey(com.android.tools.idea.gradle.parser.BuildFileKey) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper) ConfigurationException(com.intellij.openapi.options.ConfigurationException)

Example 9 with GradleWrapper

use of com.android.tools.idea.gradle.util.GradleWrapper in project android by JetBrains.

the class AndroidProjectConfigurable method apply.

@Override
public void apply() throws ConfigurationException {
    if (myGradleBuildFile == null) {
        return;
    }
    VirtualFile file = myGradleBuildFile.getFile();
    if (!ReadonlyStatusHandler.ensureFilesWritable(myProject, file)) {
        throw new ConfigurationException(String.format("Build file %1$s is not writable", file.getPath()));
    }
    CommandProcessor.getInstance().runUndoTransparentAction(() -> {
        try {
            ActionRunner.runInsideWriteAction(() -> {
                for (BuildFileKey key : PROJECT_PROPERTIES) {
                    if (key == BuildFileKey.GRADLE_WRAPPER_VERSION || !myModifiedKeys.contains(key)) {
                        continue;
                    }
                    Object value = myProjectProperties.get(key);
                    if (value != null) {
                        myGradleBuildFile.setValue(key, value);
                    } else {
                        myGradleBuildFile.removeValue(null, key);
                    }
                }
                Object wrapperVersion = myProjectProperties.get(BuildFileKey.GRADLE_WRAPPER_VERSION);
                GradleWrapper gradleWrapper = GradleWrapper.find(myProject);
                if (wrapperVersion != null && gradleWrapper != null) {
                    boolean updated = gradleWrapper.updateDistributionUrlAndDisplayFailure(wrapperVersion.toString());
                    if (updated) {
                        VirtualFile virtualFile = gradleWrapper.getPropertiesFile();
                        if (virtualFile != null) {
                            virtualFile.refresh(false, false);
                        }
                    }
                }
                myModifiedKeys.clear();
            });
        } catch (Exception e) {
            LOG.error("Error while applying changes", e);
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ConfigurationException(com.intellij.openapi.options.ConfigurationException) BuildFileKey(com.android.tools.idea.gradle.parser.BuildFileKey) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper) ConfigurationException(com.intellij.openapi.options.ConfigurationException)

Example 10 with GradleWrapper

use of com.android.tools.idea.gradle.util.GradleWrapper in project android by JetBrains.

the class VersionCompatibilityCheckerTest method testCheckAndReportComponentIncompatibilities.

public void testCheckAndReportComponentIncompatibilities() throws Exception {
    loadSimpleApplication();
    mySyncMessagesStub.clearReportedMessages();
    GradleWrapper gradleWrapper = GradleWrapper.find(getProject());
    assertNotNull(gradleWrapper);
    String gradleVersion = gradleWrapper.getGradleVersion();
    assertNotNull(gradleVersion);
    Module appModule = myModules.getAppModule();
    AndroidModuleModel androidModel = AndroidModuleModel.get(appModule);
    assertNotNull(androidModel);
    GradleVersion currentModelVersion = androidModel.getModelVersion();
    assertNotNull(currentModelVersion);
    String failureMessage = "Please use Android Gradle plugin 1.5.0 or newer.";
    @Language("XML") String metadata = "<compatibility version='1'>\n" + "  <check failureType='error'>\n" + "    <component name='gradle' version='[" + gradleVersion + ", +)'>\n" + "      <requires name='android-gradle-plugin' version='[100.0.0, +]'>\n" + "        <failureMsg>\n" + "           <![CDATA[\n" + failureMessage + "\n" + "]]>\n" + "        </failureMsg>\n" + "      </requires>\n" + "    </component>\n" + "  </check>\n" + "</compatibility>";
    VersionCompatibilityChecker checker = VersionCompatibilityChecker.getInstance();
    checker.reloadMetadataForTesting(metadata);
    checker.checkAndReportComponentIncompatibilities(getProject());
    SyncMessage message = mySyncMessagesStub.getFirstReportedMessage();
    String firstLine = String.format("Gradle %1$s requires Android Gradle plugin 100.0.0 (or newer) but project is using version %2$s.", gradleVersion, currentModelVersion);
    // @formatter:off
    assertAbout(syncMessage()).that(message).hasGroup("Gradle Sync Issues").hasType(ERROR).hasMessageLine(firstLine, 0).hasMessageLine(failureMessage, 1);
// @formatter:on
}
Also used : SyncMessage(com.android.tools.idea.gradle.project.sync.messages.SyncMessage) Language(org.intellij.lang.annotations.Language) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Module(com.intellij.openapi.module.Module) GradleVersion(com.android.ide.common.repository.GradleVersion) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper)

Aggregations

GradleWrapper (com.android.tools.idea.gradle.util.GradleWrapper)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 NotNull (org.jetbrains.annotations.NotNull)3 GradleProjectSettings (org.jetbrains.plugins.gradle.settings.GradleProjectSettings)3 GradleVersion (com.android.ide.common.repository.GradleVersion)2 BuildFileKey (com.android.tools.idea.gradle.parser.BuildFileKey)2 NotificationHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink)2 OpenGradleSettingsHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.OpenGradleSettingsHyperlink)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)2 File (java.io.File)2 DistributionType (org.jetbrains.plugins.gradle.settings.DistributionType)2 Revision (com.android.repository.Revision)1 BuildToolInfo (com.android.sdklib.BuildToolInfo)1 AndroidSdkHandler (com.android.sdklib.repository.AndroidSdkHandler)1 TestUtils.getWorkspaceFile (com.android.testutils.TestUtils.getWorkspaceFile)1 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)1 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)1 DependenciesModel (com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)1 AndroidPluginVersionUpdater (com.android.tools.idea.gradle.plugin.AndroidPluginVersionUpdater)1 UpdateResult (com.android.tools.idea.gradle.plugin.AndroidPluginVersionUpdater.UpdateResult)1