Android Gradle Groovy is an Android Gradle Groovy application for building automation
In the last part, we introduced Gradle’s basic syntax. Now let’s take a look at Gradle’s advanced techniques: build scripts, custom tasks, build lifecycles, dependency conflict resolution, multi-project building, etc.
Next, let’s take a look at a few simple applications of Gradle
- As projects grow larger and componentization prevails, we sometimes have to versioning multiple modules. The general practice is:
- Create ****. Gradle in the project root directory.
- through
apply from
Import the configuration file, and then userootProject.ext
Introduce related attributes.
Gradle ext {versions = [sdkMinVersion : 14, sdkTargetVersion : 26, ... ] depVersion = [ appCompatVersion :"26. +",
recyclerViewVersion : "26.0.0-1"
]
deps = [
suport : [
appcompat : "com.android.support:appcompat-v7:${depVersion.appCompatVersion}",
recyclerview: "com.android.support:recyclerview-v7:${depVersion.recyclerViewVersion}"[]} // 2.'config/config.gradle'
android {
def versions = rootProject.ext.versions
compileSdkVersion versions.sdkCompileVersion
buildToolsVersion versions.toolsBuildVersion
...
}
dependencies {
def dependencies = rootProject.ext.deps
compile dependencies.suport.appcompat
}
Copy the code
- For example, we often see this mistake:
Error:Execution failed for task ':test:processDebugManifest'.> Manifest merger failed with multiple errors, see logs
Copy the code
Our usual solution is to run gradlew processDebugManifest — stackTrace.
-
Finally, we are looking at a simple single-machine execution task
-
Run gradle app:dependencies to check app dependencies and exclude all jars from your application.
compile ('com. Android. Support: design: 22.2.1')
{
exclude group: 'com.android.support'
}
Copy the code
Gradle’s build. Gradle file is a Gradle script file that defines the project and all its tasks. Then execute the related task. Here we come together step by step to uncover its mystery.
Gradle projects and Tasks
As you can see in the screenshot above, each Gradle project consists of one or more projects. Each project consists of one or more tasks. A task represents the smallest unit of work. This can be a compilation of a class, printing a JAR package, generating a Javadoc, or committing a release to a repository.
Definition and use of tasks
In a task, we can use dependsOn to define a dependency, doFirst, doLast to enhance an existing task. We still use the IDEA development tool to open the previous project, delete all the content in the previous build.gradle file, write and input the following code
task hello {
doLast {
println 'Hello world! '
}
}
task release() {
doLast {
println "I'm release task"}} // add task dependency release.dependson hello // Enhance the existing task // Add task dependsOn hello ondoAdd hello.doFirst {println to the First action'Hello doFirst'} // The French side two is indoAdd hello.doLast {println to the Last action'Hello doLast'
}
Copy the code
Open the end of the command line and run gradle -q release.
task myTask {
ext.myProperty = "myValue"
}
task printTaskProperties {
doLast {println mytask.myProperty}} Run ➜ gradle -qprintTaskProperties
myValue
Copy the code
Of course we can configure existing tasks: disable or override them. First we define a myCopy task, the code is as follows:
task myCopy(type: Copy) {
from 'resources'
into 'target'
include('**/*.txt'.'**/*.xml'.'**/*.properties')}Copy the code
If you want to overwrite the copy task, you can set the overwrite attribute to true as shown in the following example:
task copy(type: Copy)
task copy(overwrite: true) {
doLast {
println('overwrite the copy.'Gradle -q copy overwrite the copy.Copy the code
Finally, let’s look at how to disable certain tasks. Let’s just look at the code
copy.enabled = false
Copy the code
Practical article
Next, let’s customize the plug-in. There are three ways to write it
-
Written directly in the build.gradle script that we used to build the project, this approach has the advantage that the plug-in is automatically compiled and loaded into our classpath, but it has the obvious limitation that it cannot be reused anywhere other than in the included script.
-
In our building project rootProjectDir buildSrc/SRC/main/groovy directory, Gradle automatically compiled into the current project classpath, the scheme all compiled script can be used, but in addition to the current project can reuse.
-
Written as a single project, this project is eventually compiled and published as a JAR package that can be shared across multiple projects or different teams.
So let’s go step by step and write a Demo in the third way.
SRC /main/groovy/ your package name. Then create a meta-INF /gradle-plugins folder in the Resources directory. Create a new hello.properties file and type on the blackboard and notice that the file name is the name you’ll use later. Here is Hello, so we have to introduce it like thisapply plugin: 'hello'
Enter the plug-in’s full path name like this:implementation-class=org.gradle.HelloPlugin
The code is then written in two steps:
- Inheriting from DefaultTask, annotated with TaskAction so that Gradle calls it by default when the task is executed
- Then, the custom plug-in class is realized by implementing the Plugin interface and the apply(Project Project) method is implemented.
To follow the steps, we first create a new mytask.groovy file that simply declares a member variable and then prints it.
class MyTask extends DefaultTask {
String input = 'hello from MyTask'
@TaskAction
def greet() {
println input
}
}
Copy the code
Then we create a new hello.groovy file. We added a hello task to the plugin. We know that gradle can configure parameters such as defaultConfig {} NDK {}, etc. Gradle uses Extension objects to add parameters to the plugin.
Class Hello implements Plugin<Project> {@override void apply(Project Project) {// Save para to extension container, And applied to the HelloPluginExtension project. Extensions. Create ("para", HelloPluginExtension) // Add hello task project.task('hello'.type:MyTask) {
input = 'Hello Plugin input! '
doLast {
println "${project.para.first}${project.para.last}"}}}}Copy the code
Next release the project to a local repository for use by other projects inbuild.gradle
In the input
Maven-publish: maven-publish: maven-publish: maven-publish:'maven-publish'
publishing{
publications {
mavenJava(MavenPublication) {
from components.java
groupId 'org.gradle'
artifactId 'customPlugin'
version 1.0 the SNAPSHOT ' '
}
}
repositories{
maven {
// change to point to your repo, e.g. http://my.org/repo
url ".. /repo"}}}Copy the code
Click Publish task and see the project successfully published to the local repository.. / repo.
Supplements and FAQs
A file tree is a hierarchical collection of files. A file tree can represent a directory structure or the content structure of a ZIP package. useProject.fileTree(java.util.Map)
Create files that can be included or excluded using filtering conditions.
FileTree tree = FileTree (dir:'src/main') // Add the specified file tree.include to the file tree object'**/*.java'Implementation fileTree(include: ['*.jar'], dir: 'libs')
Copy the code
Compile configuration for multiple modules
Include ‘:app’, ‘:plugin_common’, ‘:plugin_gallery’. You can set common behavior with guild. Gradle. Add a Hello task as shown below.
The path of a project begins with a colon (: it represents the root project), followed by the name of the project. For example: “common”. The path of a task is the project path plus the task name, for example :common:hello. For example, just execute gradle :plugin_common:hello
Download the source code