Search in sources :

Example 1 with AndroidIdeInfo

use of com.google.idea.blaze.base.ideinfo.AndroidIdeInfo in project intellij by bazelbuild.

the class BlazeAndroidWorkspaceImporter method buildAndroidResourceModules.

private ImmutableList<AndroidResourceModule> buildAndroidResourceModules(WorkspaceBuilder workspaceBuilder) {
    // Filter empty resource modules
    Stream<AndroidResourceModule> androidResourceModuleStream = workspaceBuilder.androidResourceModules.stream().map(AndroidResourceModule.Builder::build).filter(androidResourceModule -> !androidResourceModule.isEmpty()).filter(androidResourceModule -> !androidResourceModule.resources.isEmpty());
    List<AndroidResourceModule> androidResourceModules = androidResourceModuleStream.collect(Collectors.toList());
    // Detect, filter, and warn about multiple R classes
    Multimap<String, AndroidResourceModule> javaPackageToResourceModule = ArrayListMultimap.create();
    for (AndroidResourceModule androidResourceModule : androidResourceModules) {
        TargetIdeInfo target = targetMap.get(androidResourceModule.targetKey);
        AndroidIdeInfo androidIdeInfo = target.androidIdeInfo;
        assert androidIdeInfo != null;
        javaPackageToResourceModule.put(androidIdeInfo.resourceJavaPackage, androidResourceModule);
    }
    List<AndroidResourceModule> result = Lists.newArrayList();
    for (String resourceJavaPackage : javaPackageToResourceModule.keySet()) {
        Collection<AndroidResourceModule> androidResourceModulesWithJavaPackage = javaPackageToResourceModule.get(resourceJavaPackage);
        if (androidResourceModulesWithJavaPackage.size() == 1) {
            result.addAll(androidResourceModulesWithJavaPackage);
        } else {
            StringBuilder messageBuilder = new StringBuilder();
            messageBuilder.append("Multiple R classes generated with the same java package ").append(resourceJavaPackage).append(".R: ");
            messageBuilder.append('\n');
            for (AndroidResourceModule androidResourceModule : androidResourceModulesWithJavaPackage) {
                messageBuilder.append("  ").append(androidResourceModule.targetKey).append('\n');
            }
            String message = messageBuilder.toString();
            context.output(new PerformanceWarning(message));
            IssueOutput.warn(message).submit(context);
            result.add(selectBestAndroidResourceModule(androidResourceModulesWithJavaPackage));
        }
    }
    Collections.sort(result, (lhs, rhs) -> lhs.targetKey.compareTo(rhs.targetKey));
    return ImmutableList.copyOf(result);
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) GeneratedAndroidResourcesSection(com.google.idea.blaze.android.projectview.GeneratedAndroidResourcesSection) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact) Multimap(com.google.common.collect.Multimap) BlazeResourceLibrary(com.google.idea.blaze.android.sync.model.BlazeResourceLibrary) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) Kind(com.google.idea.blaze.base.model.primitives.Kind) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) AarLibrary(com.google.idea.blaze.android.sync.model.AarLibrary) IssueOutput(com.google.idea.blaze.base.scope.output.IssueOutput) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) Project(com.intellij.openapi.project.Project) TransitiveResourceMap(com.google.idea.blaze.android.sync.importer.aggregators.TransitiveResourceMap) ProjectViewTargetImportFilter(com.google.idea.blaze.base.sync.projectview.ProjectViewTargetImportFilter) LanguageClass(com.google.idea.blaze.base.model.primitives.LanguageClass) PerformanceWarning(com.google.idea.blaze.base.scope.output.PerformanceWarning) JavaSourceFilter(com.google.idea.blaze.java.sync.importer.JavaSourceFilter) ImmutableSet(com.google.common.collect.ImmutableSet) AndroidResourceModule(com.google.idea.blaze.android.sync.model.AndroidResourceModule) ArtifactLocation(com.google.idea.blaze.base.ideinfo.ArtifactLocation) Collection(java.util.Collection) Set(java.util.Set) ComparisonChain(com.google.common.collect.ComparisonChain) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) BlazeAndroidImportResult(com.google.idea.blaze.android.sync.model.BlazeAndroidImportResult) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) GeneratedResourceWarnings(com.google.idea.blaze.android.sync.importer.problems.GeneratedResourceWarnings) List(java.util.List) Stream(java.util.stream.Stream) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) WorkspaceRoot(com.google.idea.blaze.base.model.primitives.WorkspaceRoot) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) Collections(java.util.Collections) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) AndroidResourceModule(com.google.idea.blaze.android.sync.model.AndroidResourceModule) PerformanceWarning(com.google.idea.blaze.base.scope.output.PerformanceWarning) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo)

Example 2 with AndroidIdeInfo

use of com.google.idea.blaze.base.ideinfo.AndroidIdeInfo in project intellij by bazelbuild.

the class BlazeClassJarProvider method getModuleExternalLibraries.

@Override
public List<File> getModuleExternalLibraries(Module module) {
    BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
    if (blazeProjectData == null) {
        return ImmutableList.of();
    }
    TargetMap targetMap = blazeProjectData.targetMap;
    ArtifactLocationDecoder decoder = blazeProjectData.artifactLocationDecoder;
    AndroidResourceModuleRegistry registry = AndroidResourceModuleRegistry.getInstance(project);
    TargetIdeInfo target = targetMap.get(registry.getTargetKey(module));
    if (target == null) {
        return ImmutableList.of();
    }
    AppResourceRepository repository = AppResourceRepository.getOrCreateInstance(module);
    ImmutableList.Builder<File> results = ImmutableList.builder();
    for (TargetKey dependencyTargetKey : TransitiveDependencyMap.getInstance(project).getTransitiveDependencies(target.key)) {
        TargetIdeInfo dependencyTarget = targetMap.get(dependencyTargetKey);
        if (dependencyTarget == null) {
            continue;
        }
        // Add all import jars as external libraries.
        JavaIdeInfo javaIdeInfo = dependencyTarget.javaIdeInfo;
        if (javaIdeInfo != null) {
            for (LibraryArtifact jar : javaIdeInfo.jars) {
                if (jar.classJar != null && jar.classJar.isSource()) {
                    results.add(decoder.decode(jar.classJar));
                }
            }
        }
        // Tell ResourceClassRegistry which repository contains our resources and the java packages of
        // the resources that we're interested in.
        // When the class loader tries to load a custom view, and the view references resource
        // classes, layoutlib will ask the class loader for these resource classes.
        // If these resource classes are in a separate jar from the target (i.e., in a dependency),
        // then offering their jars will lead to a conflict in the resource IDs.
        // So instead, the resource class generator will produce dummy resource classes with
        // non-conflicting IDs to satisfy the class loader.
        // The resource repository remembers the dynamic IDs that it handed out and when the layoutlib
        // calls to ask about the name and content of a given resource ID, the repository can just
        // answer what it has already stored.
        AndroidIdeInfo androidIdeInfo = dependencyTarget.androidIdeInfo;
        if (androidIdeInfo != null && !Strings.isNullOrEmpty(androidIdeInfo.resourceJavaPackage) && repository != null) {
            ResourceClassRegistry.get(module.getProject()).addLibrary(repository, androidIdeInfo.resourceJavaPackage);
        }
    }
    return results.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) AndroidResourceModuleRegistry(com.google.idea.blaze.android.sync.model.AndroidResourceModuleRegistry) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) JavaIdeInfo(com.google.idea.blaze.base.ideinfo.JavaIdeInfo) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap)

Example 3 with AndroidIdeInfo

use of com.google.idea.blaze.base.ideinfo.AndroidIdeInfo in project intellij by bazelbuild.

the class BlazeAndroidJavaSyncAugmenter method addJarsForSourceTarget.

@Override
public void addJarsForSourceTarget(WorkspaceLanguageSettings workspaceLanguageSettings, ProjectViewSet projectViewSet, TargetIdeInfo target, Collection<BlazeJarLibrary> jars, Collection<BlazeJarLibrary> genJars) {
    if (!workspaceLanguageSettings.isLanguageActive(LanguageClass.ANDROID)) {
        return;
    }
    AndroidIdeInfo androidIdeInfo = target.androidIdeInfo;
    if (androidIdeInfo == null) {
        return;
    }
    LibraryArtifact idlJar = androidIdeInfo.idlJar;
    if (idlJar != null) {
        genJars.add(new BlazeJarLibrary(idlJar));
    }
    Set<String> whitelistedGenResourcePaths = projectViewSet.listItems(GeneratedAndroidResourcesSection.KEY).stream().map(genfilesPath -> genfilesPath.relativePath).collect(Collectors.toSet());
    if (BlazeAndroidWorkspaceImporter.shouldGenerateResources(androidIdeInfo) && !BlazeAndroidWorkspaceImporter.shouldGenerateResourceModule(androidIdeInfo, whitelistedGenResourcePaths)) {
        // Add blaze's output unless it's a top level rule.
        // In these cases the resource jar contains the entire
        // transitive closure of R classes. It's unlikely this is wanted to resolve in the IDE.
        boolean discardResourceJar = target.kindIsOneOf(Kind.ANDROID_BINARY, Kind.ANDROID_TEST);
        if (!discardResourceJar) {
            LibraryArtifact resourceJar = androidIdeInfo.resourceJar;
            if (resourceJar != null) {
                jars.add(new BlazeJarLibrary(resourceJar));
            }
        }
    }
}
Also used : LanguageClass(com.google.idea.blaze.base.model.primitives.LanguageClass) GeneratedAndroidResourcesSection(com.google.idea.blaze.android.projectview.GeneratedAndroidResourcesSection) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact) Collection(java.util.Collection) Set(java.util.Set) BlazeAndroidWorkspaceImporter(com.google.idea.blaze.android.sync.importer.BlazeAndroidWorkspaceImporter) WorkspaceLanguageSettings(com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings) Collectors(java.util.stream.Collectors) BlazeJavaSyncAugmenter(com.google.idea.blaze.java.sync.BlazeJavaSyncAugmenter) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) Kind(com.google.idea.blaze.base.model.primitives.Kind) BlazeJarLibrary(com.google.idea.blaze.java.sync.model.BlazeJarLibrary) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) BlazeJarLibrary(com.google.idea.blaze.java.sync.model.BlazeJarLibrary) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact)

Example 4 with AndroidIdeInfo

use of com.google.idea.blaze.base.ideinfo.AndroidIdeInfo in project intellij by bazelbuild.

the class BlazeAndroidLiteJavaSyncAugmenter method addJarsForSourceTarget.

@Override
public void addJarsForSourceTarget(WorkspaceLanguageSettings workspaceLanguageSettings, ProjectViewSet projectViewSet, TargetIdeInfo target, Collection<BlazeJarLibrary> jars, Collection<BlazeJarLibrary> genJars) {
    if (!workspaceLanguageSettings.isLanguageActive(LanguageClass.ANDROID)) {
        return;
    }
    AndroidIdeInfo androidIdeInfo = target.androidIdeInfo;
    if (androidIdeInfo == null) {
        return;
    }
    // Add R.java jars
    LibraryArtifact resourceJar = androidIdeInfo.resourceJar;
    if (resourceJar != null) {
        jars.add(new BlazeJarLibrary(resourceJar));
    }
    LibraryArtifact idlJar = androidIdeInfo.idlJar;
    if (idlJar != null) {
        genJars.add(new BlazeJarLibrary(idlJar));
    }
}
Also used : BlazeJarLibrary(com.google.idea.blaze.java.sync.model.BlazeJarLibrary) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact)

Example 5 with AndroidIdeInfo

use of com.google.idea.blaze.base.ideinfo.AndroidIdeInfo in project intellij by bazelbuild.

the class BlazeAndroidWorkspaceImporter method addSourceTarget.

private void addSourceTarget(WorkspaceBuilder workspaceBuilder, TransitiveResourceMap transitiveResourceMap, TargetIdeInfo target) {
    AndroidIdeInfo androidIdeInfo = target.androidIdeInfo;
    assert androidIdeInfo != null;
    if (shouldGenerateResources(androidIdeInfo) && shouldGenerateResourceModule(androidIdeInfo, whitelistedGenResourcePaths)) {
        AndroidResourceModule.Builder builder = new AndroidResourceModule.Builder(target.key);
        workspaceBuilder.androidResourceModules.add(builder);
        for (ArtifactLocation artifactLocation : androidIdeInfo.resources) {
            if (artifactLocation.isSource()) {
                builder.addResource(artifactLocation);
            } else {
                workspaceBuilder.generatedResourceLocations.add(artifactLocation);
                if (whitelistedGenResourcePaths.contains(artifactLocation.relativePath)) {
                    // Still track location in generatedResourceLocations, so that we can warn if a
                    // whitelist entry goes unused and can be removed.
                    builder.addResource(artifactLocation);
                }
            }
        }
        TransitiveResourceMap.TransitiveResourceInfo transitiveResourceInfo = transitiveResourceMap.get(target.key);
        for (ArtifactLocation artifactLocation : transitiveResourceInfo.transitiveResources) {
            if (artifactLocation.isSource()) {
                builder.addTransitiveResource(artifactLocation);
            } else {
                workspaceBuilder.generatedResourceLocations.add(artifactLocation);
                if (whitelistedGenResourcePaths.contains(artifactLocation.relativePath)) {
                    builder.addTransitiveResource(artifactLocation);
                }
            }
        }
        for (TargetKey resourceDependency : transitiveResourceInfo.transitiveResourceTargets) {
            if (!resourceDependency.equals(target.key)) {
                builder.addTransitiveResourceDependency(resourceDependency);
            }
        }
    }
}
Also used : AndroidResourceModule(com.google.idea.blaze.android.sync.model.AndroidResourceModule) ArtifactLocation(com.google.idea.blaze.base.ideinfo.ArtifactLocation) TransitiveResourceMap(com.google.idea.blaze.android.sync.importer.aggregators.TransitiveResourceMap) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo)

Aggregations

AndroidIdeInfo (com.google.idea.blaze.base.ideinfo.AndroidIdeInfo)10 TargetKey (com.google.idea.blaze.base.ideinfo.TargetKey)7 TargetIdeInfo (com.google.idea.blaze.base.ideinfo.TargetIdeInfo)6 AndroidResourceModule (com.google.idea.blaze.android.sync.model.AndroidResourceModule)4 ArtifactLocation (com.google.idea.blaze.base.ideinfo.ArtifactLocation)4 LibraryArtifact (com.google.idea.blaze.base.ideinfo.LibraryArtifact)4 Kind (com.google.idea.blaze.base.model.primitives.Kind)4 ImmutableList (com.google.common.collect.ImmutableList)3 GeneratedAndroidResourcesSection (com.google.idea.blaze.android.projectview.GeneratedAndroidResourcesSection)3 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 TransitiveResourceMap (com.google.idea.blaze.android.sync.importer.aggregators.TransitiveResourceMap)2 AndroidResourceModuleRegistry (com.google.idea.blaze.android.sync.model.AndroidResourceModuleRegistry)2 AndroidSdkPlatform (com.google.idea.blaze.android.sync.model.AndroidSdkPlatform)2 BlazeAndroidSyncData (com.google.idea.blaze.android.sync.model.BlazeAndroidSyncData)2 JavaIdeInfo (com.google.idea.blaze.base.ideinfo.JavaIdeInfo)2 TargetMap (com.google.idea.blaze.base.ideinfo.TargetMap)2 LanguageClass (com.google.idea.blaze.base.model.primitives.LanguageClass)2 ProjectViewSet (com.google.idea.blaze.base.projectview.ProjectViewSet)2 ArtifactLocationDecoder (com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder)2