use of com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel in project android by JetBrains.
the class PsModule method addLibraryDependencyToParsedModel.
protected void addLibraryDependencyToParsedModel(@NotNull List<String> configurationNames, @NotNull String compactNotation) {
GradleBuildModel parsedModel = getParsedModel();
if (parsedModel != null) {
DependenciesModel dependencies = parsedModel.dependencies();
configurationNames.forEach(configurationName -> dependencies.addArtifact(configurationName, compactNotation));
getParsedDependencies().reset(getParsedModel());
}
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel in project android by JetBrains.
the class AndroidPluginInfo method searchInBuildFiles.
@NotNull
private static BuildFileSearchResult searchInBuildFiles(@NotNull Project project, boolean searchForAppModule) {
BuildFileSearchResult result = new BuildFileSearchResult();
BuildFileProcessor.getInstance().processRecursively(project, buildModel -> {
boolean keepSearchingForAppModule = searchForAppModule && result.appVirtualFile == null;
if (keepSearchingForAppModule) {
List<String> pluginIds = getValues(buildModel.appliedPlugins());
for (AndroidPluginGeneration generation : AndroidPluginGeneration.values()) {
if (generation.isApplicationPluginIdIn(pluginIds)) {
result.appVirtualFile = buildModel.getVirtualFile();
result.pluginGeneration = generation;
keepSearchingForAppModule = false;
break;
}
}
}
boolean keepSearchingForPluginVersion = result.pluginVersion == null;
if (keepSearchingForPluginVersion) {
DependenciesModel dependencies = buildModel.buildscript().dependencies();
for (ArtifactDependencyModel dependency : dependencies.artifacts(CLASSPATH)) {
for (AndroidPluginGeneration generation : AndroidPluginGeneration.values()) {
if (generation.isAndroidPlugin(dependency.name().value(), dependency.group().value())) {
String version = dependency.version().value();
if (isNotEmpty(version)) {
result.pluginVirtualFile = buildModel.getVirtualFile();
result.pluginVersion = version;
}
keepSearchingForPluginVersion = false;
break;
}
}
if (!keepSearchingForPluginVersion) {
break;
}
}
}
return keepSearchingForAppModule || keepSearchingForPluginVersion;
});
return result;
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel in project android by JetBrains.
the class AndroidPluginVersionUpdater method updatePluginVersion.
/**
* Updates the plugin version and, optionally, the Gradle version used by the project.
*
* @param pluginVersion the plugin version to update to.
* @param gradleVersion the version of Gradle to update to (optional.)
* @return the result of the update operation.
*/
@NotNull
public UpdateResult updatePluginVersion(@NotNull GradleVersion pluginVersion, @Nullable GradleVersion gradleVersion) {
List<GradleBuildModel> modelsToUpdate = Lists.newArrayList();
BuildFileProcessor.getInstance().processRecursively(myProject, buildModel -> {
DependenciesModel dependencies = buildModel.buildscript().dependencies();
for (ArtifactDependencyModel dependency : dependencies.artifacts(CLASSPATH)) {
String artifactId = dependency.name().value();
String groupId = dependency.group().value();
if (AndroidPluginGeneration.find(artifactId, groupId) != null) {
String versionValue = dependency.version().value();
if (isEmpty(versionValue) || pluginVersion.compareTo(versionValue) != 0) {
dependency.setVersion(pluginVersion.toString());
modelsToUpdate.add(buildModel);
}
break;
}
}
return true;
});
UpdateResult result = new UpdateResult();
boolean updateModels = !modelsToUpdate.isEmpty();
if (updateModels) {
try {
runWriteCommandAction(myProject, new ThrowableComputable<Void, RuntimeException>() {
@Override
public Void compute() {
for (GradleBuildModel buildModel : modelsToUpdate) {
buildModel.applyChanges();
}
result.pluginVersionUpdated();
return null;
}
});
} catch (Throwable e) {
result.setPluginVersionUpdateError(e);
}
}
if (gradleVersion != null) {
String basePath = myProject.getBasePath();
if (basePath != null) {
try {
File wrapperPropertiesFilePath = getDefaultPropertiesFilePath(new File(basePath));
GradleWrapper gradleWrapper = GradleWrapper.get(wrapperPropertiesFilePath);
String current = gradleWrapper.getGradleVersion();
GradleVersion parsedCurrent = null;
if (current != null) {
parsedCurrent = GradleVersion.tryParse(current);
}
if (parsedCurrent != null && !isSupportedGradleVersion(parsedCurrent)) {
gradleWrapper.updateDistributionUrl(gradleVersion.toString());
result.gradleVersionUpdated();
}
} catch (Throwable e) {
result.setGradleVersionUpdateError(e);
}
}
}
return result;
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel in project android by JetBrains.
the class AndroidInferNullityAnnotationAction method checkModules.
// For Android we need to check SDK version and possibly update the gradle project file
protected boolean checkModules(@NotNull Project project, @NotNull AnalysisScope scope, @NotNull Map<Module, PsiFile> modules) {
Set<Module> modulesWithoutAnnotations = new HashSet<>();
Set<Module> modulesWithLowVersion = new HashSet<>();
for (Module module : modules.keySet()) {
AndroidModuleInfo info = AndroidModuleInfo.get(module);
if (info != null && info.getBuildSdkVersion() != null && info.getBuildSdkVersion().getFeatureLevel() < MIN_SDK_WITH_NULLABLE) {
modulesWithLowVersion.add(module);
}
GradleBuildModel buildModel = GradleBuildModel.get(module);
if (buildModel == null) {
LOG.warn("Unable to find Gradle build model for module " + module.getModuleFilePath());
continue;
}
boolean dependencyFound = false;
DependenciesModel dependenciesModel = buildModel.dependencies();
if (dependenciesModel != null) {
for (ArtifactDependencyModel dependency : dependenciesModel.artifacts(COMPILE)) {
String notation = dependency.compactNotation().value();
if (notation.startsWith(SdkConstants.APPCOMPAT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.SUPPORT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.ANNOTATIONS_LIB_ARTIFACT)) {
dependencyFound = true;
break;
}
}
}
if (!dependencyFound) {
modulesWithoutAnnotations.add(module);
}
}
if (!modulesWithLowVersion.isEmpty()) {
Messages.showErrorDialog(project, String.format("Infer Nullity Annotations requires the project sdk level be set to %1$d or greater.", MIN_SDK_WITH_NULLABLE), "Infer Nullity Annotations");
return false;
}
if (modulesWithoutAnnotations.isEmpty()) {
return true;
}
String moduleNames = StringUtil.join(modulesWithoutAnnotations, Module::getName, ", ");
int count = modulesWithoutAnnotations.size();
String message = String.format("The %1$s %2$s %3$sn't refer to the existing '%4$s' library with Android nullity annotations. \n\n" + "Would you like to add the %5$s now?", pluralize("module", count), moduleNames, count > 1 ? "do" : "does", SupportLibrary.SUPPORT_ANNOTATIONS.getArtifactId(), pluralize("dependency", count));
if (Messages.showOkCancelDialog(project, message, "Infer Nullity Annotations", Messages.getErrorIcon()) == Messages.OK) {
LocalHistoryAction action = LocalHistory.getInstance().startAction(ADD_DEPENDENCY);
try {
new WriteCommandAction(project, ADD_DEPENDENCY) {
@Override
protected void run(@NotNull Result result) throws Throwable {
RepositoryUrlManager manager = RepositoryUrlManager.get();
String annotationsLibraryCoordinate = manager.getLibraryStringCoordinate(SupportLibrary.SUPPORT_ANNOTATIONS, true);
for (Module module : modulesWithoutAnnotations) {
addDependency(module, annotationsLibraryCoordinate);
}
GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setGenerateSourcesOnSuccess(false);
GradleSyncInvoker.getInstance().requestProjectSync(project, request, new GradleSyncListener.Adapter() {
@Override
public void syncSucceeded(@NotNull Project project) {
restartAnalysis(project, scope);
}
});
}
}.execute();
} finally {
action.finish();
}
}
return false;
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel in project android by JetBrains.
the class AndroidGradleJavaProjectModelModifier method addModuleDependency.
@Nullable
@Override
public Promise<Void> addModuleDependency(@NotNull Module from, @NotNull Module to, @NotNull DependencyScope scope) {
Project project = from.getProject();
VirtualFile openedFile = FileEditorManagerEx.getInstanceEx(from.getProject()).getCurrentFile();
String gradlePath = getGradlePath(to);
GradleBuildModel buildModel = GradleBuildModel.get(from);
if (buildModel != null && gradlePath != null) {
DependenciesModel dependencies = buildModel.dependencies();
String configurationName = getConfigurationName(from, scope, openedFile);
dependencies.addModule(configurationName, gradlePath, null);
new WriteCommandAction(project, "Add Gradle Module Dependency") {
@Override
protected void run(@NotNull Result result) throws Throwable {
buildModel.applyChanges();
registerUndoAction(project);
}
}.execute();
return requestProjectSync(project);
}
if ((buildModel == null) ^ (gradlePath == null)) {
// If one of them is gradle module and one of them are not, reject since this is invalid dependency
return Promises.rejectedPromise();
}
return null;
}
Aggregations