Search in sources :

Example 11 with AndroidModel

use of com.android.tools.idea.gradle.dsl.model.android.AndroidModel in project android by JetBrains.

the class GradleValueTest method testListOfGradleValuesInAssignmentStatements.

public void testListOfGradleValuesInAssignmentStatements() throws Exception {
    String text = "android {\n" + "  defaultConfig {\n" + "    consumerProguardFiles = ['con-proguard-android.txt', 'con-proguard-rules.pro']\n" + "    proguardFiles = ['proguard-android.txt', 'proguard-rules.pro']\n" + "  }\n" + "}";
    writeToBuildFile(text);
    AndroidModel android = getGradleBuildModel().android();
    assertNotNull(android);
    ProductFlavorModel defaultConfig = android.defaultConfig();
    List<GradleNotNullValue<String>> consumerProguardFiles = defaultConfig.consumerProguardFiles();
    assertNotNull(consumerProguardFiles);
    assertThat(consumerProguardFiles).hasSize(2);
    verifyGradleValue(consumerProguardFiles.get(0), "android.defaultConfig.consumerProguardFiles.consumerProguardFiles", "'con-proguard-android.txt'");
    verifyGradleValue(consumerProguardFiles.get(1), "android.defaultConfig.consumerProguardFiles.consumerProguardFiles", "'con-proguard-rules.pro'");
    List<GradleNotNullValue<String>> proguardFiles = defaultConfig.proguardFiles();
    assertNotNull(proguardFiles);
    assertThat(proguardFiles).hasSize(2);
    verifyGradleValue(proguardFiles.get(0), "android.defaultConfig.proguardFiles.proguardFiles", "'proguard-android.txt'");
    verifyGradleValue(proguardFiles.get(1), "android.defaultConfig.proguardFiles.proguardFiles", "'proguard-rules.pro'");
}
Also used : AndroidModel(com.android.tools.idea.gradle.dsl.model.android.AndroidModel) ProductFlavorModel(com.android.tools.idea.gradle.dsl.model.android.ProductFlavorModel)

Example 12 with AndroidModel

use of com.android.tools.idea.gradle.dsl.model.android.AndroidModel in project android by JetBrains.

the class AndroidGradleJavaProjectModelModifier method changeLanguageLevel.

@Nullable
@Override
public Promise<Void> changeLanguageLevel(@NotNull Module module, @NotNull LanguageLevel level) {
    Project project = module.getProject();
    if (!isBuildWithGradle(module)) {
        return null;
    }
    GradleBuildModel buildModel = GradleBuildModel.get(module);
    if (buildModel == null) {
        return null;
    }
    if (getAndroidModel(module) != null) {
        AndroidModel android = buildModel.android();
        if (android == null) {
            return null;
        }
        CompileOptionsModel compileOptions = android.compileOptions();
        compileOptions.setSourceCompatibility(level);
        compileOptions.setTargetCompatibility(level);
    } else {
        JavaFacet javaFacet = JavaFacet.getInstance(module);
        if (javaFacet == null || javaFacet.getJavaModuleModel() == null) {
            return null;
        }
        JavaModel javaModel = buildModel.java();
        javaModel.setSourceCompatibility(level);
        javaModel.setTargetCompatibility(level);
    }
    new WriteCommandAction(project, "Change Gradle Language Level") {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            buildModel.applyChanges();
            registerUndoAction(project);
        }
    }.execute();
    return requestProjectSync(project);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) JavaFacet(com.android.tools.idea.gradle.project.facet.java.JavaFacet) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) Projects.getAndroidModel(com.android.tools.idea.gradle.util.Projects.getAndroidModel) AndroidModel(com.android.tools.idea.gradle.dsl.model.android.AndroidModel) JavaModel(com.android.tools.idea.gradle.dsl.model.java.JavaModel) CompileOptionsModel(com.android.tools.idea.gradle.dsl.model.android.CompileOptionsModel) Result(com.intellij.openapi.application.Result) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with AndroidModel

use of com.android.tools.idea.gradle.dsl.model.android.AndroidModel in project android by JetBrains.

the class FixBuildToolsVersionHyperlink method setBuildToolsVersion.

static void setBuildToolsVersion(@NotNull Project project, @NotNull VirtualFile buildFile, @NotNull String version, boolean requestSync) {
    // TODO check that the build file has the 'android' plugin applied.
    GradleBuildModel buildModel = parseBuildFile(buildFile, project);
    AndroidModel android = buildModel.android();
    if (android == null) {
        return;
    }
    if (version.equals(android.buildToolsVersion())) {
        return;
    }
    android.setBuildToolsVersion(version);
    runWriteCommandAction(project, buildModel::applyChanges);
    if (requestSync) {
        GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(project, null);
    }
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) AndroidModel(com.android.tools.idea.gradle.dsl.model.android.AndroidModel)

Example 14 with AndroidModel

use of com.android.tools.idea.gradle.dsl.model.android.AndroidModel in project android by JetBrains.

the class ExtModelTest method testResolveMultiLevelExtProperty.

public void testResolveMultiLevelExtProperty() throws IOException {
    String text = "ext.SDK_VERSION = 21\n" + "ext.COMPILE_SDK_VERSION = SDK_VERSION\n" + "android {\n" + "  compileSdkVersion COMPILE_SDK_VERSION\n" + "  defaultConfig {\n" + "    targetSdkVersion compileSdkVersion\n" + "  }\n" + "}";
    writeToBuildFile(text);
    GradleBuildModel buildModel = getGradleBuildModel();
    ExtModel extModel = buildModel.ext();
    assertEquals(21, extModel.getLiteralProperty("SDK_VERSION", Integer.class));
    assertEquals(21, extModel.getLiteralProperty("COMPILE_SDK_VERSION", Integer.class));
    AndroidModel androidModel = buildModel.android();
    assertNotNull(androidModel);
    assertEquals("compileSdkVersion", "21", androidModel.compileSdkVersion());
    ProductFlavorModel defaultConfig = androidModel.defaultConfig();
    assertEquals("targetSdkVersion", "21", defaultConfig.targetSdkVersion());
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) AndroidModel(com.android.tools.idea.gradle.dsl.model.android.AndroidModel) ProductFlavorModel(com.android.tools.idea.gradle.dsl.model.android.ProductFlavorModel)

Example 15 with AndroidModel

use of com.android.tools.idea.gradle.dsl.model.android.AndroidModel in project android by JetBrains.

the class ExtModelTest method testResolveQualifiedVariableInStringLiteral.

public void testResolveQualifiedVariableInStringLiteral() throws IOException {
    String text = "android {\n" + "  compileSdkVersion 23\n" + "  defaultConfig {\n" + "    targetSdkVersion \"$android.compileSdkVersion\"\n" + "  }\n" + "}";
    writeToBuildFile(text);
    GradleBuildModel buildModel = getGradleBuildModel();
    AndroidModel androidModel = buildModel.android();
    assertNotNull(androidModel);
    assertEquals("compileSdkVersion", "23", androidModel.compileSdkVersion());
    ProductFlavorModel defaultConfig = androidModel.defaultConfig();
    assertEquals("targetSdkVersion", "23", defaultConfig.targetSdkVersion());
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) AndroidModel(com.android.tools.idea.gradle.dsl.model.android.AndroidModel) ProductFlavorModel(com.android.tools.idea.gradle.dsl.model.android.ProductFlavorModel)

Aggregations

AndroidModel (com.android.tools.idea.gradle.dsl.model.android.AndroidModel)28 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)13 ProductFlavorModel (com.android.tools.idea.gradle.dsl.model.android.ProductFlavorModel)13 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 Project (com.intellij.openapi.project.Project)2 Nullable (org.jetbrains.annotations.Nullable)2 AbstractFlavorTypeModel (com.android.tools.idea.gradle.dsl.model.android.AbstractFlavorTypeModel)1 BuildTypeModel (com.android.tools.idea.gradle.dsl.model.android.BuildTypeModel)1 CompileOptionsModel (com.android.tools.idea.gradle.dsl.model.android.CompileOptionsModel)1 JavaModel (com.android.tools.idea.gradle.dsl.model.java.JavaModel)1 AndroidDslElement (com.android.tools.idea.gradle.dsl.parser.android.AndroidDslElement)1 GradleDslExpressionList (com.android.tools.idea.gradle.dsl.parser.elements.GradleDslExpressionList)1 AndroidPluginInfo (com.android.tools.idea.gradle.plugin.AndroidPluginInfo)1 JavaFacet (com.android.tools.idea.gradle.project.facet.java.JavaFacet)1 Projects.getAndroidModel (com.android.tools.idea.gradle.util.Projects.getAndroidModel)1 Module (com.intellij.openapi.module.Module)1 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 File (java.io.File)1