Search in sources :

Example 76 with AndroidFacet

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

the class AndroidPrecompileTask method checkArtifacts.

private static boolean checkArtifacts(@NotNull CompileContext context) {
    final Project project = context.getProject();
    final CompileScope scope = context.getCompileScope();
    final Set<Artifact> artifacts = ApplicationManager.getApplication().runReadAction(new Computable<Set<Artifact>>() {

        @Override
        public Set<Artifact> compute() {
            return ArtifactCompileScope.getArtifactsToBuild(project, scope, false);
        }
    });
    if (artifacts == null) {
        return true;
    }
    final Set<Artifact> debugArtifacts = new HashSet<>();
    final Set<Artifact> releaseArtifacts = new HashSet<>();
    final Map<AndroidFacet, List<Artifact>> facet2artifacts = new HashMap<>();
    for (final Artifact artifact : artifacts) {
        final ArtifactProperties<?> properties = artifact.getProperties(AndroidArtifactPropertiesProvider.getInstance());
        if (properties instanceof AndroidApplicationArtifactProperties) {
            final AndroidArtifactSigningMode mode = ((AndroidApplicationArtifactProperties) properties).getSigningMode();
            if (mode == AndroidArtifactSigningMode.DEBUG || mode == AndroidArtifactSigningMode.DEBUG_WITH_CUSTOM_CERTIFICATE) {
                debugArtifacts.add(artifact);
            } else {
                releaseArtifacts.add(artifact);
            }
        }
        final AndroidFacet facet = ApplicationManager.getApplication().runReadAction(new Computable<AndroidFacet>() {

            @Nullable
            @Override
            public AndroidFacet compute() {
                return AndroidArtifactUtil.getPackagedFacet(project, artifact);
            }
        });
        if (facet != null) {
            List<Artifact> list = facet2artifacts.get(facet);
            if (list == null) {
                list = new ArrayList<>();
                facet2artifacts.put(facet, list);
            }
            list.add(artifact);
        }
    }
    boolean success = true;
    if (debugArtifacts.size() > 0 && releaseArtifacts.size() > 0) {
        final String message = "Cannot build debug and release Android artifacts in the same session\n" + "Debug artifacts: " + toString(debugArtifacts) + "\n" + "Release artifacts: " + toString(releaseArtifacts);
        context.addMessage(CompilerMessageCategory.ERROR, message, null, -1, -1);
        success = false;
    }
    if (releaseArtifacts.size() > 0 && CompileStepBeforeRun.getRunConfiguration(context) != null) {
        final String message = "Cannot build release Android artifacts in the 'make before run' session\n" + "Release artifacts: " + toString(releaseArtifacts);
        context.addMessage(CompilerMessageCategory.ERROR, message, null, -1, -1);
        success = false;
    }
    for (Map.Entry<AndroidFacet, List<Artifact>> entry : facet2artifacts.entrySet()) {
        final List<Artifact> list = entry.getValue();
        final String moduleName = entry.getKey().getModule().getName();
        if (list.size() > 1) {
            final Artifact firstArtifact = list.get(0);
            final Object[] firstArtifactProGuardOptions = getProGuardOptions(firstArtifact);
            for (int i = 1; i < list.size(); i++) {
                final Artifact artifact = list.get(i);
                if (!Arrays.equals(getProGuardOptions(artifact), firstArtifactProGuardOptions)) {
                    context.addMessage(CompilerMessageCategory.ERROR, "Artifacts related to the same module '" + moduleName + "' have different ProGuard options: " + firstArtifact.getName() + ", " + artifact.getName(), null, -1, -1);
                    success = false;
                    break;
                }
            }
        }
    }
    return success;
}
Also used : HashSet(com.intellij.util.containers.hash.HashSet) HashMap(com.intellij.util.containers.HashMap) AndroidApplicationArtifactProperties(org.jetbrains.android.compiler.artifact.AndroidApplicationArtifactProperties) HashSet(com.intellij.util.containers.hash.HashSet) Artifact(com.intellij.packaging.artifacts.Artifact) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) ArtifactCompileScope(com.intellij.packaging.impl.compiler.ArtifactCompileScope) AndroidArtifactSigningMode(org.jetbrains.android.compiler.artifact.AndroidArtifactSigningMode) HashMap(com.intellij.util.containers.HashMap) Nullable(org.jetbrains.annotations.Nullable)

Example 77 with AndroidFacet

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

the class AndroidPrecompileTask method prepareForCompilation.

private static boolean prepareForCompilation(CompileContext context) {
    final Project project = context.getProject();
    if (!checkArtifacts(context)) {
        return false;
    }
    checkAndroidDependencies(context);
    ExcludesConfiguration configuration = CompilerConfiguration.getInstance(project).getExcludedEntriesConfiguration();
    Set<ExcludeEntryDescription> addedEntries = new HashSet<>();
    for (Module module : ModuleManager.getInstance(project).getModules()) {
        final AndroidFacet facet = AndroidFacet.getInstance(module);
        if (facet == null) {
            continue;
        }
        if (context.isRebuild()) {
            clearResCache(facet, context);
        }
        final AndroidPlatform platform = facet.getConfiguration().getAndroidPlatform();
        final int platformToolsRevision = platform != null ? platform.getSdkData().getPlatformToolsRevision() : -1;
        LOG.debug("Platform-tools revision for module " + module.getName() + " is " + platformToolsRevision);
        if (!facet.isAppProject()) {
            if (platformToolsRevision >= 0 && platformToolsRevision <= 7) {
                LOG.debug("Excluded sources of module " + module.getName());
                excludeAllSourceRoots(module, configuration, addedEntries);
            } else {
                // todo: support this by project converter to use on compile-server
                unexcludeAllSourceRoots(facet, configuration);
            }
        }
    }
    if (addedEntries.size() > 0) {
        LOG.debug("Files excluded by Android: " + addedEntries.size());
        project.getMessageBus().connect().subscribe(CompilerTopics.COMPILATION_STATUS, new MyCompilationStatusListener(project, addedEntries));
    }
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) ExcludesConfiguration(com.intellij.openapi.compiler.options.ExcludesConfiguration) ExcludeEntryDescription(com.intellij.openapi.compiler.options.ExcludeEntryDescription) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) HashSet(com.intellij.util.containers.hash.HashSet)

Example 78 with AndroidFacet

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

the class AndroidCompileUtil method isExcludedFromCompilation.

public static boolean isExcludedFromCompilation(VirtualFile child, @Nullable Project project) {
    final CompilerManager compilerManager = project != null ? CompilerManager.getInstance(project) : null;
    if (compilerManager == null) {
        return false;
    }
    if (!compilerManager.isExcludedFromCompilation(child)) {
        return false;
    }
    final Module module = ModuleUtil.findModuleForFile(child, project);
    if (module == null) {
        return true;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null || facet.isAppProject()) {
        return true;
    }
    final AndroidPlatform platform = facet.getConfiguration().getAndroidPlatform();
    if (platform == null) {
        return true;
    }
    // we exclude sources of library modules automatically for tools r7 or previous
    return platform.getSdkData().getPlatformToolsRevision() > 7;
}
Also used : CompilerManager(com.intellij.openapi.compiler.CompilerManager) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 79 with AndroidFacet

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

the class AndroidCompileUtil method isLibraryWithBadCircularDependency.

// support for lib<->lib and app<->lib circular dependencies
// see IDEA-79737 for details
public static boolean isLibraryWithBadCircularDependency(@NotNull AndroidFacet facet) {
    if (!facet.canBeDependency()) {
        return false;
    }
    final List<AndroidFacet> dependencies = AndroidUtils.getAllAndroidDependencies(facet.getModule(), false);
    final Manifest manifest = facet.getManifest();
    if (manifest == null) {
        return false;
    }
    final String aPackage = manifest.getPackage().getValue();
    if (aPackage == null || aPackage.length() == 0) {
        return false;
    }
    for (AndroidFacet depFacet : dependencies) {
        final List<AndroidFacet> depDependencies = AndroidUtils.getAllAndroidDependencies(depFacet.getModule(), true);
        if (depDependencies.contains(facet) && dependencies.contains(depFacet) && (depFacet.getModule().getName().compareTo(facet.getModule().getName()) < 0 || !depFacet.canBeDependency())) {
            return true;
        }
    }
    return false;
}
Also used : Manifest(org.jetbrains.android.dom.manifest.Manifest) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 80 with AndroidFacet

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

the class AndroidPrecompileTask method checkAndroidDependencies.

private static void checkAndroidDependencies(@NotNull CompileContext context) {
    for (Module module : context.getCompileScope().getAffectedModules()) {
        final AndroidFacet facet = AndroidFacet.getInstance(module);
        if (facet == null) {
            continue;
        }
        final Pair<String, VirtualFile> manifestMergerProp = AndroidRootUtil.getProjectPropertyValue(module, AndroidUtils.ANDROID_MANIFEST_MERGER_PROPERTY);
        if (manifestMergerProp != null && Boolean.parseBoolean(manifestMergerProp.getFirst())) {
            context.addMessage(CompilerMessageCategory.WARNING, "[" + module.getName() + "] " + AndroidBundle.message("android.manifest.merger.not.supported.error"), manifestMergerProp.getSecond().getUrl(), -1, -1);
        }
        if (facet.isAppProject()) {
            for (OrderEntry entry : ModuleRootManager.getInstance(module).getOrderEntries()) {
                if (entry instanceof ModuleOrderEntry) {
                    final ModuleOrderEntry moduleOrderEntry = (ModuleOrderEntry) entry;
                    if (moduleOrderEntry.getScope() == DependencyScope.COMPILE) {
                        final Module depModule = moduleOrderEntry.getModule();
                        if (depModule != null) {
                            final AndroidFacet depFacet = AndroidFacet.getInstance(depModule);
                            if (depFacet != null && !depFacet.canBeDependency()) {
                                String message = "Suspicious module dependency " + module.getName() + " -> " + depModule.getName() + ": Android application module depends on other application module. Possibly, you should ";
                                if (AndroidMavenUtil.isMavenizedModule(depModule)) {
                                    message += "change packaging type of module " + depModule.getName() + " to 'apklib' in pom.xml file or ";
                                }
                                message += "change dependency scope to 'Provided'.";
                                context.addMessage(CompilerMessageCategory.WARNING, message, null, -1, -1);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) OrderEntry(com.intellij.openapi.roots.OrderEntry) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

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