The introduction

In the previous section, you saw two ways to extend a project property (as defined in the gradle file with ext combined closure/gradle.properties).

In this video, we’re going to look at the More convenient File Manipulation API and other apis under Projec.

Although we learned about file manipulation in groovy, the focus of this document is on more convenient manipulation

More convenient file manipulation API

A brief classification

Operations on files in project are classified as follows:

(1) Path acquisition related API, such as: access to the current project path, root project path, etc

(2) API related to file operation

(2.1) Locate (2.2) Copy (2.3) traverseCopy the code

Path to obtain related apis

(1) Directory of the current root project

println "Current root project directory:" + getRootDir().getAbsoluteFile()
Copy the code

(2) Current project, build directory

println "Current project build:" + getBuildDir().getAbsoluteFile()
Copy the code

(3) Current project, path directory

println "Current project path:" + getProjectDir().getAbsoluteFile()
Copy the code

API for file manipulation

The file operation (location/copy/traverse, etc.) is relative to the current project location, not relative to the operating system directory;

If you need to cross projects, etc., then you’ll also need to use the file operation from groovy last time.

(1) Positioning

Print gradle for the current project:

// File location is located
def fileTxt(String p) {
    try {
        def file = file(p)
        return file.text
    } catch (GradleException e) {
        println "fileTxt : file is not exit..."
    }
    return null
}

println "File location, using the build.gradle content of the current project directory:" + fileTxt('build.gradle')
Copy the code

(2) Copy

Copy the current project files to the root project

copy {
    // Copy the folder
    from file('build/outputs/apk')
    into getRootProject().getBuildDir()
}
Copy the code

(3) traversal

Print the apK directory file name

fileTree('build/outputs/apk') { FileTree fileTree ->
    fileTree.visit { FileVisitDetails details ->
        println "FileTree traversal file name:" + details.name
    }
}
Copy the code

Other apis

Engineering depends on

There are two main categories:

(1) Gradle’s own dependence on third parties

(2) Dependencies of application programs (such as app/lib, etc.)

Gradle itself relies on third parties

(1) written 1

buildscript { ScriptHandler scriptHandler ->
    scriptHandler.dependencies {
        classpath 'com. Android. Tools. Build: gradle: 3.5.0'}}Copy the code

(2)

buildscript {
    dependencies {
        classpath 'com. Android. Tools. Build: gradle: 3.5.0'}}Copy the code

Application dependencies (e.g. App /lib, etc.)

(1) Local dependency:

 implementation fileTree(dir: 'libs'.include: ['*.jar'])
Copy the code

(2) Remote dependency:

implementation 'androidx. Constraintlayout: constraintlayout: 2.0.4'
Copy the code

(3) Rely on keywords

Dependent keywords include compileOnly and implementation. For details, see the following table

AS can be seen from different gradle versions, the dependency keywords have also been adjusted. Below, we will talk about the common keywords in daily life

(3.1) the difference between implementation and compile

The characteristics of implementation are: can not pass, can speed up compilation (reduce unnecessary repeat compilation process)

The difference between implementation and compile:

(3.2) Compile and API commonalities

Dependencies pass libraries that depend on them.

Provided and compileOnly

Only valid at compile time, does not participate in packaging (can reduce output file size)

Usage Scenario 1:

A library is only compiled to produce an AAR, etc., and does not need to be tapped into the appCopy the code

Usage Scenario 2:

The AAP module already relies on OKHTTP, and then the library that the APP relies on also uses OKHTTP, which can be compiled using placeholders;Copy the code

External command

If we need operating system-level commands (such as cross-project/local system directory interaction), we need external commands provided by Project

Definition:

task(name: 'daviCopy') {
    println "daviCopy"

    doLast {
        println "==doLast=="
        DoLast is the execution phase of Gradle

        def sourceF = this.buildDir.path + '/outputs/apk'
        def desF = '/Users/yabber/Desktop/1111'// Local directory
        def cmd = "mv -f ${sourceF} ${desF}"
        // Command base template
        exec { ExecSpec execSpec ->
            try {
                // Script type
                execSpec.setExecutable('bash')
                // Command execution
                execSpec.args('-c', cmd)
            } catch (Exception e) {
                e.printStackTrace()
            }
        }
    }
}
Copy the code

Call (Linux/MAC environment) :

./gradlew daviCopy
Copy the code

At the end

Haha, that’s all for this article (systematic learning and growing together)

Tips

For more exciting content, please follow “DaviAndroid” wechat public account