Search in sources :

Example 6 with TransformTask

use of com.android.build.gradle.internal.pipeline.TransformTask in project atlas by alibaba.

the class TransformReplacer method replaceDexTransform.

public void replaceDexTransform(AppVariantContext appVariantContext, BaseVariantOutput vod) {
    List<TransformTask> baseTransforms = TransformManager.findTransformTaskByTransformType(variantContext, DexTransform.class);
    DefaultDexOptions dexOptions = appVariantContext.getAppExtension().getDexOptions();
    DexingType dexingType = appVariantContext.getScope().getDexingType();
    DexByteCodeConverterHook dexByteCodeConverterHook = new DexByteCodeConverterHook(variantContext, variantContext.getAppVariantOutputContext(ApkDataUtils.get(vod)), LoggerWrapper.getLogger(DexByteCodeConverterHook.class), appVariantContext.getScope().getGlobalScope().getAndroidBuilder().getTargetInfo(), new GradleJavaProcessExecutor(appVariantContext.getProject()), appVariantContext.getProject().getLogger().isEnabled(LogLevel.INFO), new ExtraModelInfo(appVariantContext.getScope().getGlobalScope().getProjectOptions(), appVariantContext.getProject().getLogger()));
    for (TransformTask transformTask : baseTransforms) {
        DexTransform newTransform = new DexTransform(dexOptions, dexingType, false, appVariantContext.getProject().files(variantContext.getScope().getMainDexListFile()), verifyNotNull(appVariantContext.getScope().getGlobalScope().getAndroidBuilder().getTargetInfo(), "Target Info not set."), dexByteCodeConverterHook, appVariantContext.getScope().getGlobalScope().getAndroidBuilder().getErrorReporter(), variantContext.getScope().getMinSdkVersion().getFeatureLevel());
        ReflectUtils.updateField(transformTask, "transform", newTransform);
    }
}
Also used : DexingType(com.android.builder.dexing.DexingType) ExtraModelInfo(com.android.build.gradle.internal.ExtraModelInfo) DexByteCodeConverterHook(com.taobao.android.builder.hook.dex.DexByteCodeConverterHook) TransformTask(com.android.build.gradle.internal.pipeline.TransformTask) DefaultDexOptions(com.android.builder.core.DefaultDexOptions) GradleJavaProcessExecutor(com.android.build.gradle.internal.process.GradleJavaProcessExecutor)

Example 7 with TransformTask

use of com.android.build.gradle.internal.pipeline.TransformTask in project atlas by alibaba.

the class TransformReplacer method disableCache.

public void disableCache() {
    List<TransformTask> list = null;
    if (usingIncrementalDexing(variantContext.getScope())) {
        list = TransformManager.findTransformTaskByTransformType(variantContext, MainDexListTransform.class);
    } else {
        list = TransformManager.findTransformTaskByTransformType(variantContext, MultiDexTransform.class);
    }
    if (list != null) {
        for (TransformTask transformTask : list) {
            List<Object> list1 = (List<Object>) ReflectUtils.getField(DefaultTaskOutputs.class, transformTask.getOutputs(), "cacheIfSpecs");
            list1.clear();
            ReflectUtils.updateField(transformTask.getOutputs(), "upToDateSpec", AndSpec.empty());
        }
    }
    list = TransformManager.findTransformTaskByTransformType(variantContext, AtlasProguardTransform.class);
    if (list != null) {
        for (TransformTask transformTask : list) {
            List<Object> list1 = (List<Object>) ReflectUtils.getField(DefaultTaskOutputs.class, transformTask.getOutputs(), "cacheIfSpecs");
            list1.clear();
            ReflectUtils.updateField(transformTask.getOutputs(), "upToDateSpec", AndSpec.empty());
        }
    }
    list = TransformManager.findTransformTaskByTransformType(variantContext, DexTransform.class);
    if (list != null) {
        for (TransformTask transformTask : list) {
            List<Object> list1 = (List<Object>) ReflectUtils.getField(DefaultTaskOutputs.class, transformTask.getOutputs(), "cacheIfSpecs");
            list1.clear();
            ReflectUtils.updateField(transformTask.getOutputs(), "upToDateSpec", AndSpec.empty());
        }
    }
    list = TransformManager.findTransformTaskByTransformType(variantContext, AtlasMultiDexListTransform.class);
    if (list != null) {
        for (TransformTask transformTask : list) {
            List<Object> list1 = (List<Object>) ReflectUtils.getField(DefaultTaskOutputs.class, transformTask.getOutputs(), "cacheIfSpecs");
            list1.clear();
            ReflectUtils.updateField(transformTask.getOutputs(), "upToDateSpec", AndSpec.empty());
        }
    }
}
Also used : DefaultTaskOutputs(org.gradle.api.internal.tasks.DefaultTaskOutputs) AtlasMultiDexListTransform(com.taobao.android.builder.tasks.transform.dex.AtlasMultiDexListTransform) TransformTask(com.android.build.gradle.internal.pipeline.TransformTask) List(java.util.List)

Example 8 with TransformTask

use of com.android.build.gradle.internal.pipeline.TransformTask in project atlas by alibaba.

the class TransformManager method findTransformTaskByTransformType.

public static List<TransformTask> findTransformTaskByTransformType(VariantContext appVariantContext, Class<?> transformClass) {
    List<TransformTask> transformTasksList = Lists.newArrayList();
    VariantConfiguration config = appVariantContext.getVariantConfiguration();
    TaskCollection<TransformTask> transformTasks = appVariantContext.getProject().getTasks().withType(TransformTask.class);
    SortedMap<String, TransformTask> transformTaskSortedMap = transformTasks.getAsMap();
    String variantName = config.getFullName();
    for (String taskName : transformTaskSortedMap.keySet()) {
        TransformTask transformTask = transformTaskSortedMap.get(taskName);
        if (variantName == transformTask.getVariantName()) {
            if (transformTask.getTransform().getClass() == transformClass) {
                transformTasksList.add(transformTask);
            }
        }
    }
    return transformTasksList;
}
Also used : TransformTask(com.android.build.gradle.internal.pipeline.TransformTask) VariantConfiguration(com.android.builder.core.VariantConfiguration)

Example 9 with TransformTask

use of com.android.build.gradle.internal.pipeline.TransformTask 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 10 with TransformTask

use of com.android.build.gradle.internal.pipeline.TransformTask in project atlas by alibaba.

the class BasePlugin method configureProject.

protected void configureProject() {
    extraModelInfo = new ExtraModelInfo(project, isLibrary());
    checkGradleVersion();
    sdkHandler = new SdkHandler(project, getLogger());
    project.afterEvaluate(p -> {
        if (!p.getGradle().getStartParameter().isOffline() && AndroidGradleOptions.getUseSdkDownload(p)) {
            SdkLibData sdkLibData = SdkLibData.download(getDownloader(), getSettingsController());
            dependencyManager.setSdkLibData(sdkLibData);
            sdkHandler.setSdkLibData(sdkLibData);
        }
    });
    androidBuilder = new AndroidBuilder(project == project.getRootProject() ? project.getName() : project.getPath(), creator, new GradleProcessExecutor(project), new GradleJavaProcessExecutor(project), extraModelInfo, getLogger(), isVerbose());
    dataBindingBuilder = new DataBindingBuilder();
    dataBindingBuilder.setPrintMachineReadableOutput(extraModelInfo.getErrorFormatMode() == ExtraModelInfo.ErrorFormatMode.MACHINE_PARSABLE);
    project.getPlugins().apply(JavaBasePlugin.class);
    jacocoPlugin = project.getPlugins().apply(JacocoPlugin.class);
    project.getTasks().getByName("assemble").setDescription("Assembles all variants of all applications and secondary packages.");
    // call back on execution. This is called after the whole build is done (not
    // after the current project is done).
    // This is will be called for each (android) projects though, so this should support
    // being called 2+ times.
    project.getGradle().addBuildListener(new BuildListener() {

        private final LibraryCache libraryCache = LibraryCache.getCache();

        @Override
        public void buildStarted(Gradle gradle) {
        }

        @Override
        public void settingsEvaluated(Settings settings) {
        }

        @Override
        public void projectsLoaded(Gradle gradle) {
        }

        @Override
        public void projectsEvaluated(Gradle gradle) {
        }

        @Override
        public void buildFinished(BuildResult buildResult) {
            ExecutorSingleton.shutdown();
            sdkHandler.unload();
            ThreadRecorder.get().record(ExecutionType.BASE_PLUGIN_BUILD_FINISHED, project.getPath(), null, new Recorder.Block() {

                @Override
                public Void call() throws Exception {
                    PreDexCache.getCache().clear(new File(project.getRootProject().getBuildDir(), FD_INTERMEDIATES + "/dex-cache/cache.xml"), getLogger());
                    JackConversionCache.getCache().clear(new File(project.getRootProject().getBuildDir(), FD_INTERMEDIATES + "/jack-cache/cache.xml"), getLogger());
                    libraryCache.unload();
                    Main.clearInternTables();
                    return null;
                }
            });
        }
    });
    project.getGradle().getTaskGraph().addTaskExecutionGraphListener(new TaskExecutionGraphListener() {

        @Override
        public void graphPopulated(TaskExecutionGraph taskGraph) {
            for (Task task : taskGraph.getAllTasks()) {
                if (task instanceof TransformTask) {
                    Transform transform = ((TransformTask) task).getTransform();
                    if (transform instanceof DexTransform) {
                        PreDexCache.getCache().load(new File(project.getRootProject().getBuildDir(), FD_INTERMEDIATES + "/dex-cache/cache.xml"));
                        break;
                    } else if (transform instanceof JackPreDexTransform) {
                        JackConversionCache.getCache().load(new File(project.getRootProject().getBuildDir(), FD_INTERMEDIATES + "/jack-cache/cache.xml"));
                        break;
                    }
                }
            }
        }
    });
}
Also used : TaskExecutionGraphListener(org.gradle.api.execution.TaskExecutionGraphListener) SdkLibData(com.android.builder.sdk.SdkLibData) TransformTask(com.android.build.gradle.internal.pipeline.TransformTask) Task(org.gradle.api.Task) JackPreDexTransform(com.android.build.gradle.tasks.JackPreDexTransform) JacocoPlugin(com.android.build.gradle.internal.coverage.JacocoPlugin) DataBindingBuilder(android.databinding.tool.DataBindingBuilder) BuildListener(org.gradle.BuildListener) BuildResult(org.gradle.BuildResult) ExtraModelInfo(com.android.build.gradle.internal.ExtraModelInfo) GradleProcessExecutor(com.android.build.gradle.internal.process.GradleProcessExecutor) LibraryCache(com.android.build.gradle.internal.LibraryCache) AndroidBuilder(com.android.builder.core.AndroidBuilder) TransformTask(com.android.build.gradle.internal.pipeline.TransformTask) Gradle(org.gradle.api.invocation.Gradle) SdkHandler(com.android.build.gradle.internal.SdkHandler) Transform(com.android.build.api.transform.Transform) DexTransform(com.android.build.gradle.internal.transforms.DexTransform) JackPreDexTransform(com.android.build.gradle.tasks.JackPreDexTransform) File(java.io.File) TaskExecutionGraph(org.gradle.api.execution.TaskExecutionGraph) DexTransform(com.android.build.gradle.internal.transforms.DexTransform) JackPreDexTransform(com.android.build.gradle.tasks.JackPreDexTransform) Settings(org.gradle.api.initialization.Settings) GradleJavaProcessExecutor(com.android.build.gradle.internal.process.GradleJavaProcessExecutor)

Aggregations

TransformTask (com.android.build.gradle.internal.pipeline.TransformTask)18 File (java.io.File)4 List (java.util.List)3 BaseVariantOutput (com.android.build.gradle.api.BaseVariantOutput)2 ExtraModelInfo (com.android.build.gradle.internal.ExtraModelInfo)2 GradleJavaProcessExecutor (com.android.build.gradle.internal.process.GradleJavaProcessExecutor)2 VariantScope (com.android.build.gradle.internal.scope.VariantScope)2 DefaultDexOptions (com.android.builder.core.DefaultDexOptions)2 VariantConfiguration (com.android.builder.core.VariantConfiguration)2 DexingType (com.android.builder.dexing.DexingType)2 AtlasMultiDexListTransform (com.taobao.android.builder.tasks.transform.dex.AtlasMultiDexListTransform)2 IOException (java.io.IOException)2 Task (org.gradle.api.Task)2 DataBindingBuilder (android.databinding.tool.DataBindingBuilder)1 QualifiedContent (com.android.build.api.transform.QualifiedContent)1 Transform (com.android.build.api.transform.Transform)1 LibraryVariantOutput (com.android.build.gradle.api.LibraryVariantOutput)1 LibraryCache (com.android.build.gradle.internal.LibraryCache)1 SdkHandler (com.android.build.gradle.internal.SdkHandler)1 TaskContainerAdaptor (com.android.build.gradle.internal.TaskContainerAdaptor)1