Android 探索之 Task 分析(六)

本文将会分析如下几个task

compileReleaseJavaWithJavac
compileReleaseNdk
compileReleaseSources
lintVitalRelease
mergeReleaseShaders
compileReleaseShaders

19.compileReleaseJavaWithJavac

1、通过annotation processor生成相应的java文件;

2、将项目的所有java文件(包括R、BuildConfig、Aidl等)通过javac编译成class文件。

源码分析

//com.android.build.gradle.tasks.factory.AndroidJavaCompile
//compile的核心逻辑实际上不在AndroidJavaCompile这个类里面,该类继承了JavaCompile类,核心编译逻辑实际在com.sun.tools.javac.main.Main.java类里面的compile()方法。通过包名也能看出该类就是javac里面的核心类,具体代码逻辑不在分析,有兴趣的自行查看。

输入输出

-----task begin-------->
project:     project ':app_driver' 
name:        compileReleaseJavaWithJavac 
group:       null 
description: null
conv:        [:]
inputs:
 /Users/dongkai/Code/XiWeiLogistics/ymm_app_driver_main_module/build/intermediates/intermediate-jars/release/classes.jar
  ...
  ...
  ...
 /Users/dongkai/.gradle/caches/transforms-1/files-1.1/mmkv-static-1.0.17.aar/0c39c42d42d3b884ed1b1cdbff217b58/jars/classes.jar
 /Users/dongkai/Android/sdk/platforms/android-29/android.jar
 /Users/dongkai/Android/sdk/build-tools/29.0.2/core-lambda-stubs.jar
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/intermediates/javaPrecompile/release/annotationProcessors.json
 /Users/dongkai/Code/XiWeiLogistics/app_driver/src/main/java/com/xiwei/logistics/newapp/YmmDriverApplication.java
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/generated/source/r/release/pl/droidsonroids/gif/R.java
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/generated/source/r/release/androidx/work/R.java
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/generated/source/r/release/in/srain/cube/views/ptr/R.java
 ...
 ...
 ...
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/generated/source/buildConfig/release/com/xiwei/logistics/newapp/BuildConfig.java
outputs:
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/generated/source/apt/release
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/intermediates/classes/release
<------task end -------

输入文件类型:

  • 依赖工程的jar文件;
  • javaPreCompileRelease 任务的输出json文件(javaPrecompile/release/annotationProcessors.json);
  • 项目的java文件(/com/xiwei/logistics/newapp/YmmDriverApplication.java);
  • 项目生成的R文件(source/r/release/butterknife/R.java)、BuildConfig文件(release/com/xiwei/logistics/newapp/BuildConfig.java)、Aidl文件;

输出文件类型:

  • annotationProcessor 生成的java文件(generated/source/apt/release)
  • 生成的classes文件(intermediates/classes/release)

通过这个输入和输出基本能分析出来compileReleaseJavaWithJavac任务是做两件事:1、通过annotation processor生成相应的java文件;2、将项目的所有java文件(包括R、BuildConfig、Aidl等)通过javac编译成class文件。

20.compileReleaseNdk

根据makeFile编译C代码

源码分析

输入输出

-----task begin-------->
project:     project ':app_driver' 
name:        compileReleaseNdk 
group:       null 
description: null
conv:        [:]
inputs:
outputs:
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/intermediates/ndk/release/Android.mk
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/intermediates/ndk/release/obj
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/intermediates/ndk/release/lib
<------task end -------

21.compileReleaseSources

锚点task,输入输出均为空

源码分析

输入输出

-----task begin-------->
project:     project ':app_driver' 
name:        compileReleaseSources 
group:       build 
description: null
conv:        [:]
inputs:
outputs:
<------task end -------

22.lintVitalRelease

进行至关重要的检查,检查有没有严重错误

源码分析

//com.android.build.gradle.tasks.LintPerVariantTask
//com.android.build.gradle.tasks.LintBaseTask
// inputs files

public VariantInputs(@NonNull VariantScope variantScope) {
            name = variantScope.getFullVariantName();
            allInputs = variantScope.getGlobalScope().getProject().files();

            allInputs.from(
                    localLintJarCollection = variantScope.getGlobalScope().getOutput(LINT_JAR));
            allInputs.from(
                    dependencyLintJarCollection =
                            variantScope.getArtifactFileCollection(RUNTIME_CLASSPATH, ALL, LINT));

            if (variantScope.hasOutput(MERGED_MANIFESTS)) {
                mergedManifest = variantScope.getOutput(MERGED_MANIFESTS);
            } else if (variantScope.hasOutput(LIBRARY_MANIFEST)) {
                mergedManifest = variantScope.getOutput(LIBRARY_MANIFEST);
            } else {
                throw new RuntimeException(
                        "VariantInputs initialized with no merged manifest on: "
                                + variantScope.getVariantConfiguration().getType());
            }
            allInputs.from(mergedManifest);

            if (variantScope.hasOutput(MANIFEST_MERGE_REPORT)) {
                allInputs.from(
                        mergedManifestReport = variantScope.getOutput(MANIFEST_MERGE_REPORT));
            } else {
                throw new RuntimeException(
                        "VariantInputs initialized with no merged manifest report on: "
                                + variantScope.getVariantConfiguration().getType());
            }

            // these inputs are only there to ensure that the lint task runs after these build
            // intermediates are built.
            allInputs.from(variantScope.getOutput(ALL_CLASSES));
        }


// 实际lint 
        FileCollection lintClassPath = getLintClassPath();
        if (lintClassPath != null) {
            new ReflectiveLintRunner().runLint(getProject().getGradle(),
                    descriptor, lintClassPath.getFiles());
        }

输入输出

-----task begin-------->
project:     project ':app_driver' 
name:        lintVitalRelease 
group:       verification 
description: Runs lint on just the fatal issues in the release build.
conv:        [:]
inputs:
 /Users/dongkai/.gradle/caches/modules-2/files-2.1/com.android.tools.lint/lint-gradle/26.1.4/f8969848ceda7fb7ca3ce07c3c5e46a35a824b05/lint-gradle-26.1.4.jar
 /Users/dongkai/.gradle/caches/modules-2/files-2.1/com.android.tools.build/builder/3.1.4/afbcd4b7002c61fe898b1b4c50ed9e62386125d8/builder-3.1.4.jar
 ...
 ...
 ...
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/intermediates/manifests/full/release
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/outputs/logs/manifest-merger-release-report.txt
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/intermediates/classes/release
outputs:
<------task end -------

输入文件有 lint.jar
manifest
manifest-merger-release-report.txt
all classes
all jar lint
仅检查,不会有输出

23.mergeReleaseShaders

//合并着色器,着色器是用来实现图像渲染的 pass了

源码分析

输入输出

-----task begin-------->
project:     project ':app_driver' 
name:        mergeReleaseShaders 
group:       null 
description: null
conv:        [:]
inputs:
 /Users/dongkai/Code/XiWeiLogistics/app_driver/src/release/shaders
 /Users/dongkai/Code/XiWeiLogistics/app_driver/src/main/shaders
outputs:
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/intermediates/incremental/mergeReleaseShaders
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/intermediates/shaders/release
<------task end -------

24.compileReleaseShaders

//编译着色器,着色器是用来实现图像渲染的 pass了

源码分析

输入输出

-----task begin-------->
project:     project ':app_driver' 
name:        compileReleaseShaders 
group:       null 
description: null
conv:        [:]
inputs:
outputs:
 /Users/dongkai/Code/XiWeiLogistics/app_driver/build/generated/assets/shaders/release
<------task end -------

这年头生活不易,人生不息,折腾不止!!!!

发表评论