Search in sources :

Example 1 with GradleFacet

use of com.android.tools.idea.gradle.project.facet.gradle.GradleFacet in project android by JetBrains.

the class DataNodeCaches method isCacheMissingModels.

public boolean isCacheMissingModels(@NotNull DataNode<ProjectData> cache) {
    Collection<DataNode<ModuleData>> moduleDataNodes = findAll(cache, MODULE);
    if (!moduleDataNodes.isEmpty()) {
        Map<String, DataNode<ModuleData>> moduleDataNodesByName = indexByModuleName(moduleDataNodes);
        ModuleManager moduleManager = ModuleManager.getInstance(myProject);
        for (Module module : moduleManager.getModules()) {
            DataNode<ModuleData> moduleDataNode = moduleDataNodesByName.get(module.getName());
            if (moduleDataNode == null) {
                // When a Gradle facet is present, there should be a cache node for the module.
                GradleFacet gradleFacet = GradleFacet.getInstance(module);
                if (gradleFacet != null) {
                    return true;
                }
            } else if (isCacheMissingModels(moduleDataNode, module)) {
                return true;
            }
        }
        return false;
    }
    return true;
}
Also used : DataNode(com.intellij.openapi.externalSystem.model.DataNode) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module)

Example 2 with GradleFacet

use of com.android.tools.idea.gradle.project.facet.gradle.GradleFacet in project android by JetBrains.

the class DataNodeCaches method isCacheMissingModels.

private static boolean isCacheMissingModels(@NotNull DataNode<ModuleData> cache, @NotNull Module module) {
    GradleFacet gradleFacet = GradleFacet.getInstance(module);
    if (gradleFacet != null) {
        DataNode<GradleModuleModel> gradleDataNode = find(cache, GRADLE_MODULE_MODEL);
        if (gradleDataNode == null) {
            return true;
        }
        AndroidFacet androidFacet = AndroidFacet.getInstance(module);
        if (androidFacet != null) {
            DataNode<AndroidModuleModel> androidDataNode = find(cache, ANDROID_MODEL);
            if (androidDataNode == null || !isValidProxyObject(androidDataNode.getData().getAndroidProject())) {
                return true;
            }
        } else {
            JavaFacet javaFacet = JavaFacet.getInstance(module);
            if (javaFacet != null) {
                DataNode<JavaModuleModel> javaProjectDataNode = find(cache, JAVA_MODULE_MODEL);
                if (javaProjectDataNode == null) {
                    return true;
                }
            }
        }
    }
    NdkFacet ndkFacet = NdkFacet.getInstance(module);
    if (ndkFacet != null) {
        DataNode<NdkModuleModel> ndkModuleModelDataNode = find(cache, NDK_MODEL);
        if (ndkModuleModelDataNode == null || !isValidProxyObject(ndkModuleModelDataNode.getData().getAndroidProject())) {
            return true;
        }
    }
    return false;
}
Also used : GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) JavaFacet(com.android.tools.idea.gradle.project.facet.java.JavaFacet) JavaModuleModel(com.android.tools.idea.gradle.project.model.JavaModuleModel) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) NdkFacet(com.android.tools.idea.gradle.project.facet.ndk.NdkFacet) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 3 with GradleFacet

use of com.android.tools.idea.gradle.project.facet.gradle.GradleFacet in project android by JetBrains.

the class GradleUtil method getGradleBuildFile.

/**
   * Returns the build.gradle file in the given module. This method first checks if the Gradle model has the path of the build.gradle
   * file for the given module. If it doesn't find it, it tries to find a build.gradle inside the module's root directory.
   *
   * @param module the given module.
   * @return the build.gradle file in the given module, or {@code null} if it cannot be found.
   */
@Nullable
public static VirtualFile getGradleBuildFile(@NotNull Module module) {
    GradleFacet gradleFacet = GradleFacet.getInstance(module);
    if (gradleFacet != null && gradleFacet.getGradleModuleModel() != null) {
        return gradleFacet.getGradleModuleModel().getBuildFile();
    }
    // At the time we're called, module.getModuleFile() may be null, but getModuleFilePath returns the path where it will be created.
    File moduleFilePath = new File(module.getModuleFilePath());
    File parentFile = moduleFilePath.getParentFile();
    return parentFile != null ? getGradleBuildFile(parentFile) : null;
}
Also used : GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GradleFacet

use of com.android.tools.idea.gradle.project.facet.gradle.GradleFacet 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 5 with GradleFacet

use of com.android.tools.idea.gradle.project.facet.gradle.GradleFacet in project android by JetBrains.

the class AndroidGradleModuleUtils method getContainingModule.

/**
   * Given a file and a project, return the Module corresponding to the containing Gradle project for the file.  If the file is not
   * contained by any project then return null
   */
@Nullable
public static Module getContainingModule(File file, Project project) {
    VirtualFile vFile = VfsUtil.findFileByIoFile(file, false);
    if (vFile == null) {
        return null;
    }
    Module bestMatch = null;
    int bestMatchValue = Integer.MAX_VALUE;
    for (Module module : ModuleManager.getInstance(project).getModules()) {
        GradleFacet facet = GradleFacet.getInstance(module);
        if (facet != null) {
            GradleModuleModel gradleModuleModel = facet.getGradleModuleModel();
            assert gradleModuleModel != null;
            VirtualFile buildFile = gradleModuleModel.getBuildFile();
            if (buildFile != null) {
                VirtualFile root = buildFile.getParent();
                if (VfsUtilCore.isAncestor(root, vFile, true)) {
                    String relativePath = VfsUtilCore.getRelativePath(vFile, root, '/');
                    if (relativePath != null) {
                        int value = Iterables.size(Splitter.on('/').split(relativePath));
                        if (value < bestMatchValue) {
                            bestMatch = module;
                            bestMatchValue = value;
                        }
                    }
                }
            }
        }
    }
    return bestMatch;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GradleFacet (com.android.tools.idea.gradle.project.facet.gradle.GradleFacet)18 Module (com.intellij.openapi.module.Module)9 GradleModuleModel (com.android.tools.idea.gradle.project.model.GradleModuleModel)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 ModifiableFacetModel (com.intellij.facet.ModifiableFacetModel)4 File (java.io.File)4 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)4 Nullable (org.jetbrains.annotations.Nullable)4 JavaFacet (com.android.tools.idea.gradle.project.facet.java.JavaFacet)3 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)3 FacetManager (com.intellij.facet.FacetManager)3 ModuleManager (com.intellij.openapi.module.ModuleManager)3 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)3 VisibleForTesting (com.android.annotations.VisibleForTesting)2 GradleVersion (com.android.ide.common.repository.GradleVersion)2 NdkModuleModel (com.android.tools.idea.gradle.project.model.NdkModuleModel)2 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)2 BaseArtifact (com.android.builder.model.BaseArtifact)1 GradleFacetType (com.android.tools.idea.gradle.project.facet.gradle.GradleFacetType)1 JavaFacetConfiguration (com.android.tools.idea.gradle.project.facet.java.JavaFacetConfiguration)1