Getting started using Gradle in Android to build an App project

  • 2021-07-24 11:49:12
  • OfStack

gradle is a brand-new build system introduced in the development of Android, because the brand-new build system is mainly for the following purposes:

1. Easy reuse of code and resources
2. It is easier to build multiple versions of apk, whether it is different apk for multiple channels or apk for different environments (debug, release)
3. Easy to configure, extend, and customize the build process
4. Good IDE integration

Why Gradle?
Gradle mainly has the following points:

1. Use domain-driven language (DSL) to describe build logic
2. The build script uses Groovy, which makes it easy to customize the build logic
3. Built-in dependency management system, using Maven or Ivy
4. Very flexible. Best practices can be used but are not mandatory
5. Good IDE support

The Gradle project is configured using the build. gradle file in the project root directory
build. gradle for 1 simplest Java project is as follows


apply plugin: 'java' 

This means that the project will use the java plug-in of Gradle, which is provided by default by Gradle, and the java plug-in provides the functions of building java applications and running tests.

The simplest Android project build. gralde


buildscript { 
  repositories { 
    mavenCentral() 
  } 
 
 
  dependencies { 
    classpath 'com.android.tools.build:gradle:0.11.1' 
  } 
} 
 
 
apply plugin: 'android' 
 
 
android { 
  compileSdkVersion 19 
  buildToolsVersion "19.0.0" 
} 

The Android build file consists of three parts:
1. buildscript {...} Configuration-driven code for the build process, meaning that the configuration in buildscript only works during the build process, that is, the configuration used by the builder.
The above code states that our builder will use the Maven central repository and that there is an classpath dependency that points to an Maven repository. The Maven library is the Android Gradle plug-in, version 0.11. 1.

2. This is followed by the use of the Android plug-in, just like the previous use of the Java plug-in 1.

3. Finally, android {...} configures all the parameters required for the Android project to build.
By default, you only need to specify compileSdkVersion and buildtoolsVersion, compileSdkVersion specifying which version of sdk to compile and buildToolsVersion specifying which version of the build tool to use.
Note:
1. You only need to use the android plug-in, and then use the Java plug-in to report an error.
2. Use the sdk. dir property in local. properties to specify the SDK path, or you can use the ANDROID_HOME environment variable.

Project structure
Gradle adopts the principle that convention is better than configuration, and the simplest way is to use a default directory structure. Of course, the directory structure can be modified by itself.

By default, the code of android gradle project is under src directory, and there are two directories under src, main and androidTest, in which main directory is project code and androidTest directory is test code.


src/main/
src/androidTest/

Under the main and androidTest directories, there are no different directories for different types of code.
For the Gradle plug-ins of Java and Android, the Java code and Java resources correspond to the java directory and the resources directory, respectively. The Android plug-in also requires 1 additional directories and files, such as


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

Let's look at how to change these default configurations.

Android project, by default, java files and resource files are in src/main/java and src/main/res directories respectively, and java files and resource files can be placed in src/java and src/resources directories by adding the following code in build. gradle files and andorid {}. (Note that if you make changes in the gradle file, you only need to re-create sync1. gradle will automatically create a new directory and move the corresponding files to the new directory.)


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

A simpler way to write it is


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

In fact, you can specify multiple directories in the array, so that you can place code and resources in multiple directories.
Looking at an example of an Android project:


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') 
  } 
} 

Here, main code actually uses the default value of Android Gradle, while androidTest no longer uses the default androidTest directory, but uses tests directory.


Related articles: