Androidstudio has become a mainstream tool for Android developers, and Gradle's principles, configuration, and use remain a puzzle for many Android developers. Although there are a lot of relevant documents on the Internet, but either it will only be used but do not know the principle of gradle, or is too deep into gradle and groovy, not with the specific Android use scenes combined, resulting in a lot of people to read foggy. This article is only for Android developers, combined with specific use, the introduction of the essence of gradle and groovy, hoping that more Android developers can not only know it, but also know why.
Reference books:
Combat Gradle
Android Gradle Authoritative Guide
Using Gradle to build Android apps
1. Gradle Introduction
In 2013, Google released a new Android development IDE ———— Android Studio, the core of which is Google's new Gradle compilation environment. First, we need to understand that Android Studio uses the Gradle build tool, and Eclipse's ADT plugin uses the Ant build tool. Second, figure out why Android Studio uses the Gradle build tool. What are the characteristics of gradle?
- The Domain specific Language (DSL language) is used to describe and control the building logic.
- The build file is based on Groovy and allows you to control the custom build logic by mixing the DSL elements and using code to govern the DSL elements.
- Support for Maven or Ivy dependency management.
- Very flexible. Allows the use of the best implementations, but does not enforce them in a way.
- Plug-ins can provide their own DSL and APIs for use in building files.
- Good API tools for IDE integration.
As a result, Gradle is powerful because it inherits powerful, flexible Ant and Maven-rich dependency management, simple configuration management, easy scripting and flexible, plug-in modularity.
2. gradle file in AS
The superiority of gradle makes it widely used in many places, this article analyzes and uses from the angle of Android.
An item structure in Android is usually as follows:
The relevant file information is as follows:
| Catalog Files |
definition |
| . gradle |
Gradle Project Generation file (auto-compile tool-generated files) |
| . idea |
Idea Project file (development tool-generated files) |
| App |
One of the module, reusing the settings of the parent project, can have the same configuration file as the parent project |
| Build |
Where to generate files automatically when building |
| Gradle |
Auto-complete Gradle Environment Support folder |
| . gitignore |
Git source control files |
| Build.gradle |
Gradle Project Auto-compiled configuration file |
| Gradle.properties |
Gradle Run Environment configuration file |
| Gradlew |
Automatically complete Linux Mac scripts for the Gradle environment, with Gradle folders |
| Gradlew.bat |
Automatically complete Windows scripts for the Gradle environment, with Gradle folders |
| Local.properties |
Android SDK NDK Environment Path configuration |
| *.iml |
Idea Project file |
| Setting.gradle |
Gradle Project Sub-project contains files |
2.1 Settings.gradle
Why didn't I start with Build.gradle, but start with the Settings.gradle file? Because this is the Gradle setup file for initialization and configuration of the project tree, located in the root project directory. The majority of the settings file is intended to configure sub-engineering. Multi-engineering in Gradle is represented by the engineering tree, which is equivalent to the concept of project and Moudle as seen in as. The root project is equivalent to project in as, and a root project can have many sub-projects, that is, many moudle.
A sub-project will only be recognized if it is configured in the settings file, and will be included in the build when it is gradle. The specific use is as follows:
Defines a function named Initminshenggradleenvironment. The function internally completes some initialization operations//such as creating a specific directory, setting specific parameters such as Def inittkgradleenvironment () { println "initialize TK Gradle environment ... " ...//dry some special reverse telecommuting .... println "Initialize TK Gradle environment completes ..."}//settings.gradle when it is loaded, it executes inittkgradleenvironment
Inittkgradleenvironment ()
Include ': Voice '
Include ': Imagepicker ' Project (': Imagepicker '). ProjectDir = new File (RootDir, ' Tk/imagepicker ')
First of all, this file can set up some of its own functions, such as inittkgradleenvironment (), these functions will be gradle when building the entire project task, so you can do some initialization work in settings.
Second, the name of the Child Project (its folder name) can be included in the include function. It is important to note that if a directory like Imagepicker ' sub-project specifies a folder, its project directory is Rootdir/tk/imagepicker, and as the Voice sub-project does not specify the appropriate folder directory, the default directory is its sibling directory . That is, Gradle will use the Voice directory that is currently associated with the Settings.gradle file as the project directory for the Voice sub-project . If not, there will be an error.
2.2 Build.gradle
Each project will have a build file that is the portal that the project builds, and can be set up here for the project, such as the configuration version, which plugins are required, which libraries to rely on, and so on. There are two build files in the as project structure. We introduce them separately.
2.2.1 App Moudle's build file
Android Studio automatically creates the module default generated by the Build.gradle file, which is the most frequently developed file in the development.
This file includes multiple code blocks such as apply plugin,android,greendao,repositories,dependencies.
Android { Compilesdkversion project. android_build_sdk_version as int buildtoolsversion project. Android_build_tools_version defaultconfig { ApplicationID project. application_id Versioncode Project. Version_code as int versionname project. Version_name minsdkversion Project. android_build_min_sdk_version as int targetsdkversion project. android_build_target_sdk_version as int }} dependencies { compile filetree (include: [' *.jar '], dir: ' Libs ') //Note here are double quotes compile "com.android.support:appcompat-v7:${support_library}" compile " Com.android.support:design:${support_library} " compile" Com.android.support:recyclerview-v7:${support_ LIBRARY} " compile" com.android.support:support-annotations:${support_library} " compile" Com.android.support:cardview-v7:${support_library} " compile" Com.android.support:support-v4:${support_ LIBRARY} "}
2.2.2 Root project's build file 2.3 gradle.properties
Configures the global management file. A file used to configure the Gradle run environment, such as configuring the Gradle run mode and the size of the JVM virtual machine at run time.
# # Project-wide Gradle settings.## For more details about to configure your build environment visit# http://www.gradle.or g/docs/current/userguide/build_environment.html## Specifies the JVM arguments used for the daemon process.# the setting is Particularly useful for tweaking memory settings.# Default value:-xmx1024m-xx:maxpermsize=256m# ORG.GRADLE.JVMARGS=-XM x2048m-xx:maxpermsize=512m-xx:+heapdumponoutofmemoryerror-dfile.encoding=utf-8## when configured, Gradle'll run in incubating parallel mode.# This option should only being used with decoupled projects. More details, visit# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_ projects# org.gradle.parallel=true#wed Mar 09:55:30 CST 2018org.gradle.jvmargs=-xmx1536m-xmx4048m
The properties are actually the configuration files in the Java project, not gradle original. And because Gradle syntax can be mixed with Java, it is also possible to use the properties file on a Java project, even above the gradle. Gradle borrowed this, so the use of gradle.properties was built in.
Properties in the data format using key-value pairs, probably the following several ways:
1. Key=value
2. Key:value
3. Key:value
4. # as a comment
In Android Studio it is best to use the first notation, without warning.
In the actual development, the general large-scale projects are split for modular development, each module in the merger is prone to conflict (such as version conflict), or need to change the version of the time is more troublesome, need a place to change, this time to configure the global gradle.properties file is very necessary. Refer to the following settings for details:
# # Project-wide Gradle settings.## For more details about to configure your build environment visit# http://www.gradle.or g/docs/current/userguide/build_environment.html## Specifies the JVM arguments used for the daemon process.# the setting is Particularly useful for tweaking memory settings.# Default value:-xmx1024m-xx:maxpermsize=256m# ORG.GRADLE.JVMARGS=-XM x2048m-xx:maxpermsize=512m-xx:+heapdumponoutofmemoryerror-dfile.encoding=utf-8## when configured, Gradle'll run in incubating parallel mode.# This option should only being used with decoupled projects. More details, visit# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_ projects# org.gradle.parallel=true#wed Mar 09:55:30 CST 2018org.gradle.jvmargs=-xmx1536m-xmx4048m# Add NDK support (add as needed) android.usedeprecatedndk=true# App version name version_name=1.0.0# app version number version_code=100# Support library version support_library=24.2.1# MIN_ sdk_versionandroid_build_min_sdk_version=14# target_sdk_versionandroid_build_target_sdk_version=24# build_sdk_versionandroid_build_sdk_version=24# build_tools_versionandroid_build_tools_version=24.0.3
2.4 local.properties
In a project created automatically by Android Studio, the IDE automatically comes with a local profile and sets the Sdk.dir property. That is, the ANDROID_HOME environment variable is configured, so the IDE can automatically generate this configuration file. In general, you don't need to modify this configuration file unless you have to re-create an SDK path.
The most practical Guide to Androidgradle