Search in sources :

Example 1 with FilteredArtifactCollection

use of com.android.build.gradle.internal.dependency.FilteredArtifactCollection in project atlas by alibaba.

the class AtlasFeatureTaskManager method runTask.

@Override
public void runTask() {
    if (featureExtension.getBaseFeature()) {
        return;
    }
    featureExtension.getFeatureVariants().forEach(featureVariant -> {
        FeatureVariantContext featureVariantContext = new FeatureVariantContext((FeatureVariantImpl) featureVariant, project, atlasExtension, featureExtension);
        ArtifactCollection allArtifacts = featureVariantContext.getScope().getArtifactCollection(AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, AndroidArtifacts.ArtifactScope.EXTERNAL, AndroidArtifacts.ArtifactType.CLASSES);
        ArtifactCollection artifacts = featureVariantContext.getScope().getArtifactCollection(AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, AndroidArtifacts.ArtifactScope.ALL, AndroidArtifacts.ArtifactType.FEATURE_TRANSITIVE_DEPS);
        ArtifactCollection filterArtifacts = new FilteredArtifactCollection(featureVariantContext.getProject(), allArtifacts, artifacts.getArtifactFiles());
        List<TransformTask> transformTasks = TransformManager.findTransformTaskByTransformType(featureVariantContext, FixStackFramesTransform.class);
        if (transformTasks != null) {
            for (TransformTask transformTask : transformTasks) {
                try {
                    Field field = StreamBasedTask.class.getDeclaredField("consumedInputStreams");
                    Field field1 = StreamBasedTask.class.getDeclaredField("referencedInputStreams");
                    field1.setAccessible(true);
                    field.setAccessible(true);
                    Collection<TransformStream> consumedInputStreams = (Collection<TransformStream>) field.get(transformTask);
                    Collection<TransformStream> referencedInputStreams = (Collection<TransformStream>) field1.get(transformTask);
                    for (TransformStream stream : consumedInputStreams) {
                        if (stream.getContentTypes().contains(QualifiedContent.DefaultContentType.CLASSES) && stream.getScopes().contains(QualifiedContent.Scope.EXTERNAL_LIBRARIES)) {
                            ReflectUtils.updateField(stream, "fileCollection", filterArtifacts.getArtifactFiles());
                            ReflectUtils.updateField(stream, "artifactCollection", filterArtifacts);
                            break;
                        }
                    }
                    for (TransformStream transformStream : referencedInputStreams) {
                        if (transformStream.getContentTypes().contains(QualifiedContent.DefaultContentType.CLASSES) && transformStream.getScopes().contains(QualifiedContent.Scope.PROVIDED_ONLY)) {
                            ReflectUtils.updateField(transformStream, "fileCollection", project.files());
                        // ReflectUtils.updateField(transformStream, "artifactCollection", filterArtifacts);
                        }
                    }
                } catch (Exception e) {
                }
            }
        }
        featureVariantContext.getScope().getProcessResourcesTask().get(new TaskContainerAdaptor(featureVariantContext.getProject().getTasks())).setEnableAapt2(true);
    });
    featureExtension.getLibraryVariants().forEach(libraryVariant -> {
        LibVariantContext libVariantContext = new LibVariantContext((LibraryVariantImpl) libraryVariant, project, atlasExtension, featureExtension);
        TBuildType tBuildType = libVariantContext.getBuildType();
        if (null != tBuildType) {
            try {
                new AwoPropHandler().process(tBuildType, atlasExtension.getBundleConfig());
            } catch (Exception e) {
                throw new GradleException("process awo exception", e);
            }
        }
        AwbGenerator awbGenerator = new AwbGenerator(atlasExtension);
        Collection<BaseVariantOutput> list = libVariantContext.getBaseVariant().getOutputs();
        if (null != list) {
            for (BaseVariantOutput libVariantOutputData : list) {
                Zip zipTask = ((LibraryVariantOutput) (libVariantOutputData)).getPackageLibrary();
                if (atlasExtension.getBundleConfig().isJarEnabled()) {
                    new JarExtractTask().generateJarArtifict(zipTask);
                }
                // Build the awb and extension
                // if (atlasExtension.getBundleConfig().isAwbBundle()) {
                awbGenerator.generateAwbArtifict(zipTask, libVariantContext);
                if (null != tBuildType && (StringUtils.isNotEmpty(tBuildType.getBaseApDependency()) || null != tBuildType.getBaseApFile()) && libraryVariant.getName().equals("debug")) {
                    atlasExtension.getTBuildConfig().setUseCustomAapt(true);
                    libVariantContext.setBundleTask(zipTask);
                    try {
                        libVariantContext.setAwbBundle(awbGenerator.createAwbBundle(libVariantContext));
                    } catch (IOException e) {
                        throw new GradleException("set awb bundle error");
                    }
                }
            }
        }
    });
}
Also used : Zip(org.gradle.api.tasks.bundling.Zip) FeatureVariantContext(com.android.build.gradle.internal.api.FeatureVariantContext) JarExtractTask(com.taobao.android.builder.tasks.library.JarExtractTask) LibVariantContext(com.android.build.gradle.internal.api.LibVariantContext) IOException(java.io.IOException) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) TaskContainerAdaptor(com.android.build.gradle.internal.TaskContainerAdaptor) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException) AwoPropHandler(com.taobao.android.builder.tools.ideaplugin.AwoPropHandler) Field(java.lang.reflect.Field) BaseVariantOutput(com.android.build.gradle.api.BaseVariantOutput) AwbGenerator(com.taobao.android.builder.tasks.library.AwbGenerator) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) GradleException(org.gradle.api.GradleException) LibraryVariantOutput(com.android.build.gradle.api.LibraryVariantOutput) TransformTask(com.android.build.gradle.internal.pipeline.TransformTask) Collection(java.util.Collection) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) FileCollection(org.gradle.api.file.FileCollection) TransformStream(com.android.build.gradle.internal.pipeline.TransformStream) TBuildType(com.taobao.android.builder.extension.TBuildType)

Example 2 with FilteredArtifactCollection

use of com.android.build.gradle.internal.dependency.FilteredArtifactCollection in project atlas by alibaba.

the class AtlasDependencyGraph method computeArtifactCollection.

public static ArtifactCollection computeArtifactCollection(VariantScope variantScope, @NonNull AtlasAndroidArtifacts.ConsumedConfigType configType, @NonNull AndroidArtifacts.ArtifactScope scope, @NonNull AtlasAndroidArtifacts.AtlasArtifactType artifactType) {
    Configuration configuration;
    switch(configType) {
        case COMPILE_CLASSPATH:
            configuration = variantScope.getVariantData().getVariantDependency().getCompileClasspath();
            break;
        case RUNTIME_CLASSPATH:
            configuration = variantScope.getVariantData().getVariantDependency().getRuntimeClasspath();
            break;
        case BUNDLECOMPILE_CLASSPATH:
            configuration = variantScope.getGlobalScope().getProject().getConfigurations().maybeCreate(AtlasPlugin.BUNDLE_COMPILE);
            break;
        case ANNOTATION_PROCESSOR:
            configuration = variantScope.getVariantData().getVariantDependency().getAnnotationProcessorConfiguration();
            break;
        case METADATA_VALUES:
            configuration = variantScope.getVariantData().getVariantDependency().getMetadataValuesConfiguration();
            break;
        default:
            throw new RuntimeException("unknown ConfigType value");
    }
    Action<AttributeContainer> attributes = container -> container.attribute(ARTIFACT_TYPE, artifactType.getType());
    Spec<ComponentIdentifier> filter = getComponentFilter(scope);
    boolean lenientMode = Boolean.TRUE.equals(variantScope.getGlobalScope().getProjectOptions().get(BooleanOption.IDE_BUILD_MODEL_ONLY));
    ArtifactCollection artifacts = configuration.getIncoming().artifactView(config -> {
        config.attributes(attributes);
        if (filter != null) {
            config.componentFilter(filter);
        }
        // TODO somehow read the unresolved dependencies?
        config.lenient(lenientMode);
    }).getArtifacts();
    if (configType == AtlasAndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH && variantScope.getVariantConfiguration().getType() == VariantType.FEATURE && artifactType != AtlasAndroidArtifacts.AtlasArtifactType.FEATURE_TRANSITIVE_DEPS) {
        artifacts = new FilteredArtifactCollection(variantScope.getGlobalScope().getProject(), artifacts, computeArtifactCollection(variantScope, AtlasAndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, scope, AtlasAndroidArtifacts.AtlasArtifactType.FEATURE_TRANSITIVE_DEPS).getArtifactFiles());
    }
    return artifacts;
}
Also used : ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) AtlasPlugin(com.taobao.android.builder.AtlasPlugin) AtlasAndroidArtifacts(com.android.build.gradle.internal.publishing.AtlasAndroidArtifacts) CreatingCache(com.android.ide.common.caching.CreatingCache) Matcher(java.util.regex.Matcher) FileUtils(com.android.utils.FileUtils) ResolvedVariantResult(org.gradle.api.artifacts.result.ResolvedVariantResult) com.google.common.collect(com.google.common.collect) MavenCoordinatesImpl(com.android.builder.dependency.MavenCoordinatesImpl) Artifact(org.gradle.api.component.Artifact) GraphItem(com.android.builder.model.level2.GraphItem) Project(org.gradle.api.Project) Nullable(com.android.annotations.Nullable) AndroidArtifacts(com.android.build.gradle.internal.publishing.AndroidArtifacts) BooleanOption(com.android.build.gradle.options.BooleanOption) ComponentArtifactIdentifier(org.gradle.api.artifacts.component.ComponentArtifactIdentifier) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) Spec(org.gradle.api.specs.Spec) NonNull(com.android.annotations.NonNull) VariantType(com.android.builder.core.VariantType) ARTIFACT_TYPE(com.android.build.gradle.internal.publishing.AndroidArtifacts.ARTIFACT_TYPE) com.android.build.gradle.internal.ide.level2(com.android.build.gradle.internal.ide.level2) Pattern(java.util.regex.Pattern) ResolvedArtifactResult(org.gradle.api.artifacts.result.ResolvedArtifactResult) EMPTY_DEPENDENCY_GRAPH(com.android.build.gradle.internal.ide.ModelBuilder.EMPTY_DEPENDENCY_GRAPH) JvmLibrary(org.gradle.jvm.JvmLibrary) java.util(java.util) SdkConstants(com.android.SdkConstants) OpaqueComponentArtifactIdentifier(org.gradle.internal.component.local.model.OpaqueComponentArtifactIdentifier) VariantAttr(com.android.build.gradle.internal.dependency.VariantAttr) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) ConfigurationDependencyGraphs(com.android.build.gradle.internal.dependency.ConfigurationDependencyGraphs) VariantScope(com.android.build.gradle.internal.scope.VariantScope) EMPTY_DEPENDENCIES_IMPL(com.android.build.gradle.internal.ide.ModelBuilder.EMPTY_DEPENDENCIES_IMPL) Configuration(org.gradle.api.artifacts.Configuration) ImmutableCollectors(com.android.utils.ImmutableCollectors) DependencyHandler(org.gradle.api.artifacts.dsl.DependencyHandler) AttributeContainer(org.gradle.api.attributes.AttributeContainer) com.android.builder.model(com.android.builder.model) ArtifactResolutionQuery(org.gradle.api.artifacts.query.ArtifactResolutionQuery) SourcesArtifact(org.gradle.language.base.artifact.SourcesArtifact) Action(org.gradle.api.Action) Library(com.android.builder.model.level2.Library) File(java.io.File) ModuleComponentIdentifier(org.gradle.api.artifacts.component.ModuleComponentIdentifier) Consumer(java.util.function.Consumer) ArtifactCollectionWithExtraArtifact(com.android.build.gradle.internal.dependency.ArtifactCollectionWithExtraArtifact) JavaLibraryImpl(com.android.build.gradle.internal.ide.level2.JavaLibraryImpl) Preconditions(com.google.common.base.Preconditions) DependencyGraphs(com.android.builder.model.level2.DependencyGraphs) Configuration(org.gradle.api.artifacts.Configuration) ArtifactCollection(org.gradle.api.artifacts.ArtifactCollection) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection) ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) ModuleComponentIdentifier(org.gradle.api.artifacts.component.ModuleComponentIdentifier) AttributeContainer(org.gradle.api.attributes.AttributeContainer) FilteredArtifactCollection(com.android.build.gradle.internal.dependency.FilteredArtifactCollection)

Aggregations

FilteredArtifactCollection (com.android.build.gradle.internal.dependency.FilteredArtifactCollection)2 ArtifactCollection (org.gradle.api.artifacts.ArtifactCollection)2 SdkConstants (com.android.SdkConstants)1 NonNull (com.android.annotations.NonNull)1 Nullable (com.android.annotations.Nullable)1 BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)1 LibraryVariantOutput (com.android.build.gradle.api.LibraryVariantOutput)1 TaskContainerAdaptor (com.android.build.gradle.internal.TaskContainerAdaptor)1 FeatureVariantContext (com.android.build.gradle.internal.api.FeatureVariantContext)1 LibVariantContext (com.android.build.gradle.internal.api.LibVariantContext)1 ArtifactCollectionWithExtraArtifact (com.android.build.gradle.internal.dependency.ArtifactCollectionWithExtraArtifact)1 ConfigurationDependencyGraphs (com.android.build.gradle.internal.dependency.ConfigurationDependencyGraphs)1 VariantAttr (com.android.build.gradle.internal.dependency.VariantAttr)1 EMPTY_DEPENDENCIES_IMPL (com.android.build.gradle.internal.ide.ModelBuilder.EMPTY_DEPENDENCIES_IMPL)1 EMPTY_DEPENDENCY_GRAPH (com.android.build.gradle.internal.ide.ModelBuilder.EMPTY_DEPENDENCY_GRAPH)1 com.android.build.gradle.internal.ide.level2 (com.android.build.gradle.internal.ide.level2)1 JavaLibraryImpl (com.android.build.gradle.internal.ide.level2.JavaLibraryImpl)1 TransformStream (com.android.build.gradle.internal.pipeline.TransformStream)1 TransformTask (com.android.build.gradle.internal.pipeline.TransformTask)1 AndroidArtifacts (com.android.build.gradle.internal.publishing.AndroidArtifacts)1