Search in sources :

Example 56 with AndroidModuleModel

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

the class AnalyzeApkAction method getLastSelectedApk.

@Nullable
private static VirtualFile getLastSelectedApk(Project project) {
    String lastApkPath = PropertiesComponent.getInstance(project).getValue(LAST_APK_PATH);
    if (lastApkPath != null) {
        File f = new File(lastApkPath);
        if (f.exists()) {
            return VfsUtil.findFileByIoFile(f, true);
        }
    }
    // see if we can find an apk generated by gradle if this is the first time
    for (Module module : ModuleManager.getInstance(project).getModules()) {
        AndroidModuleModel model = AndroidModuleModel.get(module);
        if (model == null) {
            continue;
        }
        if (model.getProjectType() != PROJECT_TYPE_APP) {
            continue;
        }
        AndroidArtifact mainArtifact = model.getSelectedVariant().getMainArtifact();
        for (AndroidArtifactOutput output : mainArtifact.getOutputs()) {
            File outputFile = output.getMainOutputFile().getOutputFile();
            if (outputFile.exists()) {
                return VfsUtil.findFileByIoFile(outputFile, true);
            }
        }
    }
    return null;
}
Also used : AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) AndroidArtifactOutput(com.android.builder.model.AndroidArtifactOutput) Module(com.intellij.openapi.module.Module) AndroidArtifact(com.android.builder.model.AndroidArtifact) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 57 with AndroidModuleModel

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

the class ManifestPanel method getErrorRemoveHtml.

@NotNull
private static String getErrorRemoveHtml(@NotNull final AndroidFacet facet, @NotNull String message, @NotNull final SourceFilePosition position, @NotNull HtmlLinkManager htmlLinkManager, @Nullable final VirtualFile currentlyOpenFile) {
    /*
    Example Input:
    ERROR Overlay manifest:package attribute declared at AndroidManifest.xml:3:5-49
    value=(com.foo.manifestapplication.debug) has a different value=(com.foo.manifestapplication)
    declared in main manifest at AndroidManifest.xml:5:5-43 Suggestion: remove the overlay
    declaration at AndroidManifest.xml and place it in the build.gradle: flavorName
    { applicationId = "com.foo.manifestapplication.debug" } AndroidManifest.xml (debug)
     */
    HtmlBuilder sb = new HtmlBuilder();
    int start = message.indexOf('{');
    int end = message.indexOf('}', start + 1);
    final String declaration = message.substring(start + 1, end).trim();
    if (!declaration.startsWith("applicationId")) {
        throw new IllegalArgumentException("unexpected remove suggestion format " + message);
    }
    final GradleBuildFile buildFile = GradleBuildFile.get(facet.getModule());
    Runnable link = null;
    if (buildFile != null) {
        final String applicationId = declaration.substring(declaration.indexOf('"') + 1, declaration.lastIndexOf('"'));
        final File manifestOverlayFile = position.getFile().getSourceFile();
        assert manifestOverlayFile != null;
        VirtualFile manifestOverlayVirtualFile = LocalFileSystem.getInstance().findFileByIoFile(manifestOverlayFile);
        assert manifestOverlayVirtualFile != null;
        IdeaSourceProvider sourceProvider = ManifestUtils.findManifestSourceProvider(facet, manifestOverlayVirtualFile);
        assert sourceProvider != null;
        final String name = sourceProvider.getName();
        AndroidModuleModel androidModuleModel = AndroidModuleModel.get(facet.getModule());
        assert androidModuleModel != null;
        final XmlFile manifestOverlayPsiFile = (XmlFile) PsiManager.getInstance(facet.getModule().getProject()).findFile(manifestOverlayVirtualFile);
        assert manifestOverlayPsiFile != null;
        if (androidModuleModel.getBuildTypeNames().contains(name)) {
            final String packageName = MergedManifest.get(facet).getPackage();
            assert packageName != null;
            if (applicationId.startsWith(packageName)) {
                link = () -> new WriteCommandAction.Simple(facet.getModule().getProject(), "Apply manifest suggestion", buildFile.getPsiFile(), manifestOverlayPsiFile) {

                    @Override
                    protected void run() throws Throwable {
                        if (currentlyOpenFile != null) {
                            // We mark this action as affecting the currently open file, so the Undo is available in this editor
                            CommandProcessor.getInstance().addAffectedFiles(facet.getModule().getProject(), currentlyOpenFile);
                        }
                        removePackageAttribute(manifestOverlayPsiFile);
                        final String applicationIdSuffix = applicationId.substring(packageName.length());
                        @SuppressWarnings("unchecked") List<NamedObject> buildTypes = (List<NamedObject>) buildFile.getValue(BuildFileKey.BUILD_TYPES);
                        if (buildTypes == null) {
                            buildTypes = new ArrayList<>();
                        }
                        NamedObject buildType = find(buildTypes, name);
                        if (buildType == null) {
                            buildType = new NamedObject(name);
                            buildTypes.add(buildType);
                        }
                        buildType.setValue(BuildFileKey.APPLICATION_ID_SUFFIX, applicationIdSuffix);
                        buildFile.setValue(BuildFileKey.BUILD_TYPES, buildTypes);
                        GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(facet.getModule().getProject(), null);
                    }
                }.execute();
            }
        } else if (androidModuleModel.getProductFlavorNames().contains(name)) {
            link = () -> new WriteCommandAction.Simple(facet.getModule().getProject(), "Apply manifest suggestion", buildFile.getPsiFile(), manifestOverlayPsiFile) {

                @Override
                protected void run() throws Throwable {
                    if (currentlyOpenFile != null) {
                        // We mark this action as affecting the currently open file, so the Undo is available in this editor
                        CommandProcessor.getInstance().addAffectedFiles(facet.getModule().getProject(), currentlyOpenFile);
                    }
                    removePackageAttribute(manifestOverlayPsiFile);
                    @SuppressWarnings("unchecked") List<NamedObject> flavors = (List<NamedObject>) buildFile.getValue(BuildFileKey.FLAVORS);
                    assert flavors != null;
                    NamedObject flavor = find(flavors, name);
                    assert flavor != null;
                    flavor.setValue(BuildFileKey.APPLICATION_ID, applicationId);
                    buildFile.setValue(BuildFileKey.FLAVORS, flavors);
                    GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(facet.getModule().getProject(), null);
                }
            }.execute();
        }
    }
    if (link != null) {
        sb.addLink(message.substring(0, end + 1), htmlLinkManager.createRunnableLink(link));
        sb.add(message.substring(end + 1));
    } else {
        sb.add(message);
    }
    return sb.getHtml();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) XmlFile(com.intellij.psi.xml.XmlFile) HtmlBuilder(com.android.utils.HtmlBuilder) NamedObject(com.android.tools.idea.gradle.parser.NamedObject) IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) List(java.util.List) 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) NotNull(org.jetbrains.annotations.NotNull)

Example 58 with AndroidModuleModel

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

the class AndroidGradleProjectResolver method populateModuleContentRoots.

@Override
public void populateModuleContentRoots(@NotNull IdeaModule gradleModule, @NotNull DataNode<ModuleData> ideModule) {
    ImportedModule importedModule = new ImportedModule(gradleModule);
    ideModule.createChild(IMPORTED_MODULE, importedModule);
    GradleProject gradleProject = gradleModule.getGradleProject();
    GradleScript buildScript = null;
    try {
        buildScript = gradleProject.getBuildScript();
    } catch (UnsupportedOperationException ignore) {
    }
    if (buildScript == null || !isAndroidGradleProject()) {
        nextResolver.populateModuleContentRoots(gradleModule, ideModule);
        return;
    }
    // do not derive module root dir based on *.iml file location
    File moduleRootDirPath = new File(toSystemDependentName(ideModule.getData().getLinkedExternalProjectPath()));
    AndroidProject androidProject = resolverCtx.getExtraProject(gradleModule, AndroidProject.class);
    boolean androidProjectWithoutVariants = false;
    String moduleName = gradleModule.getName();
    if (androidProject != null) {
        Variant selectedVariant = myVariantSelector.findVariantToSelect(androidProject);
        if (selectedVariant == null) {
            // If an Android project does not have variants, it would be impossible to build. This is a possible but invalid use case.
            // For now we are going to treat this case as a Java library module, because everywhere in the IDE (e.g. run configurations,
            // editors, test support, variants tool window, project building, etc.) we have the assumption that there is at least one variant
            // per Android project, and changing that in the code base is too risky, for very little benefit.
            // See https://code.google.com/p/android/issues/detail?id=170722
            androidProjectWithoutVariants = true;
        } else {
            String variantName = selectedVariant.getName();
            AndroidModuleModel model = new AndroidModuleModel(moduleName, moduleRootDirPath, androidProject, variantName);
            ideModule.createChild(ANDROID_MODEL, model);
        }
    }
    NativeAndroidProject nativeAndroidProject = resolverCtx.getExtraProject(gradleModule, NativeAndroidProject.class);
    if (nativeAndroidProject != null) {
        NdkModuleModel ndkModuleModel = new NdkModuleModel(moduleName, moduleRootDirPath, nativeAndroidProject);
        ideModule.createChild(NDK_MODEL, ndkModuleModel);
    }
    File gradleSettingsFile = new File(moduleRootDirPath, FN_SETTINGS_GRADLE);
    if (gradleSettingsFile.isFile() && androidProject == null && nativeAndroidProject == null) {
        // This is just a root folder for a group of Gradle projects. We don't set an IdeaGradleProject so the JPS builder won't try to
        // compile it using Gradle. We still need to create the module to display files inside it.
        createJavaProject(gradleModule, ideModule, false);
        return;
    }
    BuildScriptClasspathModel buildScriptModel = resolverCtx.getExtraProject(BuildScriptClasspathModel.class);
    String gradleVersion = buildScriptModel != null ? buildScriptModel.getGradleVersion() : null;
    File buildFilePath = buildScript.getSourceFile();
    GradleModuleModel gradleModuleModel = new GradleModuleModel(moduleName, gradleProject, buildFilePath, gradleVersion);
    ideModule.createChild(GRADLE_MODULE_MODEL, gradleModuleModel);
    if (nativeAndroidProject == null && (androidProject == null || androidProjectWithoutVariants)) {
        // This is a Java lib module.
        createJavaProject(gradleModule, ideModule, androidProjectWithoutVariants);
    }
}
Also used : BuildScriptClasspathModel(org.jetbrains.plugins.gradle.model.BuildScriptClasspathModel) ImportedModule(com.android.tools.idea.gradle.ImportedModule) AndroidProject(com.android.builder.model.AndroidProject) NativeAndroidProject(com.android.builder.model.NativeAndroidProject) Variant(com.android.builder.model.Variant) GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) NativeAndroidProject(com.android.builder.model.NativeAndroidProject) GradleScript(org.gradle.tooling.model.gradle.GradleScript) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) GradleProject(org.gradle.tooling.model.GradleProject) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) File(java.io.File)

Example 59 with AndroidModuleModel

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

the class NamedObjectPanel method getObjectsFromModel.

private Collection<NamedObject> getObjectsFromModel(Collection<BuildFileKey> properties) {
    Collection<NamedObject> results = Lists.newArrayList();
    if (myModule == null) {
        return results;
    }
    AndroidFacet facet = AndroidFacet.getInstance(myModule);
    if (facet == null) {
        return results;
    }
    AndroidModuleModel androidModel = AndroidModuleModel.get(facet);
    if (androidModel == null) {
        return results;
    }
    switch(myBuildFileKey) {
        case BUILD_TYPES:
            for (String name : androidModel.getBuildTypeNames()) {
                BuildTypeContainer buildTypeContainer = androidModel.findBuildType(name);
                NamedObject obj = new UndeletableNamedObject(name);
                if (buildTypeContainer == null) {
                    break;
                }
                BuildType buildType = buildTypeContainer.getBuildType();
                obj.setValue(BuildFileKey.DEBUGGABLE, buildType.isDebuggable());
                obj.setValue(BuildFileKey.JNI_DEBUG_BUILD, buildType.isJniDebuggable());
                obj.setValue(BuildFileKey.RENDERSCRIPT_DEBUG_BUILD, buildType.isRenderscriptDebuggable());
                obj.setValue(BuildFileKey.RENDERSCRIPT_OPTIM_LEVEL, buildType.getRenderscriptOptimLevel());
                obj.setValue(BuildFileKey.APPLICATION_ID_SUFFIX, buildType.getApplicationIdSuffix());
                obj.setValue(BuildFileKey.VERSION_NAME_SUFFIX, getVersionNameSuffix(buildType));
                obj.setValue(BuildFileKey.MINIFY_ENABLED, buildType.isMinifyEnabled());
                obj.setValue(BuildFileKey.ZIP_ALIGN, buildType.isZipAlignEnabled());
                results.add(obj);
            }
            break;
        case FLAVORS:
            for (String name : androidModel.getProductFlavorNames()) {
                ProductFlavorContainer productFlavorContainer = androidModel.findProductFlavor(name);
                NamedObject obj = new UndeletableNamedObject(name);
                if (productFlavorContainer == null) {
                    break;
                }
                ProductFlavor flavor = productFlavorContainer.getProductFlavor();
                obj.setValue(BuildFileKey.APPLICATION_ID, flavor.getApplicationId());
                Integer versionCode = flavor.getVersionCode();
                if (versionCode != null) {
                    obj.setValue(BuildFileKey.VERSION_CODE, versionCode);
                }
                obj.setValue(BuildFileKey.VERSION_NAME, flavor.getVersionName());
                if (androidModel.getFeatures().isProductFlavorVersionSuffixSupported()) {
                    obj.setValue(BuildFileKey.VERSION_NAME_SUFFIX, getVersionNameSuffix(flavor));
                }
                ApiVersion minSdkVersion = flavor.getMinSdkVersion();
                if (minSdkVersion != null) {
                    obj.setValue(BuildFileKey.MIN_SDK_VERSION, minSdkVersion.getCodename() != null ? minSdkVersion.getCodename() : minSdkVersion.getApiLevel());
                }
                ApiVersion targetSdkVersion = flavor.getTargetSdkVersion();
                if (targetSdkVersion != null) {
                    obj.setValue(BuildFileKey.TARGET_SDK_VERSION, targetSdkVersion.getCodename() != null ? targetSdkVersion.getCodename() : targetSdkVersion.getApiLevel());
                }
                obj.setValue(BuildFileKey.TEST_APPLICATION_ID, flavor.getTestApplicationId());
                obj.setValue(BuildFileKey.TEST_INSTRUMENTATION_RUNNER, flavor.getTestInstrumentationRunner());
                results.add(obj);
            }
            results.add(new UndeletableNamedObject(DEFAULT_CONFIG));
            break;
        default:
            break;
    }
    return results;
}
Also used : NamedObject(com.android.tools.idea.gradle.parser.NamedObject) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 60 with AndroidModuleModel

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

the class AndroidModuleNode method getChildren.

@NotNull
public static Collection<AbstractTreeNode> getChildren(@NotNull AndroidFacet facet, @NotNull ViewSettings settings, @NotNull AndroidProjectViewPane pane, @NotNull List<IdeaSourceProvider> providers) {
    Project project = facet.getModule().getProject();
    List<AbstractTreeNode> result = Lists.newArrayList();
    AndroidModuleModel androidModuleModel = AndroidModuleModel.get(facet);
    HashMultimap<AndroidSourceType, VirtualFile> sourcesByType = getSourcesBySourceType(providers, androidModuleModel);
    NdkModuleModel ndkModuleModel = NdkModuleModel.get(facet.getModule());
    for (AndroidSourceType sourceType : sourcesByType.keySet()) {
        if (sourceType == AndroidSourceType.CPP && ndkModuleModel != null) {
            // Native sources will be added separately from NativeAndroidGradleModel.
            continue;
        }
        if (sourceType == AndroidSourceType.MANIFEST) {
            result.add(new AndroidManifestsGroupNode(project, facet, settings, sourcesByType.get(sourceType)));
            continue;
        }
        if (sourceType == AndroidSourceType.RES) {
            result.add(new AndroidResFolderNode(project, facet, settings, sourcesByType.get(sourceType), pane));
            continue;
        }
        if (sourceType == AndroidSourceType.SHADERS) {
            if (androidModuleModel == null || !androidModuleModel.getFeatures().isShadersSupported()) {
                continue;
            }
        }
        result.add(new AndroidSourceTypeNode(project, facet, settings, sourceType, sourcesByType.get(sourceType), pane));
    }
    if (ndkModuleModel != null) {
        result.add(new AndroidJniFolderNode(project, ndkModuleModel, settings));
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) AndroidSourceType(org.jetbrains.android.facet.AndroidSourceType) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) NotNull(org.jetbrains.annotations.NotNull)

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