Android gradle打包涉及task源碼解析(四)

文章序號

此篇文章將分析如下11個(gè)task爵嗅。

:app:splitsDiscoveryTaskDebug UP-TO-DATE
:app:processDebugResources
:app:generateDebugSources
:app:javaPreCompileDebug
:app:compileDebugJavaWithJavac
:app:compileDebugNdk NO-SOURCE
:app:compileDebugSources
:app:mergeDebugShaders
:app:compileDebugShaders
:app:generateDebugAssets
:app:mergeDebugAssets

splitsDiscoveryTaskDebug

執(zhí)行命令:./gradlew splitsDiscoveryTaskDebug

  • inputs&outputs
output file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/build/intermediates/splits-support/debug/split-list.gson

split-list.json 文件內(nèi)容如下:

[{
    "splitType": "DENSITY",
    "values": ["mdpi", "hdpi", "xhdpi"]
}, {
    "splitType": "LANGUAGE",
    "values": []
}, {
    "splitType": "ABI",
    "values": []
}, {
    "splitType": "ResConfigs",
    "values": []
}]

通過輸出json文件授药,基本可以才到splitsDiscoveryTaskDebug任務(wù)就是把項(xiàng)目有關(guān)的split都再次聚合生成了一個(gè)json文件。

  • 源碼

https://android.googlesource.com/platform/tools/base/+/gradle_3.0.0/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/SplitsDiscovery.java

  • 主要代碼邏輯

SplitsDiscovery.java 中的taskAction()方法。

    void taskAction() throws IOException {
        Set<File> mergedResourcesFolderFiles =
                mergedResourcesFolders != null ? mergedResourcesFolders.getFiles() : null;
        Collection<String> resConfigs = resourceConfigs;
        if (resConfigAuto) {
            resConfigs = discoverListOfResourceConfigsNotDensities();
        }
        SplitList.save(
                getPersistedList(),
                getFilters(mergedResourcesFolderFiles, DiscoverableFilterType.DENSITY),
                getFilters(mergedResourcesFolderFiles, DiscoverableFilterType.LANGUAGE),
                // no need to pass the source folders, we don't support Auto for ABI splits so far.
                getFilters(ImmutableList.of(), DiscoverableFilterType.ABI),
                resConfigs);
    }

直接看最后一句代碼就行了赐写,實(shí)現(xiàn)對DENSITY、LANGUAGE、ABI和resConfig配置的存儲(chǔ),即生成split-list.json文件捶码。

processDebugResources

執(zhí)行命令:./gradlew processDebugResources

  • inputs&outputs
input file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/build/intermediates/res/merged/debug
input file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/build/intermediates/manifests/full/debug
input file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/build/intermediates/splits-support/debug/split-list.gson
input file:/Users/zhengchao/.gradle/caches/transforms-1/files-1.1/a7cc521b4567369eba0ddb355f44a660/21d0ae01f3b4e29e6c802c00ffced930/package-aware-r.txt
input file:/Users/zhengchao/.gradle/caches/transforms-1/files-1.1/604f3f8b356720eda7bfab425c06a06e/371c636e802b54ae62f24ca3b8983aa4/package-aware-r.txt
input file:/Users/zhengchao/.gradle/caches/transforms-1/files-1.1/91acff2d03a16c67323da40e829163bd/fa97ea8e56458564023fc312949aac2e/package-aware-r.txt
input file:/Users/zhengchao/.gradle/caches/transforms-1/files-1.1/a4e729d586b413dec60d9faca70e7901/6338b6741f916337f8e90073f389bd2a/package-aware-r.txt
input file:/Users/zhengchao/.gradle/caches/transforms-1/files-1.1/0f8bcb3b30938c09cb5d45a38670a61a/327ff0b63de18940597e89288c74ad4b/package-aware-r.txt
input file:/Users/zhengchao/.gradle/caches/transforms-1/files-1.1/500188dffd88c5be8587eb6372bbf06d/44df2544155d6c7f2d9d860dc81b2987/package-aware-r.txt
input file:/Users/zhengchao/.gradle/caches/transforms-1/files-1.1/7fd9e164f67367b6857914be5e5a3aa7/7bc85529df0e5dfea9848a731eb5e623/package-aware-r.txt
input file:/Users/zhengchao/.gradle/caches/transforms-1/files-1.1/a5a5c709ac0291e9c3bdb096074909c7/953f5d6737013091f79cdf83a4e500c1/package-aware-r.txt
input file:/Users/zhengchao/.gradle/caches/transforms-1/files-1.1/6b0cfb67d4e7be9680a05836fc807a20/e2734ac50593541599802c3547ef2197/package-aware-r.txt
input file:/Users/zhengchao/.gradle/caches/transforms-1/files-1.1/9806df9e60c4aacc7f9f357a91ad2e92/f56c8e1c2d1e598093ccd977e250c742/package-aware-r.txt
input file:/Users/zhengchao/.gradle/caches/transforms-1/files-1.1/8ce7d74e9940aacec93d142f00568824/b6e216a8726556e88e69ce4b671bce42/package-aware-r.txt
---------------------------------------------------
output file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/build/intermediates/incremental/processDebugResources
output file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/build/intermediates/res/debug
output file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/build/generated/source/r/debug
output file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
output file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/build/intermediates/symbols/debug/R.txt

首先看下輸入目錄類型:

1、mergeDebugResources 任務(wù)的輸出目錄或链,intermediates/res/merged/debug該文件夾下是經(jīng)過aapt處理過的flat文件惫恼;

2、processDebugManifest 任務(wù)的輸出目錄澳盐,intermediates/manifests/full/debug該文件夾下是生成的manifest.xml文件祈纯;

3、splitsDiscoveryTaskDebug任務(wù)輸出的目錄叼耙;

4腕窥、各個(gè)依賴包的cache目錄中的package-aware-r.txt文件,該文件里面的內(nèi)容是相應(yīng)包的資源的映射旬蟋,如下:

android.support.v7.appcompat
int anim abc_fade_in 0x7f010001
int anim abc_fade_out 0x7f010002
int anim abc_grow_fade_in_from_bottom 0x7f010003
int anim abc_popup_enter 0x7f010004

第一行是包名油昂,后面是資源的映射關(guān)系革娄。

再來看下輸出目錄:

1倾贰、/intermediates/incremental/processDebugResources這個(gè)先忽略,后面再說拦惋;

2匆浙、/intermediates/res/debug該目錄下主要是resources.ap_文件,該文件實(shí)際就是資源通過aapt后生成的資源壓縮文件(解壓后會(huì)發(fā)現(xiàn)里面就是:AndroidManifest.xml厕妖、res文件首尼,以及resoures.arsc);

3、/generated/source/r/debug該目錄也就是R.java文件。

4软能、symbol-table-with-package/debug/package-aware-r.txt生成項(xiàng)目的R文件的映射關(guān)系迎捺;

5、intermediates/symbols/debug/R.txt生成R文件的txt文件查排。

  • 源碼

https://android.googlesource.com/platform/tools/base/+/gradle_3.0.0/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/ProcessAndroidResources.java

  • 主要代碼邏輯

ProcessAndroidResources.java 中的doFullTaskAction()方法凳枝。

@Override
    protected void doFullTaskAction() throws IOException, ExecutionException {
        ...
        try (Aapt aapt = bypassAapt ? null : makeAapt()) {

            // do a first pass at the list so we generate the code synchronously since it's required
            // by the full splits asynchronous processing below.
            List<ApkData> apkDataList = new ArrayList<>(splitsToGenerate);
            for (ApkData apkData : splitsToGenerate) {
                if (apkData.requiresAapt()) {
                    boolean codeGen =
                            (apkData.getType() == OutputFile.OutputType.MAIN
                                    || apkData.getFilter(OutputFile.FilterType.DENSITY) == null);
                    if (codeGen) {
                        apkDataList.remove(apkData);
                        // 調(diào)用invokeAaptForSplit();
                        invokeAaptForSplit(
                                manifestsOutputs,
                                libraryInfoList,
                                packageIdFileSet,
                                splitList,
                                featureResourcePackages,
                                apkData,
                                codeGen,
                                aapt);
                        break;
                    }
                }
            }

            // now all remaining splits will be generated asynchronously.
            for (ApkData apkData : apkDataList) {
                if (apkData.requiresAapt()) {
                    executor.execute(
                            () -> {
                                invokeAaptForSplit(
                                        manifestsOutputs,
                                        libraryInfoList,
                                        packageIdFileSet,
                                        splitList,
                                        featureResourcePackages,
                                        apkData,
                                        false,
                                        aapt);
                                return null;
                            });
                }
        ...
        // and save the metadata file.
        outputScope.save(
                ImmutableList.of(
                        VariantScope.TaskOutputType.DENSITY_OR_LANGUAGE_SPLIT_PROCESSED_RES,
                        VariantScope.TaskOutputType.PROCESSED_RES),
                resPackageOutputFolder);
    }

核心代碼調(diào)用invokeAaptForSplit()方法跋核。

void invokeAaptForSplit(
            Collection<BuildOutput> manifestsOutputs,
            @NonNull Set<File> dependencySymbolTableFiles,
            @Nullable Set<File> packageIdFileSet,
            @NonNull SplitList splitList,
            @NonNull Set<File> featureResourcePackages,
            ApkData apkData,
            boolean generateCode,
            @Nullable Aapt aapt)
            throws IOException {
        // *.ap_ 文件
        File resOutBaseNameFile =
                new File(
                        resPackageOutputFolder,
                        FN_RES_BASE
                                + RES_QUALIFIER_SEP
                                + apkData.getFullName()
                                + SdkConstants.DOT_RES);
        ...
        // manifest file
        File manifestFile = manifestOutput.getOutputFile();

        ...
        // 生產(chǎn)相應(yīng)的文件
        getBuilder().processResources(aapt, config);

    }

processDebugResources這個(gè)任務(wù)的代碼有些長岖瑰,但是主要邏輯很清晰,就是根據(jù)我們前面分析的輸入文件砂代,生成我們的resoure(*.ap_)文件蹋订,已經(jīng)相應(yīng)的R文件。

generateDebugSources

忽略

javaPreCompileDebug

  • 準(zhǔn)備

引入'butterknife':

命令行輸入:./gradlew javaPreCompileDebug

  • inputs&outputs
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife-compiler/9.0.0-rc2/dcb492fc3d86c39cdc8d00ccacc259bac7a00634/butterknife-compiler-9.0.0-rc2.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.squareup/javapoet/1.10.0/712c178d35185d8261295913c9f2a7d6867a6007/javapoet-1.10.0.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife-annotations/9.0.0-rc2/b2542071aa4b048d2ac0bf6b6e430be5a1755c3f/butterknife-annotations-9.0.0-rc2.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.google.auto/auto-common/0.10/c8f153ebe04a17183480ab4016098055fb474364/auto-common-0.10.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/24.0-jre/41ac1e74d6b4e1ea1f027139cffeb536c732a81/guava-24.0-jre.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.android.support/support-annotations/28.0.0/ed73f5337a002d1fd24339d5fb08c2c9d9ca60d8/support-annotations-28.0.0.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/1.3.9/40719ea6961c0cb6afaeb6a921eaa1f6afd4cfdf/jsr305-1.3.9.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/org.checkerframework/checker-compat-qual/2.0.0/fc89b03860d11d6213d0154a62bcd1c2f69b9efa/checker-compat-qual-2.0.0.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.google.errorprone/error_prone_annotations/2.1.3/39b109f2cd352b2d71b52a3b5a1a9850e1dc304b/error_prone_annotations-2.1.3.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.google.j2objc/j2objc-annotations/1.1/976d8d30bebc251db406f2bdb3eb01962b5685b3/j2objc-annotations-1.1.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/org.codehaus.mojo/animal-sniffer-annotations/1.14/775b7e22fb10026eed3f86e8dc556dfafe35f2d5/animal-sniffer-annotations-1.14.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/appcompat-v7-26.1.0.aar/6b443e96f1af9aa241aaa70576c67a57/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/constraint-layout-1.1.3.aar/f44da5c361a1f52801511229596f72e7/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/butterknife-9.0.0-rc2.aar/8621d7bdb16992184dcd908b05a88089/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/animated-vector-drawable-26.1.0.aar/9c804d63d6f065a8f9945f9ad94fee0e/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-vector-drawable-26.1.0.aar/4e56cc34abf77378e2b8d16ee237c82d/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-v4-26.1.0.aar/3bf8586900bd31e222ef8b68bfd6e744/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-media-compat-26.1.0.aar/267524a16ca7128dd9cef3c19f394439/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-fragment-26.1.0.aar/77cf518e9868987a283f04cec221fefa/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-core-utils-26.1.0.aar/8634ab1afa6a5a1a947a7bd163aba14f/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-core-ui-26.1.0.aar/8902e2a864b44d47c26fbc80fdafe175/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/butterknife-runtime-9.0.0-rc2.aar/52a0e29c9a97888250a7dcafb4b772d2/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-compat-28.0.0.aar/0f01dd979a4412cc88bd76adabeaa35f/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/versionedparcelable-28.0.0.aar/c52cdc9f13c25171d1f8c643b784dff9/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.android.support/collections/28.0.0/c1bcdade4d3cc2836130424a3f3e4182c666a745/collections-28.0.0.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/runtime-1.1.1.aar/37b34991d517509bf0bc71acfb777556/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/android.arch.lifecycle/common/1.1.1/207a6efae6a3555e326de41f76bdadd9a239cbce/common-1.1.1.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/android.arch.core/common/1.1.1/e55b70d1f5620db124b3e85a7f4bdc7bd48d9f95/common-1.1.1.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.android.support.constraint/constraint-layout-solver/1.1.3/bde0667d7414c16ed62d3cfe993cff7f9d732373/constraint-layout-solver-1.1.3.jar
---------------------------------------------------
output file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/intermediates/javaPrecompile/debug/annotationProcessors.json

查看input file刻伊,會(huì)發(fā)現(xiàn)所有的輸入均為項(xiàng)目直接或者間接引用到的庫的jar包露戒。

output file 是一個(gè)annotationProcessors.json文件。文件如下:

["butterknife-compiler.jar (com.jakewharton:butterknife-compiler:9.0.0-rc2)"]

通過輸入輸出我們大致的可以分析出來'javaPreCompileDebug'任務(wù)就是生成使用了AnnotationProcessors依賴的json映射關(guān)系捶箱。

  • 源碼

https://android.googlesource.com/platform/tools/base/+/gradle_3.0.0/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/JavaPreCompileTask.java

  • 主要代碼邏輯
    @TaskAction
    public void preCompile() throws IOException {
        ...
        
        // Get all the annotation processors for metrics collection.
        Set<String> classNames = Sets.newHashSet();
        // 1玫锋、通過includeCompileClasspath 配置的annotation processors;
        // Add the annotation processors on classpath only when includeCompileClasspath is true.
        if (Boolean.TRUE.equals(annotationProcessorOptions.getIncludeCompileClasspath())) {
            if (compileProcessors == null) {
                compileProcessors = collectAnnotationProcessors(compileClasspaths);
            }
            classNames.addAll(convertArtifactsToNames(compileProcessors));
        }
        // 2讼呢、通過configuration 配置的 annotation processor撩鹿。
        // Add all annotation processors on the annotation processor configuration.
        classNames.addAll(
                convertArtifactsToNames(
                        collectAnnotationProcessors(annotationProcessorConfiguration)));

        // 3、通過annotationProcessorOptions 配置的annotation processor悦屏。
        // Add the explicitly declared processors.
        // For metrics purposes, we don't care how they include the processor in their build.
        classNames.addAll(annotationProcessorOptions.getClassNames());

        // 4节沦、使用dataBinding框架。
        // Add a generic reference to data binding, if present.
        if (dataBindingEnabled) {
            classNames.add(DATA_BINDING_SPEC);
        }

        FileUtils.deleteIfExists(processorListFile);
        Gson gson = new GsonBuilder().create();
        try (FileWriter writer = new FileWriter(processorListFile)) {
            gson.toJson(classNames, writer);
        }
    }

通過代碼可以發(fā)現(xiàn)础爬,javaPreCompileDebug任務(wù)就是將4?類型的annotationProcessor添加到 className中甫贯,最后將起保存為json文件。

compileDebugJavaWithJavac

執(zhí)行命令:./gradlew compileDebugJavaWithJavac

  • inputs&outputs
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/appcompat-v7-26.1.0.aar/6b443e96f1af9aa241aaa70576c67a57/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/constraint-layout-1.1.3.aar/f44da5c361a1f52801511229596f72e7/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/butterknife-8.5.1.aar/9d5de52440cb778daab09db33955642f/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/animated-vector-drawable-26.1.0.aar/9c804d63d6f065a8f9945f9ad94fee0e/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-vector-drawable-26.1.0.aar/4e56cc34abf77378e2b8d16ee237c82d/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife-annotations/8.5.1/bb67dad90bab7cd77a8f7f1b8442b47e3a2326bc/butterknife-annotations-8.5.1.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-v4-26.1.0.aar/3bf8586900bd31e222ef8b68bfd6e744/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-media-compat-26.1.0.aar/267524a16ca7128dd9cef3c19f394439/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-fragment-26.1.0.aar/77cf518e9868987a283f04cec221fefa/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-core-utils-26.1.0.aar/8634ab1afa6a5a1a947a7bd163aba14f/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-core-ui-26.1.0.aar/8902e2a864b44d47c26fbc80fdafe175/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/support-compat-26.1.0.aar/3e4c87483eacfb4c962d7380a59a114d/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.android.support/support-annotations/26.1.0/814258103cf26a15fcc26ecce35f5b7d24b73f8/support-annotations-26.1.0.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/com.android.support.constraint/constraint-layout-solver/1.1.3/bde0667d7414c16ed62d3cfe993cff7f9d732373/constraint-layout-solver-1.1.3.jar
input file:/Users/chao.zheng/.gradle/caches/transforms-1/files-1.1/runtime-1.0.0.aar/ed085e7b9476f7a9fef4ffbb323166ba/jars/classes.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/android.arch.lifecycle/common/1.0.0/e414a4cb28434e25c4f6aa71426eb20cf4874ae9/common-1.0.0.jar
input file:/Users/chao.zheng/.gradle/caches/modules-2/files-2.1/android.arch.core/common/1.0.0/a2d487452376193fc8c103dd2b9bd5f2b1b44563/common-1.0.0.jar
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/intermediates/javaPrecompile/debug/annotationProcessors.json
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/src/main/java/org/test/task/MainActivity.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/src/main/java/org/test/task/Test.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/org/test/task11/R2.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/org/test/task11/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/android/support/coreutils/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/android/support/v7/appcompat/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/android/support/compat/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/android/support/coreui/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/android/support/v4/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/android/support/fragment/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/android/support/graphics/drawable/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/android/support/graphics/drawable/animated/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/android/support/mediacompat/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/android/support/constraint/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/android/arch/lifecycle/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/r/debug/butterknife/R.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/buildConfig/debug/org/test/task11/BuildConfig.java
input file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/aidl/debug/org/test/task/IMyAidlInterface.java
---------------------------------------------------
output file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/generated/source/apt/debug
output file:/Users/chao.zheng/sunday/OpenSpace/TasksPro/app/build/intermediates/classes/debug

輸入文件類型:

1看蚜、依賴工程的jar文件叫搁;

2、javaPreCompileDebug 任務(wù)的輸出json文件(javaPrecompile/debug/annotationProcessors.json);

3供炎、項(xiàng)目的java文件(java/org/test/task/MainActivity.java)渴逻;

4、項(xiàng)目生成的R文件(source/r/debug/butterknife/R.java)音诫、BuildConfig文件(buildConfig/debug/org/test/task11/BuildConfig.java)惨奕、Aidl文件(aidl/debug/org/test/task/IMyAidlInterface.java);

輸出文件類型:

1、annotationProcessor 生成的java文件(generated/source/apt/debug)

2竭钝、生成的classes文件(intermediates/classes/debug)

通過這個(gè)輸入和輸出基本能分析出來compileDebugJavaWithJavac任務(wù)是做兩件事:1梨撞、通過annotation processor生成相應(yīng)的java文件雹洗;2、將項(xiàng)目的所有java文件(包括R卧波、BuildConfig时肿、Aidl等)通過javac編譯成class文件。

  • 源碼

https://android.googlesource.com/platform/tools/base/+/gradle_3.0.0/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/factory/AndroidJavaCompile.java

compile的核心邏輯實(shí)際上不在AndroidJavaCompile這個(gè)類里面港粱,該類繼承了JavaCompile類嗜侮,核心編譯邏輯實(shí)際在com.sun.tools.javac.main.Main.java類里面的compile()方法。通過包名也能看出該類就是javac里面的核心類啥容,具體代碼邏輯不在分析锈颗,有興趣的自行查看。

compileDebugNdk

mergeDebugShaders

compileDebugShaders

generateDebugAssets

以上幾個(gè)task跟ndk開發(fā)相關(guān)咪惠,在此不再分析击吱。

mergeDebugAssets

執(zhí)行命令:./gradlew mergeDebugAssets

  • inputs&outputs
input file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/build/generated/assets/shaders/debug
input file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/src/main/assets
input file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/src/debug/assets
---------------------------------------------------
output file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/build/intermediates/incremental/mergeDebugAssets
output file:/Users/zhengchao/Documents/AndroidSpace/OpenSpace/TasksPro/app/build/intermediates/assets/debug

根據(jù)輸入輸出大致可以分析出將生成的assets和項(xiàng)目的assets里面的文件合并到intermediates/assets/debug目錄下。

  • 源碼

https://android.googlesource.com/platform/tools/base/+/gradle_3.0.0/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/MergeSourceSetFolders.java

  • 主要代碼邏輯

MergeSourceSetFolders中的doFullTaskAction()方法遥昧。

protected void doFullTaskAction() throws IOException {
        // this is full run, clean the previous output
        File destinationDir = getOutputDir();
        FileUtils.cleanOutputDir(destinationDir);
        
        // 1覆醇、assets 集合
        List<AssetSet> assetSets = computeAssetSetList();

        // create a new merger and populate it with the sets.
        AssetMerger merger = new AssetMerger();

        try {
            // 2、遍歷assets集合炭臭,添加AssetMerger中
            for (AssetSet assetSet : assetSets) {
                // set needs to be loaded.
                assetSet.loadFromFiles(getILogger());
                merger.addDataSet(assetSet);
            }

            // get the merged set and write it down.
            MergedAssetWriter writer = new MergedAssetWriter(destinationDir, workerExecutor);

            merger.mergeData(writer, false /*doCleanUp*/);
            // 3永脓、寫到指定目錄
            // No exception? Write the known state.
            merger.writeBlobTo(getIncrementalFolder(), writer, false);
        } catch (MergingException e) {
            getLogger().error("Could not merge source set folders: ", e);
            merger.cleanBlob(getIncrementalFolder());
            throw new ResourceException(e.getMessage(), e);
        }
    }

通過代碼注釋說明,可以看出mergeDebugAssets和我們前面分析的是一致的鞋仍。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末常摧,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子威创,更是在濱河造成了極大的恐慌落午,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,378評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肚豺,死亡現(xiàn)場離奇詭異溃斋,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)吸申,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,356評論 2 382
  • 文/潘曉璐 我一進(jìn)店門梗劫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人截碴,你說我怎么就攤上這事梳侨。” “怎么了隐岛?”我有些...
    開封第一講書人閱讀 152,702評論 0 342
  • 文/不壞的土叔 我叫張陵猫妙,是天一觀的道長瓷翻。 經(jīng)常有香客問我聚凹,道長割坠,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,259評論 1 279
  • 正文 為了忘掉前任妒牙,我火速辦了婚禮彼哼,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘湘今。我一直安慰自己敢朱,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,263評論 5 371
  • 文/花漫 我一把揭開白布摩瞎。 她就那樣靜靜地躺著拴签,像睡著了一般。 火紅的嫁衣襯著肌膚如雪旗们。 梳的紋絲不亂的頭發(fā)上蚓哩,一...
    開封第一講書人閱讀 49,036評論 1 285
  • 那天,我揣著相機(jī)與錄音上渴,去河邊找鬼岸梨。 笑死,一個(gè)胖子當(dāng)著我的面吹牛稠氮,可吹牛的內(nèi)容都是我干的曹阔。 我是一名探鬼主播,決...
    沈念sama閱讀 38,349評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼隔披,長吁一口氣:“原來是場噩夢啊……” “哼赃份!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起奢米,我...
    開封第一講書人閱讀 36,979評論 0 259
  • 序言:老撾萬榮一對情侶失蹤芥炭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后恃慧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體园蝠,經(jīng)...
    沈念sama閱讀 43,469評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,938評論 2 323
  • 正文 我和宋清朗相戀三年痢士,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了彪薛。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,059評論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡怠蹂,死狀恐怖善延,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情城侧,我是刑警寧澤易遣,帶...
    沈念sama閱讀 33,703評論 4 323
  • 正文 年R本政府宣布,位于F島的核電站嫌佑,受9級特大地震影響豆茫,放射性物質(zhì)發(fā)生泄漏侨歉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,257評論 3 307
  • 文/蒙蒙 一揩魂、第九天 我趴在偏房一處隱蔽的房頂上張望幽邓。 院中可真熱鬧,春花似錦火脉、人聲如沸牵舵。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,262評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽畸颅。三九已至,卻和暖如春方援,著一層夾襖步出監(jiān)牢的瞬間重斑,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評論 1 262
  • 我被黑心中介騙來泰國打工肯骇, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留窥浪,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,501評論 2 354
  • 正文 我出身青樓笛丙,卻偏偏與公主長得像漾脂,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子胚鸯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,792評論 2 345

推薦閱讀更多精彩內(nèi)容