Download the source code

Source in the directory/media/zen/material/android – studio – in the dev

$ mkdir studio-master-dev
$ cd studio-master-dev
$ repo init -u https://android.googlesource.com/platform/manifest -b studio-master-dev
$ repo sync -c -j4 -q
Copy the code

Compile Gradle Plugin

All projects are built together in a multi-module Gradle project setup. The root directory of the Gradle Plugin project is Tools /.

Execute the following command $. / gradlew: publishAndroidGradleLocal

Package version number in the tools/buildSrc/base/version. The properties defined, some synthetic baseVersion, such as com. Android. The tools; Some synthetic buildVersion, such as com. Android. View the build: gradle.

Com. Android. Tools. Build: gradle corresponding out/repo/com/android/tools/build/gradle directory, namely a 4.4.1 / gradle – 4.4.1. Jar, The code is in tools/base/build-system/gradle-core.

buildscript { repositories { maven { url '/media/zen/material/android-studio-dev/out/repo' } google() jcenter() } Dependencies {/ / classpath 'com. Android. View the build: gradle: 3.1.0' classpath 'com. Android. View the build: gradle: 4.4.1'}} allprojects { repositories { maven { url '/media/zen/material/android-studio-dev/out/repo' } google() jcenter() } }Copy the code

Task analysis

./src/main/java/com/android/build/gradle/tasks
./src/main/java/com/android/build/gradle/internal/tasks
Copy the code

Pay attention to the following two method calls:

fun computeTaskName(prefix: String, suffix: String): String
fun computeTaskName(prefix: String): String
Copy the code

These two methods define tools/base/build-system/gradle-core

src/main/java/com/android/build/api/component/impl/ComponentPropertiesImpl.kt
//or
src/main/java/com/android/build/gradle/internal/component/BaseCreationConfig.kt
src/main/java/com/android/build/gradle/internal/tasks/factory/TaskActions.kt
Copy the code

AppPlugin / LibraryPlugin BasePlugin.apply BasePlugin.basePluginApply com/android/build/gradle/internal/plugins/BasePlugin.createTasks / BasePlugin<,>.runAfterEvaluate TransformManager.createAndroidTasks TransformManager.createTasks

TransformManager.createPostCompilationTasks / LibraryTaskManager.doCreateTasksForVariant TransformManager.addTransform -> TaskConfigAction.configure

The Plugin statement

[Java Gradle plugins] (docs.gradle.org/current/use…).

tools/base/build-system/gradle-core/build.gradle

apply plugin: 'java-gradle-plugin'

gradlePlugin {
    testSourceSets sourceSets.apiTest
    plugins {
        androidApplication {
            id = 'com.android.application'
            implementationClass = 'com.android.build.gradle.AppPlugin'}}}... .group = 'com.android.tools.build'
archivesBaseName = 'gradle'
version = rootProject.ext.buildVersion
Copy the code
  1. tools/base/build-system/gradle-core/src/main/resources/META-INF/gradle-plugins/android-library.properties implementation-class=com.android.build.gradle.LibraryPlugin
  2. tools/base/build-system/gradle-core/src/main/resources/META-INF/gradle-plugins/com.android.library.properties implementation-class=com.android.build.gradle.LibraryPlugin
Plugin Class Properties
com.android.library com.android.build.gradle.LibraryPlugin android-library.properties
com.android.library com.android.build.gradle.internal.plugins.LibraryPlugin com.android.internal.library.properties
com.android.application com.android.build.gradle.internal.plugins.AppPlugin com.android.internal.application.properties
com.android.application com.android.build.gradle.AppPlugin com.android.application.properties

android.properties

Actually com. Android. Build. Gradle. LibraryPlugin and com. The android. Build. Gradle. AppPlugin doesn’t implement the specific function of the Plugin, Instead of entrusting the plugin respectively to com. Android. Internal. The library and com. The android. Internal. Application plugin, The com. Android. Internal. Library. The properties and com. Android. Internal. Application. The properties specify the plugin.

  1. The Class directory tools/base/build – the system/gradle – core/SRC/main/Java /
  2. Compile the properties in the out/build directory/base/build – the system/gradle – core/build/resources/main/meta-inf/gradle – plugins /

When using the Apply plugin on the Module: ‘com. Android. The application will through the above properties defined in the implementation – the value of the class find com. Android. Build. Gradle. AppPlugin, called after the void apply(Project project); Here, for an Android project, you’ll register MethodTimerTransform into a project, You can find apply plugin->plugin Properties ->apply()->transfor() in Tasks in the current Module.

com.android.ide.common.gradle.model.level2.IdeLibraryFactory

org.gradle.api.Plugin

  1. BasePlugin.apply
    1. BasePlugin.basePluginApply
    2. BasePlugin.configureExtension
      1. AppPlugin.registerModelBuilder
      2. com.android.build.gradle.internal.ide.ModelBuilder::buildAndroidProject

BasePlugin.configureProject

. .// Apply the Java plugin
        project.getPlugins().apply(JavaBasePlugin.class);

        dslServices =
                new DslServicesImpl(
                        projectServices,
                        newDslVariableFactory(syncIssueReporter), sdkComponentsBuildService); . .Copy the code
Plugin TaskManager
com.android.build.gradle.internal.plugins.AppPlugin com/android/build/gradle/internal/tasks/ApplicationTaskManager.kt
com.android.build.gradle.internal.plugins.LibraryPlugin com/android/build/gradle/internal/LibraryTaskManager.java

ApplicationTaskManager and LibraryTaskManager inherits com. Android. Build. Gradle. Internal. The TaskManager.

Task

ManifestMerger2 merges the Androidmanifest.xml file of the app and library.

build-system/gradle-core/src/main/java/com/android/build/gradle/internal/plugins/BasePlugin.java
public final void apply(@NonNull Project project)
private void basePluginApply(@NonNull Project project)
private void createTasks()
final void createAndroidTasks()

build-system/gradle-core/src/main/java/com/android/build/gradle/internal/TaskManager.java
public void createTasks()
private void createTasksForVariant(
            @NonNull ComponentInfo<VariantT, VariantPropertiesT> variant,
            @NonNull List<ComponentInfo<VariantT, VariantPropertiesT>> variants) 

build-system/gradle-core/src/main/java/com/android/build/gradle/internal/tasks/ApplicationTaskManager.kt
override fun doCreateTasksForVariant(
        variant: ComponentInfo<ApplicationVariantImpl, ApplicationVariantPropertiesImpl>,
        allVariants: MutableList<ComponentInfo<ApplicationVariantImpl, ApplicationVariantPropertiesImpl>>
    )

build-system/gradle-core/src/main/java/com/android/build/gradle/internal/AbstractAppTaskManager.java
protected void createCommonTasks(
            @NonNull ComponentInfo<VariantT, VariantPropertiesT> variant,
            @NonNull List<ComponentInfo<VariantT, VariantPropertiesT>> allComponentsWithLint)

build-system/gradle-core/src/main/java/com/android/build/gradle/internal/TaskManager.java
createMergeManifestTasks

build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/ProcessApplicationManifest.kt
doFullTaskAction

build-system/gradle-core/src/main/java/com/android/build/gradle/internal/tasks/manifest/ManifestHelper.kt
mergeManifestsForApplication
Copy the code

public MergingReport merge() throws MergeFailureException {
}

build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/ProcessLibraryManifest.kt
ProcessLibRunnable.run
build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/ProcessMultiApkApplicationManifest.kt
processVariantOutput
Copy the code