Detailed introduction to compiling and packaging Android APK with gradle

Detailed introduction to compiling and packaging Android APK with gradle

Understand the construction process of gradle and interpret the configuration of Android gradle plug-in

After reading this article, you must use gradle to generate APK. This article will not talk about how to install and run gradle. If necessary, you can read the reference article at the end of the article first.

Apk package is a zip compressed package. From Java source code and resource files to generating this APK, it has undergone a series of specific processes of compilation and packaging, which can be found in the SDK document (/ docs / tools / building / index. HTML). This series of specific processes are repetitive and cumbersome. The build tool is to process these processes and liberate your hands. As an APK early build tool, ant's build process is very intuitive, like configuration; Gradle can be easily configured, but it is more like a script and can be programmed.

Understanding gradle build

1. Simple understanding of construction tools

From a programmer's point of view, how can you write code to automate your APK generation process? First of all, you need to know where the SDK and NDK you need are. There are several library projects in the Android project. What are their java source code and resource files? The input parameters on the command line certainly can't meet the requirements, so it's natural to think of the configuration file. Therefore, your automation tool is to parse these configuration files and execute the program required to generate APK files. Gradle is such a tool program. The configuration file is your common settings.gradle and build.gradle, but it also provides more functions, such as dependency management, process control and plug-in mechanism to customize your generation process.

Gradle's programming language is groovy, which requires configuration files to support groovy. Groovy language is based on JVM like Java, and can support Java well. Therefore, you can write extension plug-ins in Java code and write configuration files like normal programming, instead of writing configuration logic in XML like ant.

2.Groovy

Groovy's syntax abbreviates your code to look like a script. I just read a few documents and listed our common introductions: first, groovy is an object-oriented dynamic language (1) semicolons can be omitted at the end of statements (2) variable definitions can be def or directly used (3) function definitions can be def or not (return value declaration is also acceptable)

Function can omit parameter types and function calls can omit parentheses

Let's take an example:

The first line outputs the string Hello, the second line defines a function, and the third line outputs the function call value. The ${} in the middle of double quotation marks can be parsed into an expression to run.

(4) List and map types

The list implementation is java.util.arraylist of Java. Variables are surrounded by [] and separated by commas, such as

The implementation of map is java.util.linkedhashmap, which is also surrounded by [] and separated by commas. The key value pair is in the form of key: value, such as:

(5) Closures

My understanding of closures is similar to that of function pointers in C, or function objects. Examples of objects {[closureparameters - >] statements} that can be called like functions

3.Gradle

Then, with automation tools and programming languages, it depends on how to implement a series of specific processes to generate APK. There is a project in gradle, which means that a project to be compiled and packaged can generate APK and jar, and the project can contain multiple projects; Each project consists of many tasks, which can be understood as different processes; Each task has different actions and a series of actions to be executed (or statements you write to be executed). The execution order of tasks in project is controlled by their dependson. Gradle is executed in task units. Gradle task is executed in the command line. The life cycle of this process is divided into three stages (the build lifecycle is also described in detail in the official user manual):

(1) Initialization phase

Judge which projects are included and create the corresponding project instance. You can see that the latest executed statement is in settings.gradle

(2) Configuration phase

Create different tasks (tasks can be generated dynamically), and determine the execution order of tasks according to the dependson between tasks, or disable some tasks. After this phase is completed, the dependencies between tasks are determined.

(3) Execution phase

After the configuration is completed, it is executed in order according to the dependency relationship.

It should be noted that the statements in the task are usually executed in the configuration phase, while actions such as dofirst and dolast are executed in the execution phase. Therefore, there will be the problem that the statements in the task that are not in your dependency are also executed

In the following example, the printed task content is different

4. After the gradle plug-in and androd plug-in provide basic process control, the next step is what to do and build. Gradle provides language specific plug-ins such as Java and groovy, which are responsible for compiling and integrating plug-ins such as application and war to generate war files of Java executable programs and web programs. Android has written its own plug-ins according to the APK generation process, in which Java plug-ins are also used.

(1) The plug-in name of the custom plug-in is in resources / meta inf / gradle plugins

In the resources / meta inf / gradle plugins directory, there is a file with the suffix properties. The name of this file is the name of the plug-in you use in build.gradle, which declares the implementation class of the plug-in. You can see two plug-in names in android.properties and com.android.application.properties in the source code of Android plug-in. Therefore, in build.gradle, the application project using Android plug-in requires application plugin: 'Android' (already deprecated) or application plugin: 'com. Android. Application'

(2) The implementation class of application in the Android plug-in is appplugin, which inherits from com.android.build.gradle.baseplugin. Call the apply method, the corresponding configureproject(), parse the local.properties, obtain the SDK location, create androidbuilder, apply javabaseplugin, and then createextension() associate BuildType, productflavor, signingconfig, and finally createtasks() , complete the creation of each task

5.DSL(Domain Specific Language)

I translate DSL into domain specific language, which is the pre-defined rules or jargon here. In the DSL of gradle, there are generally common types. One type is project, task, etc., which define different operations and usages. The other is statement block (build script block or configuration block), such as buildscript {}, allprojects {}, which are common in build. Gradle. Android also defines many sentence blocks, such as buildtypes {}, sourcesets {}.

Android gradle plug-in configuration

With the above concepts, look at build.gradle in Android applications. In fact, the article can only talk about some, but more can check the DSL of Android plug-ins

Explain several points

(1) For example, public attributes are declared in build.gradle in the root directory

(2)buildTypes { },productFlavors{ },signingConfigs { }

The type of the above three statement blocks is nameddomainobjectcontainer < T >, where t is BuildType productflavor signingconfig, and if a new type is added to buildtypes {}, productflavors {}, a new task will be generated corresponding to the rule of assembly [flavor] [BuildType]. Therefore, when compilesdkversion is low and multidex is used, instantrun is also used, You can create a new BuildType and use it only during testing

(3) Afterevaluate statement block

When Android's gradle plug-in version is 2.2.0, tasks are already created in afterevaluate. Therefore, if you want to continue using tasks.getbyname ("assemblydebug"), you must write your own statements to the afterevaluate {} statement block

Summary:

The more you write, the more guilty you become. There are a lot of fragmented contents. According to your own understanding, it runs through and covers some contents. You can basically understand build.gradle. However, how statement blocks such as buildtypes {} and BuildType are associated (or how to parse and create objects) is not well understood. If you have time, you can take a good look at in-depth understanding of Android (I): detailed explanation of gradle. General problems, such as how to configure multi-channel packaged productflavors, can be solved by checking the Android DSL.

Reference documents:

1. Official groovy documents 2. Gradle user manual 3. DSL of gradle 4. DSL of Android plug-in 5. In depth understanding of Android (I): detailed explanation of gradle

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>