Search in sources :

Example 41 with GradleBuildModel

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());
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel)

Example 42 with GradleBuildModel

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());
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel)

Example 43 with GradleBuildModel

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());
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel)

Example 44 with GradleBuildModel

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();
            }
        });
    }
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel)

Example 45 with GradleBuildModel

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);
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) ArtifactDependencySpec(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec)

Aggregations

GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)285 ResValue (com.android.tools.idea.gradle.dsl.model.android.AbstractFlavorTypeModel.ResValue)20 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)17 SigningConfigPassword (com.android.tools.idea.gradle.dsl.model.android.SigningConfigModel.SigningConfigPassword)15 AndroidModel (com.android.tools.idea.gradle.dsl.model.android.AndroidModel)13 DependenciesModel (com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)13 File (java.io.File)13 Project (com.intellij.openapi.project.Project)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)12 BuildConfigField (com.android.tools.idea.gradle.dsl.model.android.BuildTypeModel.BuildConfigField)10 Module (com.intellij.openapi.module.Module)9 ProductFlavorModel (com.android.tools.idea.gradle.dsl.model.android.ProductFlavorModel)8 SourceDirectoryModel (com.android.tools.idea.gradle.dsl.model.android.sourceSets.SourceDirectoryModel)8 Result (com.intellij.openapi.application.Result)8 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)8 GradleNotNullValue (com.android.tools.idea.gradle.dsl.model.values.GradleNotNullValue)7 ExternalNativeBuildOptionsModel (com.android.tools.idea.gradle.dsl.model.android.productFlavors.ExternalNativeBuildOptionsModel)6 NotNull (org.jetbrains.annotations.NotNull)6 NdkOptionsModel (com.android.tools.idea.gradle.dsl.model.android.productFlavors.NdkOptionsModel)5 CMakeOptionsModel (com.android.tools.idea.gradle.dsl.model.android.productFlavors.externalNativeBuild.CMakeOptionsModel)5