20 minutes to master Android gradle, the middle-aged crisis of programmers

Task declaration format

To declare a task, you only need to add a task before the task name. For example, the following declares a hello task.

task hello

Usually, we attach some execution actions to the task, which are called actions, such as

hello.doFirst{
    println "hello first"
}

hello.doLast{
    println "hello last"
}

You can also attach a closure Configuration, called Configuration, which can be used not only for assignment, but also for automatic Configuration.

hello {
    println "hello"
}

Task dependency

It is almost meaningless to declare a task alone in actual development. More often, multiple tasks are combined, one depends on the other to form a series of task sets.

task hello

hello.doFirst{
    println "hello "
}

task world(dependsOn: "hello") << {
    println "world"
}

The above code defines two tasks. When we execute the Hello task, we will output Hello. When we execute the world task, we declare dependsOn: "hello", which means that the world depends on Hello. We will execute hello first and then the world.

task xxx << {
}

This syntax is equivalent to

task xxx
xxx.dolast {
}

You can create a new one called build. Com anywhere Gradle text to practice the task definition and dependency described above.

Go on to Project

Android
   │ 
   ├──app
   │   └──build.gradle
   │
   ├──library
   │   └──build.gradle
   │
   ├──*.properties
   │
   ├──build.gradle
   │
   └──setting.gradle

An Android project is usually composed of the above structure, which has many unknown clever uses.

setting.gradle file

About setting Gradle can also write code, which many people don't know. The following code is my last article[ Design suggestions for enterprise Android modular platform ]An example described in setting In the gradle file, you can specify a project location, where you can import modules from an external project into the APP project.

getLocalProperties().entrySet().each { entry ->
    def moduleName = entry.key
    if (Boolean.valueOf(entry.value)) {
        def file = new File(rootProject.projectDir.parent, "/${moduleName.replace("\\W", "")}/${moduleName.toLowerCase()}")
        if (file.exists()) {
            include ":${moduleName.toLowerCase()}"
            project(":${moduleName.toLowerCase()}").projectDir = file
        }
    }
}

build.gradle

The root gradle file of a project is used to describe the unified resources of the project, including the use of sub resources, the dependency environment of plug-ins, and so on.

subprojects{
    apply plugin: 'com.android.library'
    dependencies {
      compile 'com.xxx.xxx:xxx:1.0.0'
   }
}

Generally, when we refer to aar in each module, we will manually compile it in each module, such as support package. But in fact, there is a very simple way to write it once, that is, to declare the dependencies in the subprojects closure in the root gradle file of the project.

Usually, when writing compile dependencies, we will write as follows:

compile 'com.android.support:appcompat-v7:25.0.0'

In fact, in gradle, this is a method call. Its essence is that the compile() method passes in a map parameter. Therefore, the complete writing method is actually as follows:

compile group: 'com.android.support' name:'appcompat-v7' version:'25.0.0'

At the same time, the available key s of map include not only the commonly used group, name and version, but also the rarely used configuration and classifier.

Look at the Task again

Groovy is based on Java, but it adds a lot of closures to help develop and build scripts more conveniently. If you don't know groovy, it doesn't matter. Just write it as Java. In fact, it's most appropriate to write it as Kotlin. If you don't know Kotlin yet, I strongly recommend you check my [ Kotlin Primer ] series of articles

Each Task can configure its input and output. If the output of a Task is consistent with the previous output, it will not be executed repeatedly. At this point, UP-TO-DATE will be output on the command line, indicating that it is the latest result.
For example, the following tasks:

task transform {
    ext.srcFile = file('hello.txt')
    ext.destDir = new File(buildDir, 'generated')
    inputs.file srcFile
    outputs.dir destDir
    doLast {
        destDir.mkdirs()
        def ins = new BufferedReader(new FileReader(srcFile))
        def stringBuilder = new StringBuilder()
        def temp
        while ((temp = ins.readLine()) != null) {
            stringBuilder.append(temp)
        }
        def destFile = new File(destDir, "world.txt")
        destFile.text = stringBuilder.toString()
    }
}

UP-TO-DATE will be output after repeated execution

Behind the operation

ending

Finally, Xiaobian wants to say: no matter what direction to choose in the future, it is important to learn Android technology well. After all, for programmers, there are too many knowledge contents and technologies to learn. If they want not to be eliminated by the environment, they have to constantly improve themselves. It is always us to adapt to the environment, not the environment to adapt to us!

It's easy to be a programmer. Being an excellent programmer requires continuous learning. From junior programmer to senior programmer, from junior architect to senior architect, or to management, from technical manager to technical director, each stage needs to master different abilities. Determine your career direction early in order to get rid of your peers in work and ability improvement.

If you want to get a high salary to improve your technology, your salary will get a qualitative leap. The quickest way is that someone can take you to analyze together, which is the most efficient way to learn. Therefore, in order for you to smoothly advance to middle and senior architects, I specially prepared a set of high-quality Android architect courses such as source code and framework video for you to learn, so as to ensure that your salary will rise to a higher level after you learn.

CodeChina open source project: Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code

When you have a learning route, what to learn, and know how to go in the future, you always have to practice when you see too much theory.

Advanced UI, custom View

The knowledge of UI is the most used nowadays. In the popular Android induction training in those years, you can easily find a good job by learning this small piece of knowledge.

However, it is obvious that it is not enough now. Refuse the endless CV, go to the project practice in person, read the source code and study the principle!

img-6jLr97Hw-1630636817571)]

Advanced UI, custom View

The knowledge of UI is the most used nowadays. In the popular Android induction training in those years, you can easily find a good job by learning this small piece of knowledge.

However, it is obvious that it is not enough now. Refuse the endless CV, go to the project practice in person, read the source code and study the principle!

[external chain picture transferring... (img-M8fV8kJ4-1630636817572)]

Keywords: Java Android Design Pattern

Added by TheDeadPool on Fri, 17 Dec 2021 03:31:57 +0200