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;
}
}
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;
}
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();
}
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);
}
});
}
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
}
Aggregations