use of com.android.tools.idea.gradle.dsl.model.GradleBuildModel in project android by JetBrains.
the class FileDependencyTest method testRemoveOneOfFileDependency.
public void testRemoveOneOfFileDependency() throws IOException {
String text = "dependencies {\n" + " compile files('lib1.jar', 'lib2.jar')\n" + "}";
writeToBuildFile(text);
GradleBuildModel buildModel = getGradleBuildModel();
DependenciesModel dependencies = buildModel.dependencies();
List<FileDependencyModel> fileDependencies = dependencies.files();
assertThat(fileDependencies).hasSize(2);
assertEquals("lib1.jar", fileDependencies.get(0).file());
FileDependencyModel file = fileDependencies.get(1);
assertEquals("lib2.jar", file.file());
dependencies.remove(file);
assertTrue(buildModel.isModified());
applyChangesAndReparse(buildModel);
fileDependencies = buildModel.dependencies().files();
assertThat(fileDependencies).hasSize(1);
assertEquals("lib1.jar", fileDependencies.get(0).file());
}
use of com.android.tools.idea.gradle.dsl.model.GradleBuildModel in project android by JetBrains.
the class FileDependencyTest method testAddFileDependency.
public void testAddFileDependency() throws IOException {
String text = "dependencies {\n" + "}";
writeToBuildFile(text);
GradleBuildModel buildModel = getGradleBuildModel();
DependenciesModel dependencies = buildModel.dependencies();
assertThat(dependencies.files()).isEmpty();
dependencies.addFile("compile", "lib1.jar");
assertTrue(buildModel.isModified());
applyChangesAndReparse(buildModel);
List<FileDependencyModel> fileDependencies = buildModel.dependencies().files();
assertThat(fileDependencies).hasSize(1);
assertEquals("lib1.jar", fileDependencies.get(0).file());
}
use of com.android.tools.idea.gradle.dsl.model.GradleBuildModel in project android by JetBrains.
the class FileDependencyTest method testSetFile.
public void testSetFile() throws IOException {
String text = "dependencies {\n" + " compile files('lib1.jar')\n" + "}";
writeToBuildFile(text);
GradleBuildModel buildModel = getGradleBuildModel();
List<FileDependencyModel> fileDependencies = buildModel.dependencies().files();
assertThat(fileDependencies).hasSize(1);
FileDependencyModel fileDependency = fileDependencies.get(0);
assertEquals("lib1.jar", fileDependency.file());
fileDependency.setFile("lib2.jar");
assertTrue(buildModel.isModified());
applyChangesAndReparse(buildModel);
fileDependencies = buildModel.dependencies().files();
assertThat(fileDependencies).hasSize(1);
assertEquals("lib2.jar", fileDependencies.get(0).file());
}
use of com.android.tools.idea.gradle.dsl.model.GradleBuildModel in project android by JetBrains.
the class AndroidInferNullityAnnotationAction method addDependency.
private static void addDependency(@NotNull Module module, @Nullable String libraryCoordinate) {
if (isNotEmpty(libraryCoordinate)) {
ModuleRootModificationUtil.updateModel(module, model -> {
GradleBuildModel buildModel = GradleBuildModel.get(module);
if (buildModel != null) {
buildModel.dependencies().addArtifact(COMPILE, libraryCoordinate);
buildModel.applyChanges();
}
});
}
}
use of com.android.tools.idea.gradle.dsl.model.GradleBuildModel in project android by JetBrains.
the class GradleOperations method removeDependencies.
@Override
public void removeDependencies(@NotNull Module module, @NotNull DeveloperServiceMetadata metadata) {
final GradleBuildModel buildModel = GradleBuildModel.get(module);
if (buildModel != null) {
boolean dependenciesChanged = false;
DependenciesModel dependenciesModel = buildModel.dependencies();
if (dependenciesModel != null) {
for (ArtifactDependencyModel dependency : dependenciesModel.artifacts()) {
ArtifactDependencySpec spec = ArtifactDependencySpec.create(dependency);
for (String dependencyValue : metadata.getDependencies()) {
if (spec.equals(ArtifactDependencySpec.create(dependencyValue))) {
dependenciesModel.remove(dependency);
dependenciesChanged = true;
break;
}
}
}
}
final Project project = module.getProject();
if (dependenciesChanged) {
new WriteCommandAction.Simple(project, "Uninstall " + metadata.getName()) {
@Override
public void run() {
buildModel.applyChanges();
}
}.execute();
}
GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(project, null);
}
}
Aggregations