Search in sources :

Example 66 with AndroidModuleModel

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

the class FmHasDependencyMethod method exec.

@Override
public TemplateModel exec(List args) throws TemplateModelException {
    if (args.size() < 1 || args.size() > 2) {
        throw new TemplateModelException("Wrong arguments");
    }
    String artifact = ((TemplateScalarModel) args.get(0)).getAsString();
    if (artifact.isEmpty()) {
        return TemplateBooleanModel.FALSE;
    }
    // Determine the configuration to check, based on the second argument passed to the function. Defaults to "compile".
    String configuration = SdkConstants.GRADLE_COMPILE_CONFIGURATION;
    if (args.size() > 1) {
        configuration = ((TemplateScalarModel) args.get(1)).getAsString();
    }
    if (myParamMap.containsKey(TemplateMetadata.ATTR_DEPENDENCIES_MULTIMAP)) {
        Object untyped = myParamMap.get(TemplateMetadata.ATTR_DEPENDENCIES_MULTIMAP);
        if (untyped instanceof SetMultimap) {
            @SuppressWarnings("unchecked") SetMultimap<String, String> dependencies = (SetMultimap<String, String>) untyped;
            for (String dependency : dependencies.get(configuration)) {
                if (dependency.contains(artifact)) {
                    return TemplateBooleanModel.TRUE;
                }
            }
        }
    }
    // Find the corresponding module, if any
    String modulePath = (String) myParamMap.get(TemplateMetadata.ATTR_PROJECT_OUT);
    if (modulePath != null) {
        Module module = FmUtil.findModule(modulePath);
        if (module != null) {
            AndroidFacet facet = AndroidFacet.getInstance(module);
            if (facet != null) {
                // TODO: b/23032990
                AndroidModuleModel androidModel = AndroidModuleModel.get(facet);
                if (androidModel != null) {
                    boolean dependsOn;
                    switch(configuration) {
                        case SdkConstants.GRADLE_COMPILE_CONFIGURATION:
                            dependsOn = GradleUtil.dependsOn(androidModel, artifact);
                            break;
                        case SdkConstants.GRADLE_ANDROID_TEST_COMPILE_CONFIGURATION:
                            dependsOn = GradleUtil.dependsOnAndroidTest(androidModel, artifact);
                            break;
                        default:
                            throw new TemplateModelException("Unknown dependency configuration " + configuration);
                    }
                    return dependsOn ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
                }
            }
        }
    }
    // created project will return true since it will be by virtue of appcompat also being installed.)
    if (artifact.contains(SdkConstants.APPCOMPAT_LIB_ARTIFACT) || artifact.contains(SdkConstants.SUPPORT_LIB_ARTIFACT)) {
        // No dependencies: Base it off of the minApi and buildApi versions:
        // If building with Lollipop, and targeting anything earlier than Lollipop, use appcompat.
        // (Also use it if minApi is less than ICS.)
        Object buildApiObject = myParamMap.get(TemplateMetadata.ATTR_BUILD_API);
        Object minApiObject = myParamMap.get(TemplateMetadata.ATTR_MIN_API_LEVEL);
        if (buildApiObject instanceof Integer && minApiObject instanceof Integer) {
            int buildApi = (Integer) buildApiObject;
            int minApi = (Integer) minApiObject;
            return minApi >= 8 && ((buildApi >= 21 && minApi < 21) || minApi < 14) ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
        }
    }
    return TemplateBooleanModel.FALSE;
}
Also used : AndroidFacet(org.jetbrains.android.facet.AndroidFacet) SetMultimap(com.google.common.collect.SetMultimap) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Module(com.intellij.openapi.module.Module)

Example 67 with AndroidModuleModel

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

the class AndroidPluginGeneration method find.

@Nullable
public static AndroidPluginGeneration find(@NotNull Module module) {
    AndroidModuleModel gradleModel = AndroidModuleModel.get(module);
    if (gradleModel != null) {
        try {
            // only true for experimental plugin 0.6.0-betaX (or whenever the getPluginGeneration() was added) or later.
            return gradleModel.getAndroidProject().getPluginGeneration() == GENERATION_COMPONENT ? COMPONENT : ORIGINAL;
        } catch (UnsupportedMethodException t) {
        // happens for 2.0.0-alphaX or earlier stable version plugins and 0.6.0-alphax or earlier experimental plugin versions.
        }
    }
    // Now look at the applied plugins in the build.gradle file.
    GradleBuildModel buildModel = GradleBuildModel.get(module);
    if (buildModel != null) {
        List<String> appliedPlugins = getValues(buildModel.appliedPlugins());
        for (AndroidPluginGeneration generation : ourValues) {
            if (appliedPlugins.contains(generation.getApplicationPluginId()) || appliedPlugins.contains(generation.getLibraryPluginId())) {
                return generation;
            }
        }
    }
    return null;
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) UnsupportedMethodException(org.gradle.tooling.model.UnsupportedMethodException) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Nullable(com.android.annotations.Nullable)

Example 68 with AndroidModuleModel

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

the class ManifestPanel method describePosition.

private void describePosition(@NotNull HtmlBuilder sb, @NotNull AndroidFacet facet, @NotNull SourceFilePosition sourceFilePosition) {
    SourceFile sourceFile = sourceFilePosition.getFile();
    SourcePosition sourcePosition = sourceFilePosition.getPosition();
    File file = sourceFile.getSourceFile();
    if (file == GRADLE_MODEL_MARKER_FILE) {
        VirtualFile gradleBuildFile = GradleUtil.getGradleBuildFile(facet.getModule());
        if (gradleBuildFile != null) {
            file = VfsUtilCore.virtualToIoFile(gradleBuildFile);
            sb.addHtml("<a href=\"");
            sb.add(file.toURI().toString());
            sb.addHtml("\">");
            sb.add(file.getName());
            sb.addHtml("</a>");
            sb.add(" injection");
        } else {
            sb.add("build.gradle injection (source location unknown)");
        }
        return;
    }
    AndroidLibrary library;
    if (file != null) {
        String source = null;
        Module libraryModule = null;
        Module[] modules = ModuleManager.getInstance(facet.getModule().getProject()).getModules();
        VirtualFile vFile = LocalFileSystem.getInstance().findFileByIoFile(file);
        if (vFile != null) {
            Module module = ModuleUtilCore.findModuleForFile(vFile, facet.getModule().getProject());
            if (module != null) {
                if (modules.length >= 2) {
                    source = module.getName();
                }
                // AAR Library?
                if (file.getPath().contains(EXPLODED_AAR)) {
                    AndroidModuleModel androidModel = AndroidModuleModel.get(module);
                    if (androidModel != null) {
                        library = GradleUtil.findLibrary(file.getParentFile(), androidModel.getSelectedVariant(), androidModel.getModelVersion());
                        if (library != null) {
                            if (library.getProject() != null) {
                                libraryModule = GradleUtil.findModuleByGradlePath(facet.getModule().getProject(), library.getProject());
                                if (libraryModule != null) {
                                    module = libraryModule;
                                    source = module.getName();
                                } else {
                                    source = library.getProject();
                                    source = StringUtil.trimStart(source, ":");
                                }
                            } else {
                                MavenCoordinates coordinates = library.getResolvedCoordinates();
                                source = /*coordinates.getGroupId() + ":" +*/
                                coordinates.getArtifactId() + ":" + coordinates.getVersion();
                            }
                        }
                    }
                }
            }
            IdeaSourceProvider provider = ManifestUtils.findManifestSourceProvider(facet, vFile);
            if (provider != null) /*&& !provider.equals(facet.getMainIdeaSourceProvider())*/
            {
                String providerName = provider.getName();
                if (source == null) {
                    source = providerName;
                } else {
                    // "the app main manifest" - "app" is the module name, "main" is the source provider name
                    source = source + " " + providerName;
                }
            }
        }
        if (source == null) {
            source = file.getName();
            if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
                source += ":" + String.valueOf(sourcePosition);
            }
        }
        sb.addHtml("<a href=\"");
        boolean redirected = false;
        if (libraryModule != null) {
            AndroidFacet libraryFacet = AndroidFacet.getInstance(libraryModule);
            if (libraryFacet != null) {
                File manifestFile = libraryFacet.getMainSourceProvider().getManifestFile();
                if (manifestFile.exists()) {
                    sb.add(manifestFile.toURI().toString());
                    redirected = true;
                    // Line numbers probably aren't right
                    sourcePosition = SourcePosition.UNKNOWN;
                // TODO: Set URL which points to the element/attribute path
                }
            }
        }
        if (!redirected) {
            sb.add(file.toURI().toString());
            if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
                sb.add(":");
                sb.add(String.valueOf(sourcePosition.getStartLine()));
                sb.add(":");
                sb.add(String.valueOf(sourcePosition.getStartColumn()));
            }
        }
        sb.addHtml("\">");
        sb.add(source);
        sb.addHtml("</a>");
        sb.add(" manifest");
        if (FileUtil.filesEqual(file, VfsUtilCore.virtualToIoFile(myFile))) {
            sb.add(" (this file)");
        }
        if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
            sb.add(", line ");
            sb.add(Integer.toString(sourcePosition.getStartLine()));
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) MavenCoordinates(com.android.builder.model.MavenCoordinates) AndroidLibrary(com.android.builder.model.AndroidLibrary) SourcePosition(com.android.ide.common.blame.SourcePosition) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) SourceFile(com.android.ide.common.blame.SourceFile) Module(com.intellij.openapi.module.Module) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFile(com.android.ide.common.blame.SourceFile) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) File(java.io.File)

Example 69 with AndroidModuleModel

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

the class PostProjectBuildTasksExecutor method excludeOutputFolders.

private static void excludeOutputFolders(@NotNull AndroidFacet facet) {
    AndroidModuleModel androidModel = AndroidModuleModel.get(facet);
    if (androidModel == null) {
        return;
    }
    File buildFolderPath = androidModel.getAndroidProject().getBuildFolder();
    if (!buildFolderPath.isDirectory()) {
        return;
    }
    Module module = facet.getModule();
    if (module.getProject().isDisposed()) {
        return;
    }
    ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
    ModifiableRootModel rootModel = moduleRootManager.getModifiableModel();
    try {
        ContentEntry[] contentEntries = rootModel.getContentEntries();
        ContentEntry parent = findParentContentEntry(buildFolderPath, contentEntries);
        if (parent == null) {
            rootModel.dispose();
            return;
        }
        List<File> excludedFolderPaths = androidModel.getExcludedFolderPaths();
        for (File folderPath : excludedFolderPaths) {
            parent.addExcludeFolder(pathToIdeaUrl(folderPath));
        }
    } finally {
        if (!rootModel.isDisposed()) {
            rootModel.commit();
        }
    }
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) ContentEntry(com.intellij.openapi.roots.ContentEntry) FilePaths.findParentContentEntry(com.android.tools.idea.gradle.util.FilePaths.findParentContentEntry) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 70 with AndroidModuleModel

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

the class PostProjectBuildTasksExecutor method getMaxJavaLangLevel.

@VisibleForTesting
@Nullable
LanguageLevel getMaxJavaLangLevel() {
    LanguageLevel maxLangLevel = null;
    Module[] modules = ModuleManager.getInstance(myProject).getModules();
    for (Module module : modules) {
        AndroidFacet facet = AndroidFacet.getInstance(module);
        if (facet == null) {
            continue;
        }
        AndroidModuleModel androidModel = AndroidModuleModel.get(facet);
        if (androidModel != null) {
            LanguageLevel langLevel = androidModel.getJavaLanguageLevel();
            if (langLevel != null && (maxLangLevel == null || maxLangLevel.compareTo(langLevel) < 0)) {
                maxLangLevel = langLevel;
            }
        }
    }
    return maxLangLevel;
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) VisibleForTesting(com.google.common.annotations.VisibleForTesting) 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