Android构建流程——篇四
文章目錄
- Task9 generateDebugResValues
- 1. input/ouput
- 2. 核心類(GenerateResValues)
- Task10 generateDebugResources
- Task11 mergeDebugResources
- 1. input/ouput
- 2. 核心類(MergeResources)
- 3. addDataSet
- 4. DataMerger.mergeData
- 4.1 MergeResourceWriter.start
- 4.2 MergeResourceWriter.addItem
- 4.3 MergeResourceWriter.end
- 4.3.1 postWriteAction
- 4.3.2 databinding、png crunching 、aapt2
- 4.3.3 mMergingLog.write
- 4.3.4 編譯后的資源是如何生成?
- Task12 createDebugCompatibleScreenManifests
- 1. input/ouput
- 2. 核心類(CompatibleScreensManifest)
- 參考
Task9 generateDebugResValues
1. input/ouput
taskName:generateDebugResValues ========================================================= output:/Users/dingbaosheng/work/mockuai/project/AndroidGradleTaskDemo/app/build/generated/res/resValues/debug簡單的說該任務就是生成一個generated.xml文件,里面包含了在build.gradle文件中自定義的資源屬性
2. 核心類(GenerateResValues)
public class GenerateResValues extends AndroidBuilderTask {@TaskActionvoid generate() throws IOException, ParserConfigurationException {File folder = getResOutputDir();//1. 讀取build.gradle中自定義的資源配置數據項List<Object> resolvedItems = getItems();//2. 如果未自定義,清空舊數據if (resolvedItems.isEmpty()) {FileUtils.cleanOutputDir(folder);} else {//如果自定義了,將自定義資源數據項交給資源值生成器對象ResValueGenerator generator = new ResValueGenerator(folder);generator.addItems(getItems());//3. 生成generated.xmlgenerator.generate();}} }代碼看流程清晰明了,關鍵代碼就是將自定義屬性(支持12種類型屬性哦)寫入到xml文件中,其實現細節集中在類ResValueGenerator.generater中;內部主要使用DOM API來實現,具體不再詳細說明,貼下實現代碼
public class ResValueGenerator {public static final String RES_VALUE_FILENAME_XML = "generated.xml";private static final List<ResourceType> RESOURCES_WITH_TAGS = ImmutableList.of(ResourceType.ARRAY,ResourceType.ATTR,ResourceType.BOOL,ResourceType.COLOR,ResourceType.DECLARE_STYLEABLE,ResourceType.DIMEN,ResourceType.FRACTION,ResourceType.INTEGER,ResourceType.PLURALS,ResourceType.STRING,ResourceType.STYLE);/*** Generates the resource files*/public void generate() throws IOException, ParserConfigurationException {File pkgFolder = getFolderPath();if (!pkgFolder.isDirectory()) {if (!pkgFolder.mkdirs()) {throw new RuntimeException("Failed to create " + pkgFolder.getAbsolutePath());}}File resFile = new File(pkgFolder, RES_VALUE_FILENAME_XML);DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();factory.setNamespaceAware(true);factory.setValidating(false);factory.setIgnoringComments(true);DocumentBuilder builder;builder = factory.newDocumentBuilder();Document document = builder.newDocument();//創建根節點resourcesNode rootNode = document.createElement(TAG_RESOURCES);document.appendChild(rootNode);//添加子節點,此處就是添加些注釋說明rootNode.appendChild(document.createTextNode("\n"));rootNode.appendChild(document.createComment("Automatically generated file. DO NOT MODIFY"));rootNode.appendChild(document.createTextNode("\n\n"));//遍歷自定義資源屬性for (Object item : mItems) {if (item instanceof ClassField) {ClassField field = (ClassField)item;ResourceType type = ResourceType.getEnum(field.getType());//判斷是否是通用的11種類型中一種boolean hasResourceTag = (type != null && RESOURCES_WITH_TAGS.contains(type));//如果不是通用的資源類型,則生成<item>標簽節點Node itemNode = document.createElement(hasResourceTag ? field.getType() : TAG_ITEM);Attr nameAttr = document.createAttribute(ATTR_NAME);nameAttr.setValue(field.getName());itemNode.getAttributes().setNamedItem(nameAttr);if (!hasResourceTag) {//如果是item標簽,需要設置其type、name屬性值//eg:<item type="id" name="button_ok" />Attr typeAttr = document.createAttribute(ATTR_TYPE);typeAttr.setValue(field.getType());itemNode.getAttributes().setNamedItem(typeAttr);}if (type == ResourceType.STRING) {//如果是string資源,添加traslatable屬性,使其設備統一使用該字段,和系統語言等無關Attr translatable = document.createAttribute(ATTR_TRANSLATABLE);translatable.setValue(VALUE_FALSE);itemNode.getAttributes().setNamedItem(translatable);}if (!field.getValue().isEmpty()) {itemNode.appendChild(document.createTextNode(field.getValue()));}rootNode.appendChild(itemNode);} else if (item instanceof String) {rootNode.appendChild(document.createTextNode("\n"));rootNode.appendChild(document.createComment((String) item));rootNode.appendChild(document.createTextNode("\n"));}}String content;try {content = XmlPrettyPrinter.prettyPrint(document, true);} catch (Throwable t) {content = XmlUtils.toXml(document);}//寫文件Files.write(content, resFile, Charsets.UTF_8);} }支持的自定義資源屬性類型
| 1 | array |
| 2 | attr |
| 3 | bool |
| 4 | color |
| 5 | declare-styleable |
| 6 | dimen |
| 7 | fraction |
| 8 | integer |
| 9 | plurals |
| 10 | string |
| 11 | style |
| 12 | item |
Task10 generateDebugResources
屬于空task直接忽略
Task11 mergeDebugResources
合并資源任務
1. input/ouput
taskName:mergeDebugResources input:/Users/apple/.gradle/caches/transforms-1/files-1.1/aapt2-3.2.0-4818971-osx.jar/c5355294e416159d9559f45d46460f7d/aapt2-3.2.0-4818971-osx input:/Users/apple/work/project/AndroidGradleTaskDemo/app/build/generated/res/resValues/debug input:/Users/apple/.gradle/caches/transforms-1/files-1.1/appcompat-v7-26.1.0.aar/2774ea4f1cf1e83a6ad8e8d3c8b463b6/res input:/Users/apple/.gradle/caches/transforms-1/files-1.1/constraint-layout-1.1.3.aar/f43c0ba95b6494825ed940fc4f04662b/res input:/Users/apple/.gradle/caches/transforms-1/files-1.1/support-media-compat-26.1.0.aar/53ab5ad72634f3497309a8788f3ca200/res input:/Users/apple/.gradle/caches/transforms-1/files-1.1/support-compat-26.1.0.aar/4ec3c1c46e5bad9ac3b91f45a2afec3e/res input:/Users/apple/work/project/AndroidGradleTaskDemo/app/build/generated/res/rs/debug input:/Users/apple/work/project/AndroidGradleTaskDemo/app/src/main/res input:/Users/apple/work/project/AndroidGradleTaskDemo/app/src/debug/res ========================================================= output:/Users/apple/work/project/AndroidGradleTaskDemo/app/build/intermediates/blame/res/debug output:/Users/apple/work/project/AndroidGradleTaskDemo/app/build/generated/res/pngs/debug output:/Users/apple/work/project/AndroidGradleTaskDemo/app/build/intermediates/incremental/mergeDebugResources output:/Users/apple/work/project/AndroidGradleTaskDemo/app/build/intermediates/res/merged/debug先來一張總流程圖吧,先把握主線;文字描述的不一定有圖清楚
xml -> drawable 、string 、color、anim、layout、value
layout、generated
png
先看輸入好了,可以很清楚看到輸入都是項目依賴的庫的res文件以及本身結構的res文件
| app/build/intermediates/blame/res/debug | 1. 日志文件 2. 資源文件和編譯后產物的映射表文件 | — | |
| app/build/generated/res/pngs/debug | 資源文件vector、png | xxx | |
| app/build/intermediates/incremental/mergeDebugResources | 1、merged.dir 存放合并資源文件的地方2、stripped.dir 3、compile-file-map.properties 資源和編譯產物映射表 4、merger.xml 合并文件 | MergedResourceWriter | |
| app/build/intermediates/res/merged/debug | flat文件 | AAPT2編譯的產物,也就是本上圖的重點產物 | MergedResourceWriter |
app/build/intermediates/incremental/mergeDebugResources
2. 核心類(MergeResources)
@CacheableTask public class MergeResources extends IncrementalTask {@Overrideprotected void doFullTaskAction() throws IOException, JAXBException {ResourcePreprocessor preprocessor = getPreprocessor();// this is full run, clean the previous outputsFile destinationDir = getOutputDir();FileUtils.cleanOutputDir(destinationDir);if (dataBindingLayoutInfoOutFolder != null) {FileUtils.deleteDirectoryContents(dataBindingLayoutInfoOutFolder);}List<ResourceSet> resourceSets = getConfiguredResourceSets(preprocessor);// create a new merger and populate it with the sets.ResourceMerger merger = new ResourceMerger(minSdk.get());MergingLog mergingLog = null;if (blameLogFolder != null) {FileUtils.cleanOutputDir(blameLogFolder);mergingLog = new MergingLog(blameLogFolder);}try (ResourceCompilationService resourceCompiler =getResourceProcessor(aaptGeneration,getBuilder(),aapt2FromMaven,workerExecutorFacade,crunchPng,variantScope,mergingLog,flags,processResources)) {for (ResourceSet resourceSet : resourceSets) {resourceSet.loadFromFiles(getILogger());//1. 添加資源merger.addDataSet(resourceSet);}MergedResourceWriter writer =new MergedResourceWriter(workerExecutorFacade,destinationDir,getPublicFile(),mergingLog,preprocessor,resourceCompiler,getIncrementalFolder(),dataBindingLayoutProcessor,mergedNotCompiledResourcesOutputDirectory,pseudoLocalesEnabled,getCrunchPng());//2. 執行資源合并操作merger.mergeData(writer, false /*doCleanUp*/);if (dataBindingLayoutProcessor != null) {dataBindingLayoutProcessor.end();}//3. 生成merger.xml文件// No exception? Write the known state.merger.writeBlobTo(getIncrementalFolder(), writer, false);} catch (MergingException e) {System.out.println(e.getMessage());merger.cleanBlob(getIncrementalFolder());throw new ResourceException(e.getMessage(), e);} finally {//4. 釋放資源cleanup();}} }上述代碼為全量動作代碼,增量類似只是輸入文件有所差異;我們分析下全量代碼流程
3. addDataSet
構建一個ResourceMerger對象,并將所有相關資源通過addDataSet方法添加到merger對象中
4. DataMerger.mergeData
這個動作是任務到核心所在,它主要包含一下幾個子動作
4.1 MergeResourceWriter.start
@Override public void start(@NonNull DocumentBuilderFactory factory) throws ConsumerException {super.start(factory);mValuesResMap = ArrayListMultimap.create();mQualifierWithDeletedValues = Sets.newHashSet();mFactory = factory; }合并資源預操作,為后續資源合并做準備
4.2 MergeResourceWriter.addItem
主要就是判斷如果文件是values.xml文件,直接記錄到內存中;否則開啟線程池,根據矢量圖生成設備適配的png資源,同時記錄到待編譯資源隊列中,以便后續aapt對其進行編譯;關鍵代碼如下
/*** Adds an item. The item may already be existing. Calling {@linkDataItem#isTouched()} will* indicate whether the item actually changed.* @param item the new item.*/ @Override public void addItem(@NonNull final ResourceMergerItem item) throws ConsumerException {final ResourceFile.FileType type = item.getSourceType();if (type == ResourceFile.FileType.XML_VALUES) {// this is a resource for the values files// just add the node to write to the map based on the qualifier.// We'll figure out later if the files needs to be written or (not)mValuesResMap.put(item.getQualifiers(), item);} else {checkState(item.getSourceFile() != null);// This is a single value file or a set of generated files. Only write it if the state// is TOUCHED.if (item.isTouched()) {File file = item.getFile();String folderName = getFolderName(item);// TODO : make this also a request and use multi-threading for generation.if (type == DataFile.FileType.GENERATED_FILES) {try {FileGenerationParameters workItem =new FileGenerationParameters(item, mPreprocessor);if (workItem.resourceItem.getSourceFile() != null) {//交給了MergeResourcesVectorDrawableRenderer.generateFile方法處理getExecutor().submit(FileGenerationWorkAction.class, workItem);}} catch (Exception e) {throw new ConsumerException(e, item.getSourceFile().getFile());}}// enlist a new crunching request.// 提交了一個編譯資源請求,后面AAPT工具會對該資源進行處理生成flat文件mCompileResourceRequests.add(new CompileResourceRequest(file, getRootFolder(), folderName));}} }//VectorDrawableRenderer.java public void generateFile(@NonNull File toBeGenerated, @NonNull File original)throws IOException {Files.createParentDirs(toBeGenerated);if (isXml(toBeGenerated)) {Files.copy(original, toBeGenerated);} else {mLogger.get().verbose("Generating PNG: [%s] from [%s]",toBeGenerated.getAbsolutePath(), original.getAbsolutePath());FolderConfiguration folderConfiguration = getFolderConfiguration(toBeGenerated);checkState(folderConfiguration.getDensityQualifier() != null);Density density = folderConfiguration.getDensityQualifier().getValue();assert density != null;float scaleFactor = density.getDpiValue() / (float) Density.MEDIUM.getDpiValue();if (scaleFactor <= 0) {scaleFactor = 1.0f;}//跟進矢量圖生成生成設備適配的png圖片VdPreview.TargetSize imageSize = VdPreview.TargetSize.createSizeFromScale(scaleFactor);String xmlContent = Files.asCharSource(original, StandardCharsets.UTF_8).read();BufferedImage image = VdPreview.getPreviewFromVectorXml(imageSize, xmlContent, null);checkState(image != null, "Generating the image failed.");ImageIO.write(image, "png", toBeGenerated);} }接下來我們看end做了什么
4.3 MergeResourceWriter.end
該步驟中主要做了以下幾點事情
4.3.1 postWriteAction
//MergeWriter.java @Override public void end() throws ConsumerException {try {postWriteAction();//等待所有資源文件被編譯成功后,流程才繼續往下走getExecutor().await();} catch (ConsumerException e) {throw e;} catch (Exception e) {throw new ConsumerException(e);} } @Override protected void postWriteAction() throws ConsumerException {/** Create a temporary directory where merged XML files are placed before being processed* by the resource compiler.*/File tmpDir = new File(mTemporaryDirectory, "merged.dir");try {FileUtils.cleanOutputDir(tmpDir);} catch (IOException e) {throw new ConsumerException(e);}//1. 遍歷valuesResMap中所有數據,按照<tmpdir/values-XXX/values-XXX.xml進行分類生成相關values.xml// now write the values files.for (String key : mValuesResMap.keySet()) {// the key is the qualifier.// check if we have to write the file due to deleted values.// also remove it from that list anyway (to detect empty qualifiers later).boolean mustWriteFile = mQualifierWithDeletedValues.remove(key);// get the list of items to writeList<ResourceMergerItem> items = mValuesResMap.get(key);// now check if we really have to write itif (!mustWriteFile) {for (ResourceMergerItem item : items) {if (item.isTouched()) {mustWriteFile = true;break;}}}if (mustWriteFile) {/** We will write the file to a temporary directory. If the folder name is "values",* we will write the XML file to "<tmpdir>/values/values.xml". If the folder name* is "values-XXX" we will write the XML file to* "<tmpdir/values-XXX/values-XXX.xml".** Then, we will issue a compile operation or copy the file if aapt does not require* compilation of this file.*/try {String folderName = key.isEmpty() ?ResourceFolderType.VALUES.getName() :ResourceFolderType.VALUES.getName() + RES_QUALIFIER_SEP + key;File valuesFolder = new File(tmpDir, folderName);// Name of the file is the same as the folder as AAPT gets confused with name// collision when not normalizing folders name.File outFile = new File(valuesFolder, folderName + DOT_XML);FileUtils.mkdirs(valuesFolder);DocumentBuilder builder = mFactory.newDocumentBuilder();Document document = builder.newDocument();final String publicTag = ResourceType.PUBLIC.getName();List<Node> publicNodes = null;Node rootNode = document.createElement(TAG_RESOURCES);document.appendChild(rootNode);Collections.sort(items);//2. 針對單個values文件,通過javax.xml API 生成xml文件for (ResourceMergerItem item : items) {Node nodeValue = item.getValue();if (nodeValue != null && publicTag.equals(nodeValue.getNodeName())) {if (publicNodes == null) {publicNodes = Lists.newArrayList();}publicNodes.add(nodeValue);continue;}// add a carriage return so that the nodes are not all on the same line.// also add an indent of 4 spaces.rootNode.appendChild(document.createTextNode("\n "));ResourceFile source = item.getSourceFile();Node adoptedNode = NodeUtils.adoptNode(document, nodeValue);if (source != null) {XmlUtils.attachSourceFile(adoptedNode, new SourceFile(source.getFile()));}rootNode.appendChild(adoptedNode);}// finish with a carriage returnrootNode.appendChild(document.createTextNode("\n"));final String content;Map<SourcePosition, SourceFilePosition> blame =mMergingLog == null ? null : Maps.newLinkedHashMap();if (blame != null) {content = XmlUtils.toXml(document, blame);} else {content = XmlUtils.toXml(document);}//3. 寫xml文件Files.write(content, outFile, Charsets.UTF_8);CompileResourceRequest request =new CompileResourceRequest(outFile,getRootFolder(),folderName,pseudoLocalesEnabled,crunchPng,blame != null ? blame : ImmutableMap.of());// If we are going to shrink resources, the resource shrinker needs to have the// final merged uncompiled file.if (notCompiledOutputDirectory != null) {File typeDir = new File(notCompiledOutputDirectory, folderName);FileUtils.mkdirs(typeDir);FileUtils.copyFileToDirectory(outFile, typeDir);}if (blame != null) {mMergingLog.logSource(new SourceFile(mResourceCompiler.compileOutputFor(request)), blame);mMergingLog.logSource(new SourceFile(outFile), blame);}//4. 生成編譯資源請求,交給aapt工具處理mResourceCompiler.submitCompile(request);if (publicNodes != null && mPublicFile != null) {// Generate public.txt:int size = publicNodes.size();StringBuilder sb = new StringBuilder(size * 80);for (Node node : publicNodes) {if (node.getNodeType() == Node.ELEMENT_NODE) {Element element = (Element) node;String name = element.getAttribute(ATTR_NAME);String type = element.getAttribute(ATTR_TYPE);if (!name.isEmpty() && !type.isEmpty()) {String flattenedName = name.replace('.', '_');sb.append(type).append(' ').append(flattenedName).append('\n');}}}File parentFile = mPublicFile.getParentFile();if (!parentFile.exists()) {boolean mkdirs = parentFile.mkdirs();if (!mkdirs) {throw new IOException("Could not create " + parentFile);}}String text = sb.toString();Files.write(text, mPublicFile, Charsets.UTF_8);}} catch (Exception e) {throw new ConsumerException(e);}}}// now remove empty values files.for (String key : mQualifierWithDeletedValues) {String folderName = key != null && !key.isEmpty() ?ResourceFolderType.VALUES.getName() + RES_QUALIFIER_SEP + key :ResourceFolderType.VALUES.getName();if (notCompiledOutputDirectory != null) {removeOutFile(FileUtils.join(notCompiledOutputDirectory, folderName, folderName + DOT_XML));}// Remove the intermediate (compiled) values file.removeOutFile(mResourceCompiler.compileOutputFor(new CompileResourceRequest(FileUtils.join(getRootFolder(), folderName, folderName + DOT_XML),getRootFolder(),folderName)));} }通過上面代碼簡要分析可以知道,postWriteAction方法主要是在merged.dir目錄生成了values相關資源文件(包含了其他語言適配資源文件),具體細節此處不再贅述
4.3.2 databinding、png crunching 、aapt2
@Override public void end() throws ConsumerException {...// now perform all the databinding, PNG crunching (AAPT1) and resources compilation (AAPT2).try {//1. 創建一個目錄File tmpDir = new File(mTemporaryDirectory, "stripped.dir");try {FileUtils.cleanOutputDir(tmpDir);} catch (IOException e) {throw new ConsumerException(e);}// 2. 對編譯資源請求進行消費處理while (!mCompileResourceRequests.isEmpty()) {CompileResourceRequest request = mCompileResourceRequests.poll();try {File fileToCompile = request.getInputFile();if (mMergingLog != null) {mMergingLog.logCopy(request.getInputFile(),mResourceCompiler.compileOutputFor(request));}//3. 此處對databing相關布局文件進行做處理,需要執行移除databinding相關表達式操作if (dataBindingExpressionRemover != null&& request.getInputDirectoryName().startsWith("layout")&& request.getInputFile().getName().endsWith(".xml")) {// Try to strip the layout. If stripping modified the file (there was data// binding in the layout), compile the stripped layout into merged resources// folder. Otherwise, compile into merged resources folder normally.File strippedLayoutFolder =new File(tmpDir, request.getInputDirectoryName());File strippedLayout =new File(strippedLayoutFolder, request.getInputFile().getName());//4. databinding移除表達式操作關鍵代碼boolean removedDataBinding =dataBindingExpressionRemover.processSingleFile(request.getInputFile(), strippedLayout);// 5. 做好記錄操作if (removedDataBinding) {// Remember in case AAPT compile or link fails.if (mMergingLog != null) {mMergingLog.logCopy(request.getInputFile(), strippedLayout);}fileToCompile = strippedLayout;}}// 備份無需編譯的資源文件,方便后續合并資源// Currently the resource shrinker and unit tests that use resources need// the final merged, but uncompiled file.if (notCompiledOutputDirectory != null) {File typeDir =new File(notCompiledOutputDirectory,request.getInputDirectoryName());FileUtils.mkdirs(typeDir);FileUtils.copyFileToDirectory(fileToCompile, typeDir);}//6. 交給aapt工具進行資源編譯mResourceCompiler.submitCompile(new CompileResourceRequest(fileToCompile,request.getOutputDirectory(),request.getInputDirectoryName(),pseudoLocalesEnabled,crunchPng,ImmutableMap.of(),request.getInputFile()));// 7. 將需要編譯的文件資源做好備份mCompiledFileMap.put(fileToCompile.getAbsolutePath(),mResourceCompiler.compileOutputFor(request).getAbsolutePath());} catch (ResourceCompilationException | IOException e) {throw MergingException.wrapException(e).withFile(request.getInputFile()).build();}}} catch (Exception e) {throw new ConsumerException(e);}...}可以看到上次主要做了三件事情
4.3.3 mMergingLog.write
if (mMergingLog != null) {try {mMergingLog.write();} catch (IOException e) {throw new ConsumerException(e);}mMergingLog = null; }/*** Persist the current state of the merging log.*/// MergingLog.javapublic void write() throws IOException {FileUtils.mkdirs(mOutputFolder);// This is intrinsically incremental, any shards that were touched were loaded, and so// will be saved. Empty map will result in the deletion of the file.for (Map.Entry<String, Map<SourceFile, Map<SourcePosition, SourceFilePosition>>> entry :mMergedFileMaps.asMap().entrySet()) {MergingLogPersistUtil.saveToMultiFileVersion2(mOutputFolder, entry.getKey(), entry.getValue());}for (Map.Entry<String, Map<SourceFile, SourceFile>> entry :mWholeFileMaps.asMap().entrySet()) {MergingLogPersistUtil.saveToSingleFile(mOutputFolder, entry.getKey(), entry.getValue());}}上述代碼只是在intermediate/blame/res/debug/single目錄生成一個debug.json文件,該文件記錄了編譯后的資源xxx.flat和原始文件的路徑
內容類似如下
4.3.4 編譯后的資源是如何生成?
這是該任務的一個重點,我們從上面代碼可以看到,編譯資源請求是被mResourceCompiler來處理的,追蹤代碼發現這個變量賦值是MergeResources.doFullTaskAction中,也就是任務剛開始執行是的代碼
@Override protected void doFullTaskAction() throws IOException, JAXBException {...try (ResourceCompilationService resourceCompiler =getResourceProcessor(aaptGeneration,getBuilder(),aapt2FromMaven,workerExecutorFacade,crunchPng,variantScope,mergingLog,flags,processResources)) {...} catch (MergingException e) {...} finally {cleanup();}}繼續跟蹤代碼
@NonNull private static ResourceCompilationService getResourceProcessor(@NonNull AaptGeneration aaptGeneration,@NonNull AndroidBuilder builder,@Nullable FileCollection aapt2FromMaven,@NonNull WorkerExecutorFacade workerExecutor,boolean crunchPng,@NonNull VariantScope scope,@Nullable MergingLog blameLog,ImmutableSet<Flag> flags,boolean processResources) {// If we received the flag for removing namespaces we need to use the namespace remover to// process the resources.if (flags.contains(Flag.REMOVE_RESOURCE_NAMESPACES)) {return NamespaceRemover.INSTANCE;}// If we're not removing namespaces and there's no need to compile the resources, return a// no-op resource processor.if (!processResources) {return CopyToOutputDirectoryResourceCompilationService.INSTANCE;}if (aaptGeneration == AaptGeneration.AAPT_V2_DAEMON_SHARED_POOL) {Aapt2ServiceKey aapt2ServiceKey =Aapt2DaemonManagerService.registerAaptService(aapt2FromMaven, builder.getBuildToolInfo(), builder.getLogger());return new WorkerExecutorResourceCompilationService(workerExecutor, aapt2ServiceKey);}// Finally, use AAPT or one of AAPT2 versions based on the project flags.return new QueueableResourceCompilationService(AaptGradleFactory.make(aaptGeneration,builder,createProcessOutputHandler(aaptGeneration, builder, blameLog),crunchPng,scope.getGlobalScope().getExtension().getAaptOptions().getCruncherProcesses())); }調試發現我們看到最終是WorkerExecutorResourceCompilationService這個類來處理資源編譯的
/** Resource compilation service built on top of a Aapt2Daemon and Gradle Worker Executors. */ class WorkerExecutorResourceCompilationService(private val workerExecutor: WorkerExecutorFacade,private val aapt2ServiceKey: Aapt2ServiceKey) : ResourceCompilationService {/** Temporary workaround for b/73804575 / https://github.com/gradle/gradle/issues/4502* Only submit a small number of worker actions */private val requests: MutableList<CompileResourceRequest> = ArrayList()override fun submitCompile(request: CompileResourceRequest) {// b/73804575requests.add(request)}override fun compileOutputFor(request: CompileResourceRequest): File {return File(request.outputDirectory,Aapt2RenamingConventions.compilationRename(request.inputFile))}override fun close() {if (requests.isEmpty()) {return}val buckets = minOf(requests.size, 8) // Max 8 buckets// 最多進行分8個組,并發處理,編譯扔給Aapt2CompileWithBlameRunnable處理for (bucket in 0 until buckets) {val bucketRequests = requests.filterIndexed { i, _ ->i.rem(buckets) == bucket}// b/73804575workerExecutor.submit(Aapt2CompileWithBlameRunnable::class.java,Aapt2CompileWithBlameRunnable.Params(aapt2ServiceKey, bucketRequests))}requests.clear()// No need for workerExecutor.await() here as resource compilation is the last part of the// merge task. This means the MergeResources task action can return, allowing other tasks// in the same subproject to run while resources are still being compiled.workerExecutor.close()} }可以看到submitCompile方法存儲了編譯資源的請求,而在close方法中可以看到對其進行分組處理消費編譯請求
編譯鏈路總結如下
經過層層調用最終Aapt2DaemonUtil.requestCompile方法被調用,這個類里面主要是構造一些aapt2識別的參數集合而已,方便aapt編譯
public class Aapt2DaemonUtil {public static final String DAEMON_MODE_COMMAND = "m";public static void requestCompile(@NonNull Writer writer, @NonNull CompileResourceRequest command) throws IOException {request(writer, "c", AaptV2CommandBuilder.makeCompileCommand(command));}private static void request(Writer writer, String command, Iterable<String> args)throws IOException {writer.write(command);writer.write('\n');for (String s : args) {writer.write(s);writer.write('\n');}// Finish the requestwriter.write('\n');writer.write('\n');writer.flush();} } // AaptV2CommandBuilder.kt fun makeCompileCommand(request: CompileResourceRequest): ImmutableList<String> {val parameters = ImmutableList.Builder<String>()if (request.isPseudoLocalize) {parameters.add("--pseudo-localize")}if (!request.isPngCrunching) {// Only pass --no-crunch for png files and not for 9-patch files as that breaks them.val lowerName = request.inputFile.path.toLowerCase(Locale.US)if (lowerName.endsWith(SdkConstants.DOT_PNG)&& !lowerName.endsWith(SdkConstants.DOT_9PNG)) {parameters.add("--no-crunch")}}if (request.partialRFile != null) {parameters.add("--output-text-symbols", request.partialRFile!!.absolutePath)}parameters.add("--legacy")parameters.add("-o", request.outputDirectory.absolutePath)parameters.add(request.inputFile.absolutePath)return parameters.build() }最終還是調用了build-tools包中aapt2腳步,生成的flat文件在build/intermediates/res/merged/debug中可以找到
關于AAPT2的詳細知識可以閱讀官方文檔,aapt工具路徑如下,剛興趣可以深入研究
至此mergeDebugResources任務已梳理完畢,該任務內部細節較多,基本本章篇幅都是在闡述該任務內部實現
Task12 createDebugCompatibleScreenManifests
該任務是生成一個指定屏幕密度可適配多屏幕的清單文件,該清單文件中只包含compatible-screens標簽而已,example如下
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="${packageName}"><uses-sdk android:minSdkVersion="14"/><compatible-screens><screen android:screenSize="small" android:screenDensity="hdpi" /><screen android:screenSize="normal" android:screenDensity="hdpi" /><screen android:screenSize="large" android:screenDensity="hdpi" /><screen android:screenSize="xlarge" android:screenDensity="hdpi" /></compatible-screens> </manifest>關于compatible-screens標簽的使用可以參見android split使用
Gradle 可以創建單獨的 APK,讓每個 APK 僅包含每種密度或 ABI 專用的代碼和資源。
如需針對多個 APK 配置 build,請將 splits 代碼塊添加到模塊級 build.gradle 文件。
在 splits 代碼塊內,提供一個 density 代碼塊,指定 Gradle 應如何按密度生成 APK;或提供一個 abi 代碼塊,指定 Gradle 應如何按 ABI 生成 APK。您可以同時提供密度和 ABI 代碼塊,構建系統將針對密度和 ABI 的每個組合創建一個 APK。
添加代碼至app.build.gradle中
android{...splits {// Configures multiple APKs based on screen density.density {// Configures multiple APKs based on screen density.enable true// Specifies a list of screen densities Gradle should not create multiple APKs for.exclude "ldpi", "xxhdpi", "xxxhdpi"// Specifies a list of compatible screen size settings for the manifest.compatibleScreens 'small', 'normal', 'large', 'xlarge'}} }執行
./gradlew clean assembleDebug1. input/ouput
taskName:createDebugCompatibleScreenManifests ========================================================= output:/Users/apple/work/project/AndroidGradleTaskDemo/app/build/intermediates/compatible_screen_manifest/debug/createDebugCompatibleScreenManifests/out可以看到在out目錄輸出了多個清單文件
可以看到out目錄輸出了所有適配的屏幕清單文件,而生成的清單代碼文件核心代碼如下
2. 核心類(CompatibleScreensManifest)
/*** Task to generate a manifest snippet that just contains a compatible-screens node with the given* density and the given list of screen sizes.*/ @CacheableTask open class CompatibleScreensManifest : AndroidVariantTask() {@get:Inputlateinit var screenSizes: Set<String>internal set@get:Inputval splits: List<ApkData>get() = outputScope.apkDatas@Input@Optionalinternal fun getMinSdkVersion(): String? {return minSdkVersion.get()}@get:OutputDirectorylateinit var outputFolder: Fileinternal set...@TaskAction@Throws(IOException::class)fun generateAll() {//任務入口類BuildElements(outputScope.apkDatas.mapNotNull { apkInfo ->//1. 遍歷splits,生成清單文件val generatedManifest = generate(apkInfo)if (generatedManifest != null)BuildOutput(COMPATIBLE_SCREEN_MANIFEST, apkInfo, generatedManifest)elsenull }.toList()).save(outputFolder)//生成output.json日志文件}//清單文件生成代碼fun generate(apkData: ApkData): File? {val densityFilter = apkData.getFilter(VariantOutput.FilterType.DENSITY)?: return null//1. 構建根節點val content = StringBuilder()content.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n").append("<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n").append(" package=\"\${packageName}\">\n").append("\n")if (minSdkVersion.get() != null) {content.append(" <uses-sdk android:minSdkVersion=\"").append(minSdkVersion.get()).append("\"/>\n")}content.append(" <compatible-screens>\n")// convert unsupported values to numbers.val density = convert(densityFilter.identifier, Density.XXHIGH, Density.XXXHIGH)//2. 針對指定的屏幕密度生成可適配多個屏幕大小的節點標簽<screen>for (size in screenSizes) {content.append(" <screen android:screenSize=\"").append(size).append("\" " + "android:screenDensity=\"").append(density).append("\" />\n")}content.append(" </compatible-screens>\n" + "</manifest>")val splitFolder = File(outputFolder, apkData.dirName)//3. 存儲目錄FileUtils.mkdirs(splitFolder)val manifestFile = File(splitFolder, SdkConstants.ANDROID_MANIFEST_XML)try {//4. 保存Files.asCharSink(manifestFile, Charsets.UTF_8).write(content.toString())} catch (e: IOException) {throw BuildException(e.message, e)}return manifestFile} }從generate方法中的VariantOutput.FilterType.DENSITY可以看到splits支持三種屬性DENSITY、ABI、LANGUAGE三種組合方式;輸出的結果見上圖。除了清單文件輸出還有output.json的日志輸出,其內容格式如下 (格式化后)
[{"outputType": {"type": "COMPATIBLE_SCREEN_MANIFEST"},"apkInfo": {"type": "FULL_SPLIT","splits": [{"filterType": "DENSITY","value": "hdpi"}],"versionCode": 1,"versionName": "1.0","enabled": true,"filterName": "hdpi","outputFile": "app-hdpi-debug.apk","fullName": "hdpiDebug","baseName": "hdpi-debug"},"path": "hdpi/AndroidManifest.xml","properties": {}},... ]output.json的代碼實現就在入口類的save方法中,跟進去看看
//BuildElements.kt @Throws(IOException::class) fun save(folder: File) : BuildElements {val persistedOutput = persist(folder.toPath())FileWriter(ExistingBuildElements.getMetadataFile(folder)).use { writer ->writer.append(persistedOutput)}return this } /*** Persists the passed output types and split output to a [String] using gson.** @param projectPath path to relativize output file paths against.* @return a json String.*/ fun persist(projectPath: Path): String {val gsonBuilder = GsonBuilder()gsonBuilder.registerTypeAdapter(ApkInfo::class.java, ExistingBuildElements.ApkInfoAdapter())gsonBuilder.registerTypeAdapter(InternalArtifactType::class.java, ExistingBuildElements.OutputTypeTypeAdapter())gsonBuilder.registerTypeAdapter(AnchorOutputType::class.java,ExistingBuildElements.OutputTypeTypeAdapter())val gson = gsonBuilder.create()// flatten and relativize the file paths to be persisted.return gson.toJson(elements.asSequence().map { buildOutput ->BuildOutput(buildOutput.type,buildOutput.apkInfo,projectPath.relativize(buildOutput.outputPath),buildOutput.properties) }.toList()) }代碼已經非常清晰了,內部其實使用Gson庫來實現序列化輸出json字符串數組
👇
Android構建流程——上篇
Android構建流程——下篇
參考
https://developer.android.com/studio/build/gradle-tips#share-custom-fields-and-resource-values-with-your-app-code
https://developer.android.com/guide/topics/resources/more-resources
http://tools.android.com/recent/non-translatablestrings
總結
以上是生活随笔為你收集整理的Android构建流程——篇四的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android构建流程——篇三
- 下一篇: Android-gradle插件调试