Gradle Basic concepts

  • projectsEach build is made up of one or more projects. Each Module of an Android project is a project, which corresponds to a project in a modulebuild.gradlefile

  • tasksTasks: Each project consists of one or more tasks. A task represents some more detailed build

Gradle extension

Look at the project above, which implements ExtensionAware. The getExtensions method of ExtensionAware returns an ExtensionContainer, which is an extendable container. With ExtensionContainer we can create extensions.

Org. Gradle. API. The tasks. TaskContainer interface: / / search task findByPath path: (String) : task getByPath (path: String) : Task getByName(name: String): Task withType(type: Class): TaskCollection matching(condition: Closure): Task create(name: String): task create(name: String, configure: Closure): task create(name: String, configure: Closure): task create(name: Closure) String, type: Class): Task create(options: Map<String, ? >): Task create(options: Map<String, ? >, configure: Closure): TaskCopy the code

According to the document, let’s try it out

Comment out the contents of the configuration, and let’s see the result of running the print

As you can see from the figure above, if we do not have a configuration, we will use the default configuration. This is not familiar, for example, we often change the version number in android{}, etc. If you look at the source code, you know that Android is actually an AppExtension class. So what properties android can configure can be found in AppExtension.

/** * Gradle plugin class for 'application' projects. */ public class AppPlugin extends BasePlugin implements Plugin<Project> { protected BaseExtension createExtension( @NonNull Project project, @NonNull Instantiator instantiator, @NonNull AndroidBuilder androidBuilder, @NonNull SdkHandler sdkHandler, @NonNull NamedDomainObjectContainer<BuildType> buildTypeContainer, @NonNull NamedDomainObjectContainer<ProductFlavor> productFlavorContainer, @NonNull NamedDomainObjectContainer<SigningConfig> signingConfigContainer, @nonnull ExtraModelInfo ExtraModelInfo) {return project.getextensions ().create("android",//"android" is the name of the Extension  AppExtension.class, project, instantiator, androidBuilder, sdkHandler, buildTypeContainer, productFlavorContainer, signingConfigContainer, extraModelInfo); }Copy the code

Gradle Local script plug-in

Sometimes we don’t want to have many gradle scripts written in build.gradle, so apply can rely on other local gradle files, just like import in Java

Gradle Remote script plug-in

It is the same as the local plug-in, but it depends on the URL of the remote HTTP. Sometimes we need to use this method when we need to change scripts dynamically. For example, we need to unify the version numbers of dependent components, and all components released in the same version refer to the remote Gradle file in the same branch.

Gradle binary plugin

Gradle plug-in mainly implements the Plugin interface apply method. The following is a plug-in I wrote before to check whether all the images in the project are WebP. Because webP is small, it is used for volume optimization task plug-in

  1. Create a buildSrc directory at the root of the project. The name must be buildSrc. Once created, rebuild the project, which will generate some directories under buildSrc
  2. Create it in the buildSrc directorybuild.gradle.ktsFile, write the plugin in Kotlin

plugins {
    `kotlin-dsl`
}
repositories {
    jcenter()
}
Copy the code
  1. Create it in the buildSrc directorysrc/main/kotlinDirectory, directory name must not be wrong!! , and then write the plug-in

4. Create the resources directory in main, then create the Meta-INF directory in resources, and create the Gradle-plugins directory in meta-INF. Finally, create a properties file in the gradle-plugins directory. Note the name of this file. You can name it whatever you want, but you will use this name later when using the plugin. Named, for example, you com. Example. Harry. The properties, then the plug-in id is com. Example. Harry

5. Reference in the app

  • There is another way to generate plug-ins
GradlePlugin {plugins {create("HarryPlugin2") {// plugins id used to refer to plugins, e.g. : apply plugin: 'id' id = "com. Example. Harry2" / / specified implementationClass plug-in implementation class = "com. Example. Harry. HarryPlugin2"}}}Copy the code

  • We can also use this buildSrc package for dependency management

Release of maven standalone plug-in

  • Create a new module without using the buildSrc command. Create a new module, remove the default gradle content, add Maven dependencies, and write a plugin to publish the local Maven repository

Once published, you can reference it in your project

The above is the local repository to use. If you have a Maven server, replace the URL with remote Maven. Click here for maven service setup

Code address in text