Detailed explanation of Android gradle Development Guide

About gradle

Gradle is an excellent build system and build tool that allows you to create custom build logic through plug-ins. It has the following characteristics:

The purpose of using gradle to build a project is to achieve the following objectives:

Build project foundation

File construction

The construction process of a gradle project is defined in the build. Gradle file, which is located in the root directory of the project. The build.gradle file of the simplest pure java project contains the following contents.

apply plugin: 'java'

The above code introduces gradle's Java plug-in, which provides everything you need to build and test Java applications. For example, the following is the source code of the build.gradle file of the simplest Android project.

buildscript {
  repositories {
    google()
    jcenter()

  }
  dependencies {
    classpath 'com.android.tools.build:gradle:3.4.1'
  }
}

allprojects {
  repositories {
    google()
    jcenter()

  }
}

task clean(type: Delete) {
  delete rootProject.buildDir
}

Build.gradle file

Generally, there are at least two build.gradle files in an Android project, one is the gradle file of project and the other is the gradle file of APP module.

Many common commands and codes are involved in the gradle file. Their specific meanings are as follows:

1.jcenter()

After setting, the code hosting library can reference the open source project on jcenter in the project and declare it in the repositories closure of the build.gradle file.

2. Gradle plug-in and version number

You often see the following code:

classpath 'com.android.tools.build:gradle:3.4.1'

3. Android closure configuration

Some common configurations can be seen in the Android closure of build.gradle, as shown below:

4. Buildtypes closure

This configuration package generally contains two closure configurations, one is debug and the other is release; Of course, there can be other closures. The debug closure is used to generate the configuration of the beta installation file, and the release closure is used to generate the configuration of the formal installation file. The configuration of this file is as follows:

5. Dependencies closure

There are three dependency modes in Android studio project development: local dependency, library dependency and remote dependency.

Project structure

Gradle follows the concept of convention over configuration and provides reasonable default configuration parameters as far as possible. The basic Android project starts with two components called "source sets", namely main source code and test code. They are located in: Src / main / and Src / Android test / files, respectively. For Java plugin and Android plugin, their java source code and resource file paths are as follows: in the Java / and resources / file directories.

For Android plugin, it also has the following unique file and folder structure:

Configuration structure

When the default project structure is not applicable, you may need to customize it. According to the gradle documentation, you can reconfigure sourcesets for a java project using the following methods:

sourceSets {
  main {
    java {
      srcDir 'src/java'
    }
    resources {
      srcDir 'src/resources'
    }
  }
}

Of course, the following configuration methods can also be used:

sourceSets {
  main.java.srcDirs = ['src/java']
  main.resources.srcDirs = ['src/resources']
}

Android plugin uses a similar syntax. However, since it uses its own sourcesets, these configurations will be added to the Android object.

The following is an example that uses the main source code from the old project structure and remaps the Android testsourceset component to the tests folder.

android {
  sourceSets {
    main {
      manifest.srcFile 'AndroidManifest.xml'
      java.srcDirs = ['src']
      resources.srcDirs = ['src']
      aidl.srcDirs = ['src']
      renderscript.srcDirs = ['src']
      res.srcDirs = ['res']
      assets.srcDirs = ['assets']
    }

    androidTest.setRoot('tests')
  }
}

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>