Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Android Studio quick overview of Gradle     - Windows and Ubuntu dual system, repair of two ways UEFI boot (Linux)

- Can not empty the Recycle Bin to repair problems in Ubuntu 14.04 (Linux)

- JSON Introduction and Usage Summary (Programming)

- MySQL function: group_concat () function (Database)

- Analysis of Java keyword final (Programming)

- Tmux Crash Course: Tips and adjustment (Linux)

- Ubuntu 10.04 to Ubuntu 10.10 Upgrade (Linux)

- 20 Top Linux commands (Linux)

- Sleuth Kit: used to analyze a disk image and restore files open source forensics tools (Linux)

- How to manage the time and date at systemd Linux systems (Linux)

- AngularJS - Getting Started with Routing (Programming)

- Java rewrite the hashcode method (Programming)

- CentOS 6.6 install Oracle 11gR2 database (Database)

- Spring + MyBatis Multi data source switching (Database)

- Creating and extracting archives 11 tar command examples in Linux (Linux)

- Linux file system (inode and block) (Linux)

- Linux uses shared memory communication process synchronization Withdrawal (Programming)

- Practical top command (Linux)

- Linux screen commonly commands (Linux)

- Linux signal and orphans, and zombie process (Programming)

  Android Studio quick overview of Gradle
  Add Date : 2017-08-31      
  Introduction Before reading this article, you already know the expectations of these elements:

What gradle, it has basic features which
gradle task of basic concepts and wording
I use the environment: LUbuntu 14.04, AndroidStudio 1.3.2, which comes with Gradle 2.4

Gradle Scripts

Use AndroidStudio a new Android project, until the default has been created, then open the AndroidStudio left the Project panel, you can see the following engineering organizations

Briefly rude to look at what these files ghost.

build.gradle (Project: MyApplication) This is build.gradle Android project root directory, the file name is the name of a convention, gradle will rely on it to build the project. A project can include multiple projects, gradle can build multiple projects and each project can have build.gradle file subdirectory as make tools and build the relationship between the Makefile. We do most of the work is to prepare and organize build.gradle files.

build.gradle (Module: app) This is the Android project root /app/build.gradle,app gradle is a project, where build.gradle file is used to describe how to build the app works gradle can support the construction of multiple projects, and set dependencies between the projects.


Gradle Wrapper is Android on gradle use some of the necessary package from a script --gradlew (located under the project root directory, use the line for the command is very useful in integrated environment), it will automatically detect the current project-specific gradle version exists, if it does not exist to download (stored in ~ / .gradle / wrapper /). So next Each project has its own gradle Android package, the official suggested that these packages also submitted into the source code management system so that the build system (such as continuous integration server) is not required in advance (manual) installation gradle this project. gradle-wrapper.properties in Gradle Wrapper configuration affect the operation of which distributionUrl attribute specifies where to download gradle, of course, if we do not want it to download, you can download a specified manual distributionPath with local gradle package. (Advanced: with its wrapper related settings See: Wrapper API)

In addition, Android created a default wrapper called the gralde task, by using this task, you can access and modify the properties in the project build.gradle wrapper inside.

proguard-rules.proproguard is an open source project to confuse java code, Android project to integrate it in, the first matter.

gradle.properties because gradle is java program that will run in the JVM environment, this file is used to specify gradle how to run (or called Build Environment), comprising:

Open gradle the daemon parallel build mode, set the JVM memory parameters, accelerate build.

You can customize the attributes (key and value), these properties can be passed to gradle java program can also be accessed build.grade.

gradle provides several ways to add a property to the build environment:

Gradle command line with \ -D parameters, \ - D parameters will be passed to the java (in this case gradle run java program)

Write gradle.properties file inside, and can have its own file gradle.properties under each sub-directory project

Write GRADLE_USER_HOME environment variable points gradle.properties directory under.

ORG_GRADLE_PROJECT_ written in the beginning of the environment variable.

In the above manner can be used simultaneously, gradle in the order listed above were analyzed (from low to high priority), if there is a conflict property, retained high priority configuration.

Set the proxy.

This file is now nothing default configuration, the first matter.

This file is named gradle convention, the default is only one line of code include ': app', indicates that the current project is only one module (we used to call engineering, gradle accustomed to calling module), app is a directory name, as well as the project name. When there are multiple projects, you can add here.
local.properties default content path is to configure the Android SDK and NDK, which is before I AndroidStudio configuration before, we do not need to modify this file in the IDE settings automatically override the setting of this file, the file should not commit to the source management system, because it is configured personal environment.
Well, let's look at the specific build.gradle file.

Root build.gradle

// Top-level build file where you can add configuration options common to all sub-projects / modules.
buildscript {
 repositories {
  jcenter ()
 dependencies {
  classpath 'com.android.tools.build:gradle:1.3.0'
  // NOTE: Do not place your application dependencies here; they belong
  // In the individual module build.gradle files
allprojects {
 repositories {
  jcenter ()

This is a top-level gradle project. gradle engineering systems in a project with gradle objects) that it has the following elements:

allprojects {}

artifacts {}

buildscript {}

configurations {}

dependencies {}

repositories {}

sourceSets {}

subprojects {}

publishing {}

Here we explore only the relevant conduct.

Gradle Plugins

Gradle kernel integrates a number of plug-ins (such as compiling java code plug-in), the use of plug-in, we just have to do very little work to complete the construction of a conventional task. In .jar files into the plug-in called binary plug, before using this plug-in to give it to the current build script classpath.


buildscript is a method gradle project defined, can pass a block of statements (someone called closure), this statement returns a block ScriptHandler object. See ScriptHandler object API that, ScriptHandler contains two methods: repositories and dependencies, dependent on what to call when setting build dependencies, repositories set these calls come from dependence warehouse. Now I understand, the above code is actually specify a location and version of Android gradle plug.


It is also gradle features, gradle supports allprojects in root build.gradle in advance for all projects may contain the entire build unified configuration, the above code is to give all projects are configured jcenter warehouse, this project depends first tripartite library during the build process will be downloaded automatically. If more than one project (sub-project) have the same behavior or attributes you want to configure, use, this is very convenient.

build.gradle app works under

apply plugin: 'com.android.application'
android {
 compileSdkVersion 21
 buildToolsVersion "22.0.1"
 defaultConfig {
  applicationId "example.jk.myapplication"
  minSdkVersion 19
  targetSdkVersion 21
  versionCode 1
  versionName "1.0"
 buildTypes {
  release {
   minifyEnabled false
   proguardFiles getDefaultProguardFile ( 'proguard-android.txt'), 'proguard-rules.pro'
dependencies {
 compile fileTree (dir: 'libs', include: [ '* .jar'])
 compile 'com.android.support:appcompat-v7:22.2.0'

apply plugin

apply plugin: 'com.android.application' application for Android gradle plug into the current build script, this plug-in fact inherited from gradle the application plug-ins, plug-ins using the application can be run by adding a task or project to build Java-based command line program.

Android gradle plug having application plug-in function, and it will use java plug-ins and distribution, after two plug-ins used to automate compile and publish java program.


This element Android gradle plug provided for configuring android dependent build options, including:

compileSdkVersion Specifies the build version when using the Android SDK

buildToolsVersion Specifies the build version of the tool, build tool can be installed using the Android SDK Manager, you can install multiple versions

Official recommendations build tool version specified here is preferably greater than equal to the target SDK version (targetSdkVersion).

defaultConfig element in the configuration corresponding to the AndroidManifest.xml file, a higher priority than the AndroidManifest.xml file configuration. gradle support for a project to build multiple versions, such as the trial version, commercial version, each version can have its own defaultConfig.

buildTypes element defines the default debug and release both constructs, debug build out of APK will also contain debug symbol and debug key signing, and release type of default is not signed. minifyEnabled true to enable automatic purge useless codes for slimming package. proguardFiles specified code obfuscation using the relevant documents.

Engineering dependent

 dependencies {
    compile fileTree (dir: 'libs', include: [ '* .jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'

Here are the elements gradle project dependencies defined to specify what this project dependencies. The compile method to compile the main program to specify what you want to add, these things will eventually be packaged into the APK.

fileTree (dir: 'libs', include: [ '* .jar']) will return a file tree object, these files are located in app / libs directory and end with .jar.

Sentence meaning of the code is to put all the jar files in the libs directory of the current project directory of the classpath at compile time, and packaged into APK. This is a local binaries depend on the wording.

And compile 'com.android.support:appcompat-v7:22.2.0' represents a dependency located warehouse appcompat library and its version in the Android Support Library, if a local Android Support Library is not installed, use the Android SDK Manager to download.

So far, we've probably learned AndroidStudio default project related gradle script. In fact AndroidStudio IDE provides a lot of settings, these settings modifications, AndroidStudio will automatically update the relevant build.gradle and * .properties files. If we manually edit build.gradle file, AndroidStudio will prompt you to sync (synchronous) operation, so that these configurations reflected in the IDE interface.
- Simple solution CC attack under Linux VPS (Linux)
- How to write a new Git protocol (Linux)
- FreeBSD install Gnome 3 Desktop (Linux)
- Python class of operator overloading (Programming)
- Linux - Common process the command (Linux)
- An Example of GoldenGate Extract Process Hang Problem Solving (Database)
- How to merge two pictures in Cacti (Linux)
- C # get the current screenshot (Programming)
- The FreeBSD zfs: failed with error 6 Error Resolution (Linux)
- Debian (Wheezy) were installed wxPython GUI development (Linux)
- How to choose the first programming language based on the life you want (Programming)
- swap space is insufficient cause OOM kill MySQL Case (Database)
- Moosefs Distributed File System Configuration (Server)
- Install DB2 V10 and Data Studio V3 under Linux (Ubuntu) environment (Database)
- MongoDB configuration in Ubuntu 14.04 (Database)
- To create a file in Linux directory by setfacl (Linux)
- Fedora 22 users to install the VLC media player (Linux)
- MySQL 5.5 on master-slave copy filter (Database)
- Oracle database online redo logs are several methods of recovery of deleted (Database)
- KUbuntu / Ubuntu 14.04 (downgrade) installed SVN 1.7 (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.