An introductory guide to using Gradle to build app projects in Android _android

Source: Internet
Author: User
Tags maven central

Gradle is a new build system introduced in Android development because the new build system is mainly for the following purposes:

1. Facilitate reuse of code and resources
2. Build multiple versions of the APK more simple, whether it is to build different apk for multi-channel or to build different environments apk (debug,release)
3. Convenient configuration, extension, custom build process
4. Good IDE Integration

Why Choose Gradle?
Gradle mainly have the following a few:

1. Use domain-driven language (DSL) to describe building logic
2. Build scripts use groovy to make it easy to customize the build logic
3. Built-in dependency management system, using MAVEN or ivy
4. Very flexible. You can use best practices but do not enforce
5. Good IDE Support

The Gradle project uses the Build.gradle file in the root directory of the project to configure
A build.gradle of the simplest Java project is as follows

Apply plugin: ' java ' 

This means that the change project will use the Gradle Java plug-in, which is provided by default Gradle, and Java Plug-ins provide the ability to build Java applications and run 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 
  buildtoolsversion "19.0.0" 
} 

The Android build file contains 3 parts:
1.buildscript {...} configures the code that drives the build process, meaning that the configuration in Buildscript only works during the build process, which is the configuration used by the build program.
The above code declares that our builder uses the MAVEN central repository, and that there is a classpath dependency that points to a MAVEN library. This MAVEN library is the Android Gradle plugin, the version number is 0.11.1.

2. Then you use this Android plug-in, just as you did with the Java plugin.

3. Finally, Android {...} Configure all the parameters required for the Android project build.
By default, you only need to specify which version of the SDK compilation to use Compilesdkversion and buildtoolsversion,compilesdkversion specify, and Buildtoolsversion specify which version of the build tool to use.
Note the point:
1. Just use the Android plug-in and use the Java plugin to make 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 the principle of using conventions over configuration, the simplest way is to use a default directory structure. Of course, the directory structure can be modified by itself.

By default, the Android Gradle Project code is in the SRC directory, SRC will have two directory main and Androidtest, where the main directory is the project code, Androidtest directory is the test code.

src/main/
src/androidtest/

Under Main and Androidtest directories, there are no different directories for different types of code.
For Java and Android Gradle Plug-ins, Java code and Java resources correspond to the Java directory and resource directory respectively. The Android plugin also requires a number of other directories and files, such as

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

Now let's take a look at how to change these default configurations.

The Android project, by default, Java and resource files are added to the Src/main/java and Src/main/res directories, with the following code in Build.gradle file, andorid{}. You can place the Java and resource files in the Src/java and src/resources directories. (Note that if you change it in the Gradle file, you just need to sync again, Gradle will automatically create a new directory and move the corresponding file to the new directory)

Sourcesets { 
  Main { 
    java { 
      srcdir ' Src/java ' 
    } resources 
    { 
      srcdir ' src/resources ' 
    } 
  } 
} 

A simpler formulation is

sourcesets { 
  main.java.srcDirs = [' Src/java '] 
  main.resources.srcDirs = [' Src/resources '] 
} 

In the array, you can specify multiple directories so that you can place your 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 ') 
  } 
} 

The main code here is actually using the default value of the Android Gradle, and Androidtest no longer uses the default Androidtest directory, but instead uses the tests directory.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.