Search in sources :

Example 11 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class GradleUtil method setBuildToolsVersion.

public static void setBuildToolsVersion(@NotNull Project project, @NotNull String version) {
    List<GradleBuildModel> modelsToUpdate = Lists.newArrayList();
    for (Module module : ModuleManager.getInstance(project).getModules()) {
        AndroidFacet facet = AndroidFacet.getInstance(module);
        if (facet != null) {
            GradleBuildModel buildModel = GradleBuildModel.get(module);
            if (buildModel != null) {
                AndroidModel android = buildModel.android();
                if (android != null && !version.equals(android.buildToolsVersion().value())) {
                    android.setBuildToolsVersion(version);
                    modelsToUpdate.add(buildModel);
                }
            }
        }
    }
    if (!modelsToUpdate.isEmpty()) {
        runWriteCommandAction(project, () -> {
            for (GradleBuildModel buildModel : modelsToUpdate) {
                buildModel.applyChanges();
            }
        });
    }
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) AndroidModel(com.android.tools.idea.gradle.dsl.model.android.AndroidModel) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 12 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class ConflictResolution method solveSelectionConflict.

private static boolean solveSelectionConflict(@NotNull Conflict conflict, boolean showConflictResolutionDialog) {
    AndroidFacet facet = AndroidFacet.getInstance(conflict.getSource());
    if (facet == null || !facet.requiresAndroidModel()) {
        // project structure may have changed and the conflict is not longer applicable.
        return true;
    }
    AndroidModuleModel source = AndroidModuleModel.get(facet);
    if (source == null) {
        return false;
    }
    Collection<String> variants = conflict.getVariants();
    if (variants.size() == 1) {
        String expectedVariant = getFirstItem(variants);
        if (isNotEmpty(expectedVariant)) {
            source.setSelectedVariantName(expectedVariant);
            source.syncSelectedVariantAndTestArtifact(facet);
            return true;
        }
    } else if (showConflictResolutionDialog) {
        ConflictResolutionDialog dialog = new ConflictResolutionDialog(conflict);
        if (dialog.showAndGet()) {
            String selectedVariant = dialog.getSelectedVariant();
            if (isNotEmpty(selectedVariant)) {
                source.setSelectedVariantName(selectedVariant);
                source.syncSelectedVariantAndTestArtifact(facet);
                return true;
            }
        }
    }
    return false;
}
Also used : AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 13 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class AddAndroidActivityPath method getDirectories.

private Map<String, Object> getDirectories() {
    Map<String, Object> templateParameters = Maps.newHashMap();
    Module module = getModule();
    assert module != null;
    AndroidFacet facet = AndroidFacet.getInstance(module);
    assert facet != null;
    AndroidPlatform platform = AndroidPlatform.getInstance(module);
    if (platform != null) {
        templateParameters.put(ATTR_BUILD_API, platform.getTarget().getVersion().getFeatureLevel());
        templateParameters.put(ATTR_BUILD_API_STRING, getBuildApiString(platform.getTarget().getVersion()));
    }
    // Read minSdkVersion and package from manifest and/or build.gradle files
    AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(facet);
    AndroidModel androidModel = facet.getAndroidModel();
    SourceProvider sourceProvider1 = myState.get(KEY_SOURCE_PROVIDER);
    if (sourceProvider1 != null && androidModel != null) {
        String packageName = myState.get(KEY_PACKAGE_NAME);
        assert packageName != null;
        templateParameters.putAll(selectSourceProvider(sourceProvider1, androidModel, module, packageName));
    }
    AndroidVersion minSdkVersion = moduleInfo.getMinSdkVersion();
    String minSdkName = minSdkVersion.getApiString();
    templateParameters.put(ATTR_MIN_API, minSdkName);
    templateParameters.put(ATTR_TARGET_API, moduleInfo.getTargetSdkVersion().getApiLevel());
    templateParameters.put(ATTR_MIN_API_LEVEL, minSdkVersion.getFeatureLevel());
    templateParameters.put(ATTR_IS_LIBRARY_MODULE, facet.isLibraryProject());
    try {
        templateParameters.put(ATTR_DEBUG_KEYSTORE_SHA1, KeystoreUtils.sha1(getDebugKeystore(facet)));
    } catch (Exception e) {
        LOG.info("Could not compute SHA1 hash of debug keystore.", e);
        templateParameters.put(ATTR_DEBUG_KEYSTORE_SHA1, "");
    }
    @SuppressWarnings("deprecation") String projectLocation = NewModuleWizardState.ATTR_PROJECT_LOCATION;
    Project project = getProject();
    assert project != null;
    templateParameters.put(projectLocation, project.getBasePath());
    // We're really interested in the directory name on disk, not the module name. These will be different if you give a module the same
    // name as its containing project.
    String moduleName = new File(module.getModuleFilePath()).getParentFile().getName();
    templateParameters.put(FormFactorUtils.ATTR_MODULE_NAME, moduleName);
    return templateParameters;
}
Also used : IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) SourceProvider(com.android.builder.model.SourceProvider) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) AndroidVersion(com.android.sdklib.AndroidVersion) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) Project(com.intellij.openapi.project.Project) AndroidModel(com.android.tools.idea.model.AndroidModel) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 14 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class AppResourceRepository method findAarLibraries.

@NotNull
public static Collection<AndroidLibrary> findAarLibraries(@NotNull AndroidFacet facet) {
    List<AndroidLibrary> libraries = Lists.newArrayList();
    if (facet.requiresAndroidModel()) {
        AndroidModuleModel androidModel = AndroidModuleModel.get(facet);
        if (androidModel != null) {
            List<AndroidFacet> dependentFacets = AndroidUtils.getAllAndroidDependencies(facet.getModule(), true);
            addGradleLibraries(libraries, androidModel);
            for (AndroidFacet dependentFacet : dependentFacets) {
                AndroidModuleModel dependentGradleModel = AndroidModuleModel.get(dependentFacet);
                if (dependentGradleModel != null) {
                    addGradleLibraries(libraries, dependentGradleModel);
                }
            }
        }
    }
    return libraries;
}
Also used : AndroidLibrary(com.android.builder.model.AndroidLibrary) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with AndroidFacet

use of org.jetbrains.android.facet.AndroidFacet in project android by JetBrains.

the class AppResourceRepository method findAarLibrariesFromGradle.

/**
   * Looks up the library dependencies from the Gradle tools model and returns the corresponding {@code .aar}
   * resource directories.
   */
@NotNull
private static Map<File, String> findAarLibrariesFromGradle(@NotNull GradleVersion modelVersion, List<AndroidFacet> dependentFacets, List<AndroidLibrary> libraries) {
    // Pull out the unique directories, in case multiple modules point to the same .aar folder
    Map<File, String> files = new HashMap<>(libraries.size());
    Set<String> moduleNames = Sets.newHashSet();
    for (AndroidFacet f : dependentFacets) {
        moduleNames.add(f.getModule().getName());
    }
    try {
        for (AndroidLibrary library : libraries) {
            // We should only add .aar dependencies if they aren't already provided as modules.
            // For now, the way we associate them with each other is via the library name;
            // in the future the model will provide this for us
            String libraryName = null;
            String projectName = library.getProject();
            if (projectName != null && !projectName.isEmpty()) {
                libraryName = projectName.substring(projectName.lastIndexOf(':') + 1);
                // Since this library has project!=null, it exists in module form; don't
                // add it here.
                moduleNames.add(libraryName);
                continue;
            } else {
                File folder = library.getFolder();
                String name = folder.getName();
                if (modelVersion.getMajor() > 2 || modelVersion.getMajor() == 2 && modelVersion.getMinor() >= 2) {
                    // Library.getName() was added in 2.2
                    libraryName = library.getName();
                } else if (name.endsWith(DOT_AAR)) {
                    libraryName = name.substring(0, name.length() - DOT_AAR.length());
                } else if (folder.getPath().contains(AndroidModuleModel.EXPLODED_AAR)) {
                    libraryName = folder.getParentFile().getName();
                }
            }
            if (libraryName != null && !moduleNames.contains(libraryName)) {
                File resFolder = library.getResFolder();
                if (resFolder.exists()) {
                    files.put(resFolder, libraryName);
                    // Don't add it again!
                    moduleNames.add(libraryName);
                }
            }
        }
    } catch (UnsupportedMethodException e) {
        // This happens when there is an incompatibility between the builder-model interfaces embedded in Android Studio and the
        // cached model.
        // If we got here is because this code got invoked before project sync happened (e.g. when reopening a project with open editors).
        // Project sync now is smart enough to handle this case and will trigger a full sync.
        LOG.warn("Incompatibility found between the IDE's builder-model and the cached Gradle model", e);
    }
    return files;
}
Also used : TObjectIntHashMap(gnu.trove.TObjectIntHashMap) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) AndroidLibrary(com.android.builder.model.AndroidLibrary) UnsupportedMethodException(org.gradle.tooling.model.UnsupportedMethodException) File(java.io.File) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AndroidFacet (org.jetbrains.android.facet.AndroidFacet)299 Module (com.intellij.openapi.module.Module)122 VirtualFile (com.intellij.openapi.vfs.VirtualFile)73 NotNull (org.jetbrains.annotations.NotNull)61 Nullable (org.jetbrains.annotations.Nullable)51 Project (com.intellij.openapi.project.Project)39 File (java.io.File)29 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)28 PsiFile (com.intellij.psi.PsiFile)24 XmlFile (com.intellij.psi.xml.XmlFile)20 PsiElement (com.intellij.psi.PsiElement)17 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)16 XmlTag (com.intellij.psi.xml.XmlTag)16 ArrayList (java.util.ArrayList)16 Manifest (org.jetbrains.android.dom.manifest.Manifest)14 IAndroidTarget (com.android.sdklib.IAndroidTarget)13 ResourceFolderType (com.android.resources.ResourceFolderType)11 Configuration (com.android.tools.idea.configurations.Configuration)10 PsiClass (com.intellij.psi.PsiClass)10 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)10