Search in sources :

Example 6 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)

Example 7 with GradleFacet

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

the class JavaFacetModuleSetupStep method doSetUpModule.

@Override
protected void doSetUpModule(@NotNull Module module, @NotNull IdeModifiableModelsProvider ideModelsProvider, @NotNull JavaModuleModel javaModuleModel, @Nullable SyncAction.ModuleModels gradleModels, @Nullable ProgressIndicator indicator) {
    JavaFacet facet = setAndGetJavaGradleFacet(module, ideModelsProvider);
    GradleFacet gradleFacet = findFacet(module, ideModelsProvider, GradleFacet.getFacetTypeId());
    if (gradleFacet != null) {
        // This is an actual Gradle module, because it has the GradleFacet. Top-level modules in a multi-module project usually don't
        // have this facet.
        facet.setJavaModuleModel(javaModuleModel);
    }
    File buildFolderPath = javaModuleModel.getBuildFolderPath();
    JavaFacetConfiguration facetProperties = facet.getConfiguration();
    facetProperties.BUILD_FOLDER_PATH = buildFolderPath != null ? toSystemIndependentName(buildFolderPath.getPath()) : "";
    facetProperties.BUILDABLE = javaModuleModel.isBuildable();
}
Also used : JavaFacetConfiguration(com.android.tools.idea.gradle.project.facet.java.JavaFacetConfiguration) JavaFacet(com.android.tools.idea.gradle.project.facet.java.JavaFacet) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) File(java.io.File)

Example 8 with GradleFacet

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

the class GradleProjectSyncData method createFrom.

@Nullable
@VisibleForTesting
static GradleProjectSyncData createFrom(@NotNull Project project) throws IOException {
    GradleProjectSyncData data = new GradleProjectSyncData();
    File rootDirPath = getBaseDirPath(project);
    Module[] modules = ModuleManager.getInstance(project).getModules();
    for (Module module : modules) {
        GradleFacet gradleFacet = GradleFacet.getInstance(module);
        if (gradleFacet != null) {
            GradleModuleModel gradleModuleModel = gradleFacet.getGradleModuleModel();
            if (gradleModuleModel != null) {
                data.addFileChecksum(rootDirPath, gradleModuleModel.getBuildFile());
            } else {
                LOG.warn(String.format("Trying to create project data from a not initialized project '%1$s'. Abort.", project.getName()));
                return null;
            }
        }
        if (isGradleProjectModule(module)) {
            data.addFileChecksum(rootDirPath, getGradleBuildFile(module));
            data.addFileChecksum(rootDirPath, getGradleSettingsFile(rootDirPath));
            data.addFileChecksum(rootDirPath, new File(rootDirPath, FN_GRADLE_PROPERTIES));
            data.addFileChecksum(rootDirPath, new File(rootDirPath, FN_LOCAL_PROPERTIES));
            data.addFileChecksum(rootDirPath, getGradleUserSettingsFile());
        }
        NdkModuleModel ndkModuleModel = NdkModuleModel.get(module);
        if (ndkModuleModel != null) {
            for (File externalBuildFile : ndkModuleModel.getAndroidProject().getBuildFiles()) {
                data.addFileChecksum(rootDirPath, externalBuildFile);
            }
        }
    }
    GradleSyncState syncState = GradleSyncState.getInstance(project);
    data.myLastGradleSyncTimestamp = syncState.getSummary().getSyncTimestamp();
    return data;
}
Also used : GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) Projects.isGradleProjectModule(com.android.tools.idea.gradle.util.Projects.isGradleProjectModule) Module(com.intellij.openapi.module.Module) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) GradleSyncState(com.android.tools.idea.gradle.project.sync.GradleSyncState) VisibleForTesting(com.android.annotations.VisibleForTesting) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with GradleFacet

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

the class NewProjectImportGradleSyncListener method createTopLevelModule.

@VisibleForTesting
public static void createTopLevelModule(@NotNull Project project) {
    ModuleManager moduleManager = ModuleManager.getInstance(project);
    File projectRootDir = getBaseDirPath(project);
    VirtualFile contentRoot = findFileByIoFile(projectRootDir, true);
    if (contentRoot != null) {
        File moduleFile = new File(projectRootDir, projectRootDir.getName() + ".iml");
        Module module = moduleManager.newModule(moduleFile.getPath(), JAVA.getId());
        // This prevents the balloon "Unsupported Modules detected".
        ExternalSystemModulePropertyManager.getInstance(module).setExternalId(GRADLE_SYSTEM_ID);
        ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
        model.addContentEntry(contentRoot);
        if (IdeInfo.getInstance().isAndroidStudio()) {
            // If sync fails, make sure that the project has a JDK, otherwise Groovy indices won't work (a common scenario where
            // users will update build.gradle files to fix Gradle sync.)
            // See: https://code.google.com/p/android/issues/detail?id=194621
            Sdk jdk = IdeSdks.getInstance().getJdk();
            if (jdk != null) {
                model.setSdk(jdk);
            }
        }
        model.commit();
        FacetManager facetManager = FacetManager.getInstance(module);
        ModifiableFacetModel facetModel = facetManager.createModifiableModel();
        try {
            GradleFacet gradleFacet = GradleFacet.getInstance(module);
            if (gradleFacet == null) {
                // Add "gradle" facet, to avoid balloons about unsupported compilation of modules.
                gradleFacet = facetManager.createFacet(GradleFacet.getFacetType(), GradleFacet.getFacetName(), null);
                facetModel.addFacet(gradleFacet);
            }
            gradleFacet.getConfiguration().GRADLE_PROJECT_PATH = GRADLE_PATH_SEPARATOR;
            // Add "android" facet to avoid the balloon "Android Framework detected".
            AndroidFacet androidFacet = AndroidFacet.getInstance(module);
            if (androidFacet == null) {
                androidFacet = facetManager.createFacet(AndroidFacet.getFacetType(), AndroidFacet.NAME, null);
                facetModel.addFacet(androidFacet);
            }
            // This is what actually stops Studio from showing the balloon.
            androidFacet.getProperties().ALLOW_USER_CONFIGURATION = false;
        } finally {
            facetModel.commit();
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ModifiableFacetModel(com.intellij.facet.ModifiableFacetModel) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) Sdk(com.intellij.openapi.projectRoots.Sdk) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) File(java.io.File) FacetManager(com.intellij.facet.FacetManager) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) VisibleForTesting(com.android.annotations.VisibleForTesting)

Example 10 with GradleFacet

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

the class GradleBuildInvoker method findCleanTasksForModules.

@NotNull
public static List<String> findCleanTasksForModules(@NotNull Module[] modules) {
    List<String> tasks = new ArrayList<>();
    for (Module module : modules) {
        GradleFacet gradleFacet = GradleFacet.getInstance(module);
        if (gradleFacet == null) {
            continue;
        }
        String gradlePath = gradleFacet.getConfiguration().GRADLE_PROJECT_PATH;
        addTaskIfSpecified(tasks, gradlePath, CLEAN_TASK_NAME);
    }
    return tasks;
}
Also used : GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

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