Search in sources :

Example 21 with AndroidModuleModel

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

the class GradleBuildInvoker method findAndAddGradleBuildTasks.

private static void findAndAddGradleBuildTasks(@NotNull Module module, @NotNull BuildMode buildMode, @NotNull List<String> tasks, @NotNull TestCompileType testCompileType) {
    GradleFacet gradleFacet = GradleFacet.getInstance(module);
    if (gradleFacet == null) {
        return;
    }
    String gradlePath = gradleFacet.getConfiguration().GRADLE_PROJECT_PATH;
    if (isEmpty(gradlePath)) {
        // Gradle project path is never, ever null. If the path is empty, it shows as ":". We had reports of this happening. It is likely that
        // users manually added the Android-Gradle facet to a project. After all it is likely not to be a Gradle module. Better quit and not
        // build the module.
        String msg = String.format("Module '%1$s' does not have a Gradle path. It is likely that this module was manually added by the user.", module.getName());
        getLogger().info(msg);
        return;
    }
    AndroidFacet androidFacet = AndroidFacet.getInstance(module);
    if (androidFacet != null) {
        JpsAndroidModuleProperties properties = androidFacet.getProperties();
        AndroidModuleModel androidModel = AndroidModuleModel.get(module);
        switch(buildMode) {
            // Intentional fall-through.
            case CLEAN:
            case SOURCE_GEN:
                addAfterSyncTasks(tasks, gradlePath, properties);
                addAfterSyncTasksForTestArtifacts(tasks, gradlePath, testCompileType, androidModel);
                break;
            case ASSEMBLE:
                tasks.add(createBuildTask(gradlePath, properties.ASSEMBLE_TASK_NAME));
                // Add assemble tasks for tests.
                if (testCompileType != TestCompileType.NONE) {
                    for (BaseArtifact artifact : getArtifactsForTestCompileType(testCompileType, androidModel)) {
                        addTaskIfSpecified(tasks, gradlePath, artifact.getAssembleTaskName());
                    }
                }
                break;
            default:
                addAfterSyncTasks(tasks, gradlePath, properties);
                addAfterSyncTasksForTestArtifacts(tasks, gradlePath, testCompileType, androidModel);
                // no *.class files and would be just a waste of time.
                if (testCompileType != TestCompileType.UNIT_TESTS) {
                    addTaskIfSpecified(tasks, gradlePath, properties.COMPILE_JAVA_TASK_NAME);
                }
                // Add compile tasks for tests.
                for (BaseArtifact artifact : getArtifactsForTestCompileType(testCompileType, androidModel)) {
                    addTaskIfSpecified(tasks, gradlePath, artifact.getCompileTaskName());
                }
                break;
        }
    } else {
        JavaFacet javaFacet = JavaFacet.getInstance(module);
        if (javaFacet != null && javaFacet.getConfiguration().BUILDABLE) {
            String gradleTaskName = javaFacet.getGradleTaskName(buildMode);
            if (gradleTaskName != null) {
                tasks.add(createBuildTask(gradlePath, gradleTaskName));
            }
            if (testCompileType == TestCompileType.UNIT_TESTS) {
                tasks.add(createBuildTask(gradlePath, JavaFacet.TEST_CLASSES_TASK_NAME));
            }
        }
    }
}
Also used : JavaFacet(com.android.tools.idea.gradle.project.facet.java.JavaFacet) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) JpsAndroidModuleProperties(org.jetbrains.jps.android.model.impl.JpsAndroidModuleProperties) BaseArtifact(com.android.builder.model.BaseArtifact) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 22 with AndroidModuleModel

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

the class AndroidGeneratedSourcesFilter method isGeneratedSource.

// See: https://code.google.com/p/android/issues/detail?id=121156
// Alternative could be marking the entire build folder as generated source folder, but this have many side affects, e.g. a broken source
// file (e.g. have some syntax error / warning) under build folder that is not actually used in the project would be marked as red too and
// the user might be asked to fix it.
@Override
public boolean isGeneratedSource(@NotNull VirtualFile file, @NotNull Project project) {
    GradleProjectInfo projectInfo = GradleProjectInfo.getInstance(project);
    AndroidModuleModel androidModel = projectInfo.findAndroidModelInModule(file);
    if (androidModel != null) {
        return isAncestor(androidModel.getAndroidProject().getBuildFolder(), virtualToIoFile(file), false);
    }
    // Gradle projects also sometimes create a "build" folder at the top level (where there is no AndroidFacet module). Unfortunately, this
    // folder is not available in the Gradle project model so we have to look for it by hardcoded name.
    VirtualFile rootFolder = project.getBaseDir();
    if (rootFolder == null) {
        return false;
    }
    VirtualFile buildFolder = rootFolder.findChild(BUILD_DIR_DEFAULT_NAME);
    return buildFolder != null && projectInfo.isBuildWithGradle() && isAncestor(buildFolder, file, false);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleProjectInfo(com.android.tools.idea.gradle.project.GradleProjectInfo) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel)

Example 23 with AndroidModuleModel

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

the class LintIdeProject method createModuleProject.

/** Creates a new module project */
@Nullable
private static LintModuleProject createModuleProject(@NonNull LintClient client, @NonNull Module module) {
    AndroidFacet facet = AndroidFacet.getInstance(module);
    File dir = getLintProjectDirectory(module, facet);
    if (dir == null)
        return null;
    LintModuleProject project = null;
    if (facet == null) {
        project = new LintModuleProject(client, dir, dir, module);
        AndroidFacet f = findAndroidFacetInProject(module.getProject());
        if (f != null) {
            project.gradleProject = f.requiresAndroidModel();
        }
    } else if (facet.requiresAndroidModel()) {
        AndroidModel androidModel = facet.getAndroidModel();
        if (androidModel instanceof AndroidModuleModel) {
            project = new LintGradleProject(client, dir, dir, facet, (AndroidModuleModel) androidModel);
        } else {
            project = new LintAndroidModelProject(client, dir, dir, facet, androidModel);
        }
    } else {
        project = new LintAndroidProject(client, dir, dir, facet);
    }
    if (project != null) {
        client.registerProject(dir, project);
    }
    return project;
}
Also used : AndroidModel(com.android.tools.idea.model.AndroidModel) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with AndroidModuleModel

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

the class LintIdeProject method addProjects.

/**
   * Recursively add lint projects for the given module, and any other module or library it depends on, and also
   * populate the reverse maps so we can quickly map from a lint project to a corresponding module/library (used
   * by the lint client
   */
private static void addProjects(@NonNull LintClient client, @NonNull Module module, @Nullable List<VirtualFile> files, @NonNull Map<Module, Project> moduleMap, @NonNull Map<AndroidLibrary, Project> libraryMap, @NonNull Map<Project, Module> projectMap, @NonNull List<Project> projects) {
    if (moduleMap.containsKey(module)) {
        return;
    }
    LintModuleProject project = createModuleProject(client, module);
    if (project == null) {
        // It's possible for the module to *depend* on Android code, e.g. in a Gradle
        // project there will be a top-level non-Android module
        List<AndroidFacet> dependentFacets = AndroidUtils.getAllAndroidDependencies(module, false);
        for (AndroidFacet dependentFacet : dependentFacets) {
            addProjects(client, dependentFacet.getModule(), files, moduleMap, libraryMap, projectMap, projects);
        }
        return;
    }
    projects.add(project);
    moduleMap.put(module, project);
    projectMap.put(project, module);
    if (processFileFilter(module, files, project)) {
        // No need to process dependencies when doing single file analysis
        return;
    }
    List<Project> dependencies = Lists.newArrayList();
    // No, this shouldn't use getAllAndroidDependencies; we may have non-Android dependencies that this won't include
    // (e.g. Java-only modules)
    List<AndroidFacet> dependentFacets = AndroidUtils.getAllAndroidDependencies(module, true);
    for (AndroidFacet dependentFacet : dependentFacets) {
        Project p = moduleMap.get(dependentFacet.getModule());
        if (p != null) {
            dependencies.add(p);
        } else {
            addProjects(client, dependentFacet.getModule(), files, moduleMap, libraryMap, projectMap, dependencies);
        }
    }
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet != null) {
        AndroidModuleModel androidModuleModel = AndroidModuleModel.get(facet);
        if (androidModuleModel != null) {
            addGradleLibraryProjects(client, files, libraryMap, projects, facet, androidModuleModel, project, projectMap, dependencies);
        }
    }
    project.setDirectLibraries(dependencies);
}
Also used : Project(com.android.tools.lint.detector.api.Project) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 25 with AndroidModuleModel

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

the class AndroidRunState method execute.

@Nullable
@Override
public ExecutionResult execute(Executor executor, @NotNull ProgramRunner runner) throws ExecutionException {
    ProcessHandler processHandler;
    ConsoleView console;
    String applicationId;
    try {
        applicationId = myApplicationIdProvider.getPackageName();
    } catch (ApkProvisionException e) {
        throw new ExecutionException("Unable to obtain application id", e);
    }
    // TODO: this class is independent of gradle, except for this hack
    AndroidModuleModel model = AndroidModuleModel.get(myModule);
    if (InstantRunSettings.isInstantRunEnabled() && InstantRunGradleUtils.getIrSupportStatus(model, null) == InstantRunGradleSupport.SUPPORTED) {
        assert model != null;
        InstantRunBuildInfo info = InstantRunGradleUtils.getBuildInfo(model);
        if (info != null && !info.isCompatibleFormat()) {
            throw new ExecutionException("This version of Android Studio is incompatible with the Gradle Plugin used. " + "Try disabling Instant Run (or updating either the IDE or the Gradle plugin to " + "the latest version)");
        }
    }
    LaunchTasksProvider launchTasksProvider = myLaunchTasksProviderFactory.get();
    if (launchTasksProvider.createsNewProcess()) {
        // and the new one).
        if (myPreviousSessionProcessHandler != null) {
            myPreviousSessionProcessHandler.detachProcess();
        }
        processHandler = new AndroidProcessHandler(applicationId, launchTasksProvider.monitorRemoteProcess());
        console = attachConsole(processHandler, executor);
    } else {
        assert myPreviousSessionProcessHandler != null : "No process handler from previous session, yet current tasks don't create one";
        processHandler = myPreviousSessionProcessHandler;
        console = null;
    }
    LaunchInfo launchInfo = new LaunchInfo(executor, runner, myEnv, myConsoleProvider);
    LaunchTaskRunner task = new LaunchTaskRunner(myModule.getProject(), myLaunchConfigName, launchInfo, processHandler, myDeviceFutures, launchTasksProvider);
    ProgressManager.getInstance().run(task);
    return console == null ? null : new DefaultExecutionResult(console, processHandler);
}
Also used : InstantRunBuildInfo(com.android.tools.fd.client.InstantRunBuildInfo) DefaultExecutionResult(com.intellij.execution.DefaultExecutionResult) ConsoleView(com.intellij.execution.ui.ConsoleView) LaunchTasksProvider(com.android.tools.idea.run.tasks.LaunchTasksProvider) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) ProcessHandler(com.intellij.execution.process.ProcessHandler) ExecutionException(com.intellij.execution.ExecutionException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)117 Module (com.intellij.openapi.module.Module)54 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)26 File (java.io.File)24 NotNull (org.jetbrains.annotations.NotNull)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 Nullable (org.jetbrains.annotations.Nullable)18 AndroidProject (com.android.builder.model.AndroidProject)12 GradleVersion (com.android.ide.common.repository.GradleVersion)11 NdkModuleModel (com.android.tools.idea.gradle.project.model.NdkModuleModel)10 Project (com.intellij.openapi.project.Project)9 Variant (com.android.builder.model.Variant)8 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)7 AndroidLibrary (com.android.builder.model.AndroidLibrary)5 PsiFile (com.intellij.psi.PsiFile)5 AndroidArtifact (com.android.builder.model.AndroidArtifact)4 AndroidArtifactOutput (com.android.builder.model.AndroidArtifactOutput)4 NativeAndroidProject (com.android.builder.model.NativeAndroidProject)4 ModuleNodeBuilder (com.android.tools.idea.gradle.AndroidModelView.ModuleNodeBuilder)4 AndroidVersion (com.android.sdklib.AndroidVersion)3