Android开发之玩转 Gradle,你可不能不熟悉 Transform

AppExtension appExtension = project.getExtensions().getByType(AppExtension.class);

appExtension.registerTransform(new DoubleTabTransform(project));

当我们在编写一个Transformplugin的时候,其实是对安卓的AppExtension进行了一个注册Transform的操作而已,那么Transform的本质到底是什么呢?

高能预警,下面的源代码比较长,可以考虑直接跳过看结论,但是看得懂的同学最好可以学习下。

public class LibraryTaskManager extends TaskManager {

@Override

public void createTasksForVariantScope(@NonNull final VariantScope variantScope) {

// ----- External Transforms -----

// apply all the external transforms.

List customTransforms = extension.getTransforms();

List<List> customTransformsDependencies = extension.getTransformsDependencies();

for (int i = 0, count = customTransforms.size(); i < count; i++) {

Transform transform = customTransforms.get(i);

// Check the transform only applies to supported scopes for libraries:

// We cannot transform scopes that are not packaged in the library

// itself.

Sets.SetView<? super Scope> difference =

Sets.difference(transform.getScopes(), TransformManager.PROJECT_ONLY);

if (!difference.isEmpty()) {

String scopes = difference.toString();

globalScope

.getAndroidBuilder()

.getIssueReporter()

.reportError(

Type.GENERIC,

new EvalIssueException(

String.format(

“Transforms with scopes ‘%s’ cannot be applied to library projects.”,

scopes)));

}

List deps = customTransformsDependencies.get(i);

transformManager.addTransform(

taskFactory,

variantScope,

transform,

null,

task -> {

if (!deps.isEmpty()) {

task.dependsOn(deps);

}

},

taskProvider -> {

// if the task is a no-op then we make assemble task

// depend on it.

if (transform.getScopes().isEmpty()) {

TaskFactoryUtils.dependsOn(

variantScope.getTaskContainer().getAssembleTask(),

taskProvider);

}

});

}

// Now add transforms for intermediate publishing (projects to projects).

File jarOutputFolder = variantScope.getIntermediateJarOutputFolder();

File mainClassJar = new File(jarOutputFolder, FN_CLASSES_JAR);

File mainResJar = new File(jarOutputFolder, FN_INTERMEDIATE_RES_JAR);

LibraryIntermediateJarsTransform intermediateTransform =

new LibraryIntermediateJarsTransform(

mainClassJar,

mainResJar,

variantConfig::getPackage
FromManifest,

extension.getPackageBuildConfig());

excludeDataBindingClassesIfNecessary(variantScope, intermediateTransform);

BuildArtifactsHolder artifacts = variantScope.getArtifacts();

transformManager.addTransform(

taskFactory,

variantScope,

intermediateTransform,

taskName -> {

// publish the intermediate classes.jar

artifacts.appendArtifact(

InternalArtifactType.LIBRARY_CLASSES,

ImmutableList.of(mainClassJar),

taskName);

// publish the res jar

artifacts.appendArtifact(

InternalArtifactType.LIBRARY_JAVA_RES,

ImmutableList.of(mainResJar),

taskName);

},

null,

null);

taskFactory.register(new LibraryDexingTask.CreationAction(variantScope));

// Create a jar with both classes and java resources. This artifact is not

// used by the Android application plugin and the task usually don’t need to

// be executed. The artifact is useful for other Gradle users who needs the

// ‘jar’ artifact as API dependency.

taskFactory.register(new ZipMergingTask.CreationAction(variantScope));

// now add a transform that will take all the native libs and package

// them into an intermediary folder. This processes only the PROJECT

// scope.

final File intermediateJniLibsFolder = new File(jarOutputFolder, FD_JNI);

LibraryJniLibsTransform intermediateJniTransform =

new LibraryJniLibsTransform(

“intermediateJniLibs”,

intermediateJniLibsFolder,

TransformManager.PROJECT_ONLY);

transformManager.addTransform(

taskFactory,

variantScope,

intermediateJniTransform,

taskName -> {

// publish the jni folder as intermediate

variantScope

.getArtifacts()

.appendArtifact(

InternalArtifactType.LIBRARY_JNI,

ImmutableList.of(intermediateJniLibsFolder),

taskName);

},

null,

null);

// Now go back to fill the pipeline with transforms used when

// publishing the AAR

// first merge the resources. This takes the PROJECT and LOCAL_DEPS

// and merges them together.

createMergeJavaResTransform(variantScope);

// ----- Minify next -----

maybeCreateJavaCodeShrinkerTransform(variantScope);

maybeCreateResourcesShrinkerTransform(variantScope);

// now add a transform that will take all the class/res and package them

// into the main and secondary jar files that goes in the AAR.

// This transform technically does not use its transform output, but that’s

// ok. We use the transform mechanism to get incremental data from

// the streams.

// This is used for building the AAR.

File classesJar = variantScope.getAarClassesJar();

File libsDirectory = variantScope.getAarLibsDirectory();

LibraryAarJarsTransform transform =

new LibraryAarJarsTransform(

classesJar,

libsDirectory,

artifacts.hasArtifact(InternalArtifactType.ANNOTATIONS_TYPEDEF_FILE)

? artifacts.getFinalArtifactFiles(

InternalArtifactType.ANNOTATIONS_TYPEDEF_FILE)
null,

variantConfig::getPackageFromManifest,

extension.getPackageBuildConfig());

excludeDataBindingClassesIfNecessary(variantScope, transform);

transformManager.addTransform(

taskFactory,

variantScope,

transform,

taskName -> {

variantScope

.getArtifacts()

.appendArtifact(

InternalArtifactType.AAR_MAIN_JAR,

ImmutableList.of(classesJar),

taskName);

variantScope

.getArtifacts()

.appendArtifact(

InternalArtifactType.AAR_LIBS_DIRECTORY,

ImmutableList.of(libsDirectory),

taskName);

},

null,

null);

// now add a transform that will take all the native libs and package

// them into the libs folder of the bundle. This processes both the PROJECT

// and the LOCAL_PROJECT scopes

final File jniLibsFolder =

variantScope.getIntermediateDir(InternalArtifactType.LIBRARY_AND_LOCAL_JARS_JNI);

LibraryJniLibsTransform jniTransform =

new LibraryJniLibsTransform(

“syncJniLibs”,

jniLibsFolder,

TransformManager.SCOPE_FULL_LIBRARY_WITH_LOCAL_JARS);

transformManager.addTransform(

taskFactory,

variantScope,

jniTransform,

taskName ->

variantScope

.getArtifacts()

.appendArtifact(

InternalArtifactType.LIBRARY_AND_LOCAL_JARS_JNI,

ImmutableList.of(jniLibsFolder),

taskName),

null,

null);

createLintTasks(variantScope);

createBundleTask(variantScope);

}

}

自定义Transform和其他系统Transform执行的顺序

而且上述方法我们可以看出,任务还是会根据DAG(有向无环图)生成Task,其中会包含一些系统的Transform,其顺序有可能会被插入到自定义的Transform之前,而有一些则会被放置在所有的Tranform执行之后。比如LibraryJniLibsTransform

Transform和Task的关系

从这部分源代码其实我们就可以看出,我们注册到AppExtension里面的Transform,最后会createTasksForVariantScope方法调用到。

@NonNull

public Optional<TaskProvider> addTransform(

@NonNull TaskFactory taskFactory,

@NonNull TransformVariantScope scope,

@NonNull T transform,

@Nullable PreConfigAction preConfigAction,

@Nullable TaskConfigAction configAction,

@Nullable TaskProviderCallback providerCallback) {

if (!validateTransform(transform)) {

// validate either throws an exception, or records the problem during sync

// so it’s safe to just return null here.

return Optional.empty();

}

List inputStreams = Lists.newArrayList();

String taskName = scope.getTaskName(getTaskNamePrefix(transform));

// get referenced-only streams

List referencedStreams = grabReferencedStreams(transform);

// find input streams, and compute output streams for the transform.

IntermediateStream outputStream = findTransformStreams(

transform,

scope,

inputStreams,

taskName,

scope.getGlobalScope().getBuildDir());

if (inputStreams.isEmpty() && referencedStreams.isEmpty()) {

// didn’t find any match. Means there is a broken order somewhere in the streams.

issueReporter.reportError(

Type.GENERIC,

new EvalIssueException(

最后

小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

资料⬅专栏获取
tStreams.isEmpty() && referencedStreams.isEmpty()) {

// didn’t find any match. Means there is a broken order somewhere in the streams.

issueReporter.reportError(

Type.GENERIC,

new EvalIssueException(

最后

小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

[外链图片转存中…(img-laRP6qYx-1718989704413)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

资料⬅专栏获取

  • 20
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值