Search in sources :

Example 16 with Variant

use of com.android.builder.model.Variant in project android by JetBrains.

the class VariantSelector method findVariantToSelect.

@Nullable
public Variant findVariantToSelect(@NotNull AndroidProject androidProject) {
    Collection<Variant> variants = androidProject.getVariants();
    if (variants.size() == 1) {
        Variant variant = getFirstItem(variants);
        assert variant != null;
        return variant;
    }
    // trying to match something else may add more complexity for little gain.
    for (Variant variant : variants) {
        if ("debug".equals(variant.getName())) {
            return variant;
        }
    }
    List<Variant> sortedVariants = Lists.newArrayList(variants);
    sort(sortedVariants, (o1, o2) -> o1.getName().compareTo(o2.getName()));
    return sortedVariants.isEmpty() ? null : sortedVariants.get(0);
}
Also used : Variant(com.android.builder.model.Variant) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with Variant

use of com.android.builder.model.Variant in project android by JetBrains.

the class ModuleVariantsInfoDialog method createDependentsTree.

@NotNull
private static JTree createDependentsTree(@NotNull Module module, @NotNull AndroidModuleModel androidModel) {
    VariantCheckboxTreeCellRenderer renderer = new VariantCheckboxTreeCellRenderer() {

        @Override
        public void customizeRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            if (value instanceof DefaultMutableTreeNode) {
                Object data = ((DefaultMutableTreeNode) value).getUserObject();
                if (data instanceof String) {
                    appendVariant((String) data);
                } else if (data instanceof DependentTreeElement) {
                    DependentTreeElement dependent = (DependentTreeElement) data;
                    String variant = null;
                    if (!dependent.myVariants.isEmpty()) {
                        variant = Joiner.on(", ").join(dependent.myVariants);
                    }
                    appendModule(dependent.myModule, variant);
                }
            }
        }
    };
    //noinspection ConstantConditions
    CheckedTreeNode root = new CheckedTreeNode(null);
    AndroidProject androidProject = GradleUtil.getAndroidProject(module);
    assert androidProject != null;
    String gradlePath = GradleUtil.getGradlePath(module);
    assert gradlePath != null;
    Multimap<String, DependentTreeElement> dependentsByVariant = HashMultimap.create();
    ModuleManager moduleManager = ModuleManager.getInstance(module.getProject());
    for (Module dependent : moduleManager.getModuleDependentModules(module)) {
        AndroidProject dependentProject = GradleUtil.getAndroidProject(dependent);
        if (dependentProject == null) {
            continue;
        }
        DependentTreeElement element = new DependentTreeElement(dependent);
        for (Variant variant : dependentProject.getVariants()) {
            for (AndroidLibrary library : GradleUtil.getDirectLibraryDependencies(variant, androidModel)) {
                if (gradlePath.equals(library.getProject())) {
                    element.addVariant(variant.getName());
                    String projectVariant = library.getProjectVariant();
                    if (StringUtil.isNotEmpty(projectVariant)) {
                        dependentsByVariant.put(projectVariant, element);
                    }
                }
            }
        }
    }
    List<String> variantNames = Lists.newArrayList(dependentsByVariant.keySet());
    Collections.sort(variantNames);
    for (String variantName : variantNames) {
        Collection<DependentTreeElement> dependents = dependentsByVariant.get(variantName);
        if (!dependents.isEmpty()) {
            List<DependentTreeElement> sortedDependents = Lists.newArrayList(dependents);
            Collections.sort(sortedDependents);
            DefaultMutableTreeNode variantNode = new DefaultMutableTreeNode(variantName);
            for (DependentTreeElement dependent : dependents) {
                variantNode.add(new DefaultMutableTreeNode(dependent));
            }
            root.add(variantNode);
        }
    }
    CheckboxTree tree = new CheckboxTree(renderer, root);
    tree.setRootVisible(false);
    TreeUtil.expandAll(tree);
    return tree;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) CheckboxTree(com.intellij.ui.CheckboxTree) CheckedTreeNode(com.intellij.ui.CheckedTreeNode) AndroidProject(com.android.builder.model.AndroidProject) ModuleManager(com.intellij.openapi.module.ModuleManager) Variant(com.android.builder.model.Variant) AndroidLibrary(com.android.builder.model.AndroidLibrary) Module(com.intellij.openapi.module.Module) VariantCheckboxTreeCellRenderer(com.android.tools.idea.gradle.variant.ui.VariantCheckboxTreeCellRenderer) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with Variant

use of com.android.builder.model.Variant in project android by JetBrains.

the class BuildVariantUpdater method updateSelectedVariant.

private boolean updateSelectedVariant(@NotNull AndroidFacet androidFacet, @NotNull AndroidModuleModel androidModel, @NotNull String variantToSelect, @NotNull List<AndroidFacet> affectedFacets) {
    Variant selectedVariant = androidModel.getSelectedVariant();
    if (variantToSelect.equals(selectedVariant.getName())) {
        return false;
    }
    androidModel.setSelectedVariantName(variantToSelect);
    androidModel.syncSelectedVariantAndTestArtifact(androidFacet);
    Module module = setUpModule(androidFacet.getModule(), androidModel);
    for (AndroidLibrary library : androidModel.getSelectedMainCompileDependencies().getLibraries()) {
        String gradlePath = library.getProject();
        if (isEmpty(gradlePath)) {
            continue;
        }
        String projectVariant = library.getProjectVariant();
        if (isNotEmpty(projectVariant)) {
            ensureVariantIsSelected(module.getProject(), gradlePath, projectVariant, affectedFacets);
        }
    }
    return true;
}
Also used : NdkVariant(com.android.tools.idea.gradle.project.model.NdkModuleModel.NdkVariant) Variant(com.android.builder.model.Variant) AndroidLibrary(com.android.builder.model.AndroidLibrary) Module(com.intellij.openapi.module.Module)

Example 19 with Variant

use of com.android.builder.model.Variant in project android by JetBrains.

the class AndroidTestRunConfiguration method findInstrumentationRunner.

@Nullable
public static String findInstrumentationRunner(@NotNull AndroidFacet facet) {
    String runner = getRunnerFromManifest(facet);
    // TODO: Resolve direct AndroidGradleModel dep (b/22596984)
    AndroidModuleModel androidModel = AndroidModuleModel.get(facet);
    if (runner == null && androidModel != null) {
        Variant selectedVariant = androidModel.getSelectedVariant();
        String testRunner = selectedVariant.getMergedFlavor().getTestInstrumentationRunner();
        if (testRunner != null) {
            runner = testRunner;
        }
    }
    return runner;
}
Also used : Variant(com.android.builder.model.Variant) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with Variant

use of com.android.builder.model.Variant in project android by JetBrains.

the class GradleApkProvider method getTargetedApks.

/**
   * Gets the list of targeted apks for the specified variant.
   *
   * <p>This is used for test-only modules when specifying the tested apk
   * using the targetProjectPath and targetVariant properties in the build file.
   */
@NotNull
private List<ApkInfo> getTargetedApks(@NotNull Variant selectedVariant, @NotNull IDevice device) throws ApkProvisionException {
    List<ApkInfo> targetedApks = Lists.newArrayList();
    for (TestedTargetVariant testedVariant : getTestedTargetVariants(selectedVariant)) {
        Module targetModule = ApplicationManager.getApplication().runReadAction((Computable<Module>) () -> GradleUtil.findModuleByGradlePath(myFacet.getModule().getProject(), testedVariant.getTargetProjectPath()));
        // target module has to exist, otherwise we would not be able to build test apk
        assert targetModule != null;
        AndroidFacet targetFacet = AndroidFacet.getInstance(targetModule);
        if (targetFacet == null) {
            LOG.warn("Please install tested apk manually.");
            continue;
        }
        AndroidModuleModel targetAndroidModel = AndroidModuleModel.get(targetFacet);
        if (targetAndroidModel == null) {
            LOG.warn("Android model for tested module is null. Sync might have failed.");
            continue;
        }
        Variant targetVariant = targetAndroidModel.findVariantByName(testedVariant.getTargetVariant());
        if (targetVariant == null) {
            LOG.warn("Tested variant not found. Sync might have failed.");
            continue;
        }
        File targetApk = getApk(targetVariant, device);
        targetedApks.add(new ApkInfo(targetApk, targetVariant.getMergedFlavor().getApplicationId()));
    }
    return targetedApks;
}
Also used : Variant(com.android.builder.model.Variant) TestedTargetVariant(com.android.builder.model.TestedTargetVariant) TestedTargetVariant(com.android.builder.model.TestedTargetVariant) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Module(com.intellij.openapi.module.Module) OutputFile(com.android.build.OutputFile) File(java.io.File) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Variant (com.android.builder.model.Variant)23 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)8 AndroidProject (com.android.builder.model.AndroidProject)6 File (java.io.File)6 Module (com.intellij.openapi.module.Module)5 NotNull (org.jetbrains.annotations.NotNull)5 AndroidLibrary (com.android.builder.model.AndroidLibrary)4 OutputFile (com.android.build.OutputFile)3 AndroidArtifact (com.android.builder.model.AndroidArtifact)3 AndroidArtifactOutput (com.android.builder.model.AndroidArtifactOutput)3 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)3 Nullable (org.jetbrains.annotations.Nullable)3 NonNull (com.android.annotations.NonNull)2 TestedTargetVariant (com.android.builder.model.TestedTargetVariant)2 GradleVersion (com.android.ide.common.repository.GradleVersion)2 VariantCheckboxTreeCellRenderer (com.android.tools.idea.gradle.variant.ui.VariantCheckboxTreeCellRenderer)2 ModuleManager (com.intellij.openapi.module.ModuleManager)2 CheckboxTree (com.intellij.ui.CheckboxTree)2 CheckedTreeNode (com.intellij.ui.CheckedTreeNode)2 IOException (java.io.IOException)2