use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel 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.ArtifactDependencyModel in project android by JetBrains.
the class DefaultRecipeExecutor method addClasspath.
@Override
public void addClasspath(@NotNull String mavenUrl) {
mavenUrl = mavenUrl.trim();
myReferences.addClasspath(mavenUrl);
ArtifactDependencySpec toBeAddedDependency = ArtifactDependencySpec.create(mavenUrl);
if (toBeAddedDependency == null) {
throw new RuntimeException(mavenUrl + " is not a valid classpath dependency");
}
Project project = myContext.getProject();
File rootBuildFile = getGradleBuildFilePath(getBaseDirPath(project));
if (project.isInitialized()) {
GradleBuildModel buildModel = getBuildModel(rootBuildFile, project);
DependenciesModel buildscriptDependencies = buildModel.buildscript().dependencies();
ArtifactDependencyModel targetDependencyModel = null;
for (ArtifactDependencyModel dependencyModel : buildscriptDependencies.artifacts(CLASSPATH_CONFIGURATION_NAME)) {
if (toBeAddedDependency.equalsIgnoreVersion(ArtifactDependencySpec.create(dependencyModel))) {
targetDependencyModel = dependencyModel;
}
}
if (targetDependencyModel == null) {
buildscriptDependencies.addArtifact(CLASSPATH_CONFIGURATION_NAME, toBeAddedDependency);
} else {
GradleVersion toBeAddedDependencyVersion = GradleVersion.parse(nullToEmpty(toBeAddedDependency.version));
GradleVersion existingDependencyVersion = GradleVersion.parse(nullToEmpty(targetDependencyModel.version().value()));
if (toBeAddedDependencyVersion.compareTo(existingDependencyVersion) > 0) {
targetDependencyModel.setVersion(nullToEmpty(toBeAddedDependency.version));
}
}
myIO.applyChanges(buildModel);
} else {
String destinationContents = rootBuildFile.exists() ? nullToEmpty(readTextFile(rootBuildFile)) : "";
String result = myIO.mergeGradleFiles(formatClasspath(mavenUrl), destinationContents, project, "");
try {
myIO.writeFile(this, result, rootBuildFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
myNeedsGradleSync = true;
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel in project android by JetBrains.
the class AndroidPluginVersionUpdaterIntegrationTest method setAndroidPluginVersion.
private void setAndroidPluginVersion(@NotNull String version) {
GradleBuildModel buildModel = getTopLevelBuildModel(getProject());
ArtifactDependencyModel androidPluginDependency = findAndroidPlugin(buildModel);
androidPluginDependency.setVersion(version);
runWriteCommandAction(getProject(), buildModel::applyChanges);
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel in project android by JetBrains.
the class PsAndroidModuleTest method testEditableDependencies.
public void testEditableDependencies() throws Throwable {
loadProject(PROJECT_WITH_APPAND_LIB);
Project resolvedProject = myFixture.getProject();
Module appModule = ModuleManager.getInstance(resolvedProject).findModuleByName("app");
assertNotNull(appModule);
// Make sure 'app' has an artifact dependency with version not including a '+'
GradleBuildModel buildModel = GradleBuildModel.get(appModule);
assertNotNull(buildModel);
for (ArtifactDependencyModel dependency : buildModel.dependencies().artifacts("compile")) {
if ("com.android.support".equals(dependency.group().value()) && "appcompat-v7".equals(dependency.name().value())) {
dependency.setVersion("23.1.1");
break;
}
}
runWriteCommandAction(resolvedProject, buildModel::applyChanges);
//noinspection ConstantConditions
importProject(resolvedProject.getName(), new File(resolvedProject.getBasePath()), null);
PsProject project = new PsProject(resolvedProject);
PsAndroidModule module = (PsAndroidModule) project.findModuleByName("app");
assertNotNull(module);
List<PsAndroidDependency> declaredDependencies = getDeclaredDependencies(module);
assertThat(declaredDependencies).hasSize(1);
// Verify that appcompat is considered a "editable" dependency, and it was matched properly
PsLibraryAndroidDependency appCompatV7 = (PsLibraryAndroidDependency) declaredDependencies.get(0);
assertTrue(appCompatV7.isDeclared());
PsArtifactDependencySpec resolvedSpec = appCompatV7.getResolvedSpec();
assertEquals("com.android.support", resolvedSpec.group);
assertEquals("appcompat-v7", resolvedSpec.name);
// Verify that the variants where appcompat is are properly registered.
Collection<String> variants = appCompatV7.getVariants();
assertThat(variants).containsExactly("paidDebug", "paidRelease", "basicDebug", "basicRelease");
for (String variant : variants) {
assertNotNull(module.findVariant(variant));
}
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel in project android by JetBrains.
the class GradleBuildModelFixture method requireDependency.
public void requireDependency(@NotNull String configurationName, @NotNull ArtifactDependencySpec expected) {
DependenciesModel dependenciesModel = myTarget.dependencies();
for (ArtifactDependencyModel dependency : dependenciesModel.artifacts()) {
ArtifactDependencySpec actual = ArtifactDependencySpec.create(dependency);
if (configurationName.equals(dependency.configurationName()) && expected.equals(actual)) {
return;
}
}
fail("Failed to find dependency '" + expected.compactNotation() + "'");
}
Aggregations