Search in sources :

Example 1 with AndroidModel

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

the class ReferenceResolutionTest method testResolveRootProject.

public void testResolveRootProject() throws Exception {
    String settingsText = "include ':" + SUB_MODULE_NAME + "'";
    String mainModuleText = "android {\n" + "  compileSdkVersion = \"android-23\"\n" + "}";
    String subModuleText = "android {\n" + "  compileSdkVersion = rootProject.android.compileSdkVersion\n" + "}";
    writeToSettingsFile(settingsText);
    writeToBuildFile(mainModuleText);
    writeToSubModuleBuildFile(subModuleText);
    AndroidModel androidModel = getGradleBuildModel().android();
    assertNotNull(androidModel);
    assertEquals("compileSdkVersion", "android-23", androidModel.compileSdkVersion());
    AndroidModel subModuleAndroidModel = getSubModuleGradleBuildModel().android();
    assertNotNull(subModuleAndroidModel);
    assertEquals("compileSdkVersion", "android-23", subModuleAndroidModel.compileSdkVersion());
}
Also used : AndroidModel(com.android.tools.idea.gradle.dsl.model.android.AndroidModel)

Example 2 with AndroidModel

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

the class ReferenceResolutionTest method testResolveParent.

public void testResolveParent() throws Exception {
    String settingsText = "include ':" + SUB_MODULE_NAME + "'";
    String mainModuleText = "android {\n" + "  compileSdkVersion = \"android-23\"\n" + "}";
    String subModuleText = "android {\n" + "  compileSdkVersion = parent.android.compileSdkVersion\n" + "}";
    writeToSettingsFile(settingsText);
    writeToBuildFile(mainModuleText);
    writeToSubModuleBuildFile(subModuleText);
    AndroidModel androidModel = getGradleBuildModel().android();
    assertNotNull(androidModel);
    assertEquals("compileSdkVersion", "android-23", androidModel.compileSdkVersion());
    AndroidModel subModuleAndroidModel = getSubModuleGradleBuildModel().android();
    assertNotNull(subModuleAndroidModel);
    assertEquals("compileSdkVersion", "android-23", subModuleAndroidModel.compileSdkVersion());
}
Also used : AndroidModel(com.android.tools.idea.gradle.dsl.model.android.AndroidModel)

Example 3 with AndroidModel

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

the class GradleUtil method setBuildToolsVersion.

public static void setBuildToolsVersion(@NotNull Project project, @NotNull String version) {
    List<GradleBuildModel> modelsToUpdate = Lists.newArrayList();
    for (Module module : ModuleManager.getInstance(project).getModules()) {
        AndroidFacet facet = AndroidFacet.getInstance(module);
        if (facet != null) {
            GradleBuildModel buildModel = GradleBuildModel.get(module);
            if (buildModel != null) {
                AndroidModel android = buildModel.android();
                if (android != null && !version.equals(android.buildToolsVersion().value())) {
                    android.setBuildToolsVersion(version);
                    modelsToUpdate.add(buildModel);
                }
            }
        }
    }
    if (!modelsToUpdate.isEmpty()) {
        runWriteCommandAction(project, () -> {
            for (GradleBuildModel buildModel : modelsToUpdate) {
                buildModel.applyChanges();
            }
        });
    }
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) AndroidModel(com.android.tools.idea.gradle.dsl.model.android.AndroidModel) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 4 with AndroidModel

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

the class GradleBuildModel method android.

/**
   * Returns {@link AndroidModel} to read and update android block contents in the build.gradle file.
   *
   * <p>Returns {@code null} when experimental plugin is used as reading and updating android section is not supported for the
   * experimental dsl.</p>
   */
@Nullable
public AndroidModel android() {
    AndroidPluginInfo androidPluginInfo = AndroidPluginInfo.find(myGradleDslFile.getProject());
    if (androidPluginInfo != null && androidPluginInfo.isExperimental()) {
        // Reading or updating Android block contents is not supported when experimental plugin is used.
        return null;
    }
    AndroidDslElement androidDslElement = myGradleDslFile.getPropertyElement(ANDROID_BLOCK_NAME, AndroidDslElement.class);
    if (androidDslElement == null) {
        androidDslElement = new AndroidDslElement(myGradleDslFile);
        myGradleDslFile.setNewElement(ANDROID_BLOCK_NAME, androidDslElement);
    }
    return new AndroidModel(androidDslElement);
}
Also used : AndroidPluginInfo(com.android.tools.idea.gradle.plugin.AndroidPluginInfo) AndroidModel(com.android.tools.idea.gradle.dsl.model.android.AndroidModel) AndroidDslElement(com.android.tools.idea.gradle.dsl.parser.android.AndroidDslElement) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with AndroidModel

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

the class LinkExternalCppProjectDialog method doOKAction.

@Override
protected void doOKAction() {
    String projectPath = toSystemIndependentName(myProjectPathTextField.getText().trim());
    String relativePath = getPathRelativeToModuleDir(myModule, projectPath);
    assert relativePath != null;
    GradleBuildModel buildModel = GradleBuildModel.get(myModule);
    assert buildModel != null;
    AndroidModel android = buildModel.android();
    assert android != null;
    if (myBuildSystemCombo.getSelectedItem() == BuildSystem.CMAKE) {
        android.externalNativeBuild().cmake().setPath(new File(relativePath));
    } else {
        android.externalNativeBuild().ndkBuild().setPath(new File(relativePath));
    }
    Project project = myModule.getProject();
    new WriteCommandAction(project, "Link C++ Project with Gradle") {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            buildModel.applyChanges();
        }
    }.execute();
    GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(project, null);
    super.doOKAction();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) AndroidModel(com.android.tools.idea.gradle.dsl.model.android.AndroidModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) Result(com.intellij.openapi.application.Result)

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