Search in sources :

Example 1 with NamedObject

use of com.android.tools.idea.gradle.parser.NamedObject in project android by JetBrains.

the class KeystoreUtils method getGradleDebugKeystore.

/**
   * Gets a custom debug keystore defined in the build.gradle file for this module
   *
   * @return null if there is no custom debug keystore configured, or if the project is not a Gradle project.
   */
@Nullable
private static File getGradleDebugKeystore(@NotNull AndroidFacet facet) {
    GradleSettingsFile gradleSettingsFile = GradleSettingsFile.get(facet.getModule().getProject());
    if (gradleSettingsFile == null) {
        return null;
    }
    String modulePath = GradleSettingsFile.getModuleGradlePath(facet.getModule());
    if (modulePath == null) {
        return null;
    }
    final GradleBuildFile moduleBuildFile = gradleSettingsFile.getModuleBuildFile(modulePath);
    if (moduleBuildFile == null) {
        return null;
    }
    Iterable<NamedObject> signingConfigs = ApplicationManager.getApplication().runReadAction(new Computable<Iterable<NamedObject>>() {

        @Override
        @SuppressWarnings("unchecked")
        public Iterable<NamedObject> compute() {
            return (Iterable<NamedObject>) moduleBuildFile.getValue(BuildFileKey.SIGNING_CONFIGS);
        }
    });
    if (signingConfigs == null) {
        return null;
    }
    for (NamedObject namedObject : signingConfigs) {
        if (!"debug".equals(namedObject.getName())) {
            continue;
        }
        File debugKey = (File) namedObject.getValue(BuildFileKey.STORE_FILE);
        if (debugKey == null) {
            continue;
        }
        // NOTE: debugKey.getParent() is the current working directory.
        return new File(ModuleUtilCore.getModuleDirPath(facet.getModule()), debugKey.getPath());
    }
    return null;
}
Also used : GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) NamedObject(com.android.tools.idea.gradle.parser.NamedObject) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) File(java.io.File) GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with NamedObject

use of com.android.tools.idea.gradle.parser.NamedObject 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 3 with NamedObject

use of com.android.tools.idea.gradle.parser.NamedObject 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 4 with NamedObject

use of com.android.tools.idea.gradle.parser.NamedObject in project android by JetBrains.

the class NamedObjectPanel method updatePanelGroup.

private void updatePanelGroup() {
    List<String> values = Lists.newArrayList();
    for (NamedObject o : myListModel) {
        values.add(o.getName());
    }
    myPanelGroup.valuesUpdated(myBuildFileKey, values);
}
Also used : NamedObject(com.android.tools.idea.gradle.parser.NamedObject)

Example 5 with NamedObject

use of com.android.tools.idea.gradle.parser.NamedObject in project android by JetBrains.

the class NamedObjectPanel method updateUiFromCurrentObject.

private void updateUiFromCurrentObject() {
    try {
        myUpdating = true;
        NamedObject currentObject = getSelectedObject();
        myCurrentObject = currentObject;
        myObjectName.setText(currentObject != null ? currentObject.getName() : "");
        myObjectName.setEnabled(currentObject != null);
        myDetailsPane.setCurrentBuildFileObject(myCurrentObject != null ? myCurrentObject.getValues() : null);
        myDetailsPane.setCurrentModelObject(myCurrentObject != null ? myModelObjects.get(myCurrentObject.getName()) : null);
        myDetailsPane.updateUiFromCurrentObject();
        validateName();
    } finally {
        myUpdating = false;
    }
}
Also used : NamedObject(com.android.tools.idea.gradle.parser.NamedObject)

Aggregations

NamedObject (com.android.tools.idea.gradle.parser.NamedObject)8 BuildFileKey (com.android.tools.idea.gradle.parser.BuildFileKey)2 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)2 ValueFactory (com.android.tools.idea.gradle.parser.ValueFactory)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 File (java.io.File)2 List (java.util.List)2 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)2 SourceFile (com.android.ide.common.blame.SourceFile)1 GradleSettingsFile (com.android.tools.idea.gradle.parser.GradleSettingsFile)1 HtmlBuilder (com.android.utils.HtmlBuilder)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 XmlFile (com.intellij.psi.xml.XmlFile)1 JBList (com.intellij.ui.components.JBList)1 SortedList (com.intellij.util.containers.SortedList)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1 IdeaSourceProvider (org.jetbrains.android.facet.IdeaSourceProvider)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1