Explanation of Android Gradle Development Guide

  • 2021-11-13 18:05:26
  • OfStack

Introduction to 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:

Domain Specific Language (DSL language) is used to describe and control the construction logic. The build file is based on Groovy and allows you to control custom build logic by mixing declaring DSL elements and using code to control DSL elements. Support dependency management of Maven or Ivy. Very flexible. The best implementation is allowed, but the implementation method is not enforced. Plugins can provide their own DSL and API for use by build files. Good API tool for IDE integration.

Using Gradle to build a project is mainly for the following purposes:

Make it easier to reuse code and resources Make it easier to create different versions of the same 1 application, whether it's multiple apk releases or different custom versions of the same 1 application Make the build process easier to configure, extend, and customize. Integrate excellent IDE.

Build the project foundation

File construction

The build process for 1 Gradle project is defined in the build. gradle file under the root directory of the project. The build. gradle file for the simplest Gradle pure Java project contains the following.


apply plugin: 'java'

The code above is the Java plug-in that introduces Gradle, which provides everything you need to build and test an Java application. For example, the following is the source code for the build. gradle file for one of the simplest Android projects.


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

At least 2 build. gradle files, 1 gradle file of project and 1 gradle file of app module will appear in an Android project.

There are many common commands and codes involved in the gradle file, and their specific meanings are as follows:

1.jcenter()

Code managed library, can be set in the project reference jcenter on the open source project, declared in build. gradle file repositories closure.

2. gradle plug-in and version number

You will often see the following 1 piece of code:


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

3. Android closure configuration

You will see one common configuration in the Android closure of build. gradle, as follows:

compileSdkVersion: Used to specify the compiled version of the project. buildToolsVersion: Used to specify the version of the build tool for the project. applicationId: Used to specify the package name of the project, which was specified when the project was created, and can be changed here when you want to modify the package name of the entire project. minSdkVersion: The lowest compatible version of the project. targetSdkVersion: This means that you have been fully tested on the target version and the system will open up some of the latest features and features for your application. If targetSdkVersion is 23 or higher, new functions and features will be opened when running this application in Android 6.0; If it is set to 22, it only means that your application has been fully tested on Android 5.1, and the new features of Android 6.0 will not be enabled. versionCode: The version number of the project. versionName: The version name of the project version number.

4. buildTypes closure

This configuration package 1 generally contains two closure configurations, one is debug and one 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 official installation file. The configuration of this file is as follows:

minifyEnabled: Used to set whether the code for the project is obfuscated. true stands for open and false stands for closed. proguardFiles: Specifies the file to use when obfuscating. proguard-android. txt: Under the Android SDK directory, there are obfuscation rules common to all projects. proguard-rules. pro: Is in the current project root directory, where you write obfuscation rules specific to the current project.

5. dependencies closure

There are three dependencies in Android Studio project development: local dependency, library dependency and remote dependency.

Local dependencies: You can add dependencies to local Jar packages or directories. Library dependencies: You can add dependencies to library modules in your project. Remote dependencies: = You can add dependencies to open source projects on the jcenter library.

Project structure

Gradle follows the concept of convention over configuration and provides reasonable default configuration parameters wherever possible. The basic project for Android begins with two components called "source sets", main source code and test code. They are in the files src/main/and src/androidTest/, 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:

AndroidManifest.xml res/ assets/ aidl/ rs/ jni/

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 an Java project using the following methods:


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

Of course, you can also use the following configuration methods:


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

Android Plugin uses a similar syntax. But 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 from the old project structure and remaps the androidTestsourceSet 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')
 }
}

Related articles: