/** * Returns file collection containing all raw Android resources, including the ones from * transitive dependencies. * * <p><strong>This is an incubating API, and it can be changed or removed without * notice.</strong> */ @Incubating @NonNull FileCollection getAllRawAndroidResources();
/** * 这里也判断了shrinkResources,我们默认资源缩减是false,所以在不配置android.precompileDependenciesResources=false的情况下,一直返回true,如果 * android.precompileDependenciesResources设置false或者shrinkResources=true,都会返回false。 */ public boolean isPrecompileDependenciesResourcesEnabled() { // Resource shrinker expects MergeResources task to have all the resources merged and with // overlay rules applied, so we have to go through the MergeResources pipeline in case it's // enabled, see b/134766811. return globalScope.getProjectOptions().get(BooleanOption.PRECOMPILE_DEPENDENCIES_RESOURCES) && !useResourceShrinker(); }
同时在为true的情况下,在依赖处理的逻辑中注册了转化flat的Transform
1 2 3 4 5 6
if (globalScope.projectOptions[BooleanOption.PRECOMPILE_DEPENDENCIES_RESOURCES]) { dependencies.registerTransform( AarResourcesCompilerTransform::class.java ) ...... }
chenyulong01deMacBook-Pro:MyActivityTest chenyulong01$ ./gradlew -q help --task app:mergeReleaseResources Detailed task information for app:mergeReleaseResources Path :app:mergeReleaseResources Type MergeResources (com.android.build.gradle.tasks.MergeResources) Description - Group - chenyulong01deMacBook-Pro:MyActivityTest chenyulong01$
// this is full run, clean the previous outputs File destinationDir = getOutputDir().get().getAsFile(); FileUtils.cleanOutputDir(destinationDir); if (getDataBindingLayoutInfoOutFolder().isPresent()) { FileUtils.deleteDirectoryContents(getDataBindingLayoutInfoOutFolder().get().getAsFile()); }
/** * Computes resource sets for merging, if [precompileDependenciesResources] flag is enabled we * filter out the non-values resources as it's precompiled and is consumed directly in the * linking step. * 计算用于合并的资源集,如果启用[PrecompiledDependenciesResources]标志, * 我们将在预编译时过滤掉非值资源,并在链接步骤中直接使用。 */ @JvmOverloads fun compute(precompileDependenciesResources: Boolean = false): List<ResourceSet> { ...... val resourceSetList = ArrayList<ResourceSet>(size) // 这里要注意,过滤的是libraries库的资源。 addLibraryResources(libraries, resourceSetList, precompileDependenciesResources) ...... return resourceSetList }
private fun addLibraryResources( libraries: ArtifactCollection?, resourceSetList: MutableList<ResourceSet>, resourceArePrecompiled: Boolean ) { // add at the beginning since the libraries are less important than the folder based // resource sets. // get the dependencies first libraries?.let { val libArtifacts = it.artifacts // the order of the artifact is descending order, so we need to reverse it. for (artifact in libArtifacts) { val resourceSet = ResourceSet() ....... resourceSet.isFromDependency = true //我们在loadFromFiles处理resourceSet时,会遍历这个set resourceSet.addSource(artifact.file)
if (resourceArePrecompiled) { // For values resources we impose stricter rules different from aapt so they need to go // through the merging step. // 设置了setAllowedFolderPrefix(FD_RES_VALUES) resourceSet.setAllowedFolderPrefix(FD_RES_VALUES) } // add to 0 always, since we need to reverse the order. resourceSetList.add(0, resourceSet) } } }