Android under Gradle compilation code and confusion

Source: Internet
Author: User

1. What is Gradle?

Baidu Encyclopedia said: Gradle is based on the Groovy language, oriented to Java applications, based on the DSL grammar of the automated building tools.

Features:
    • Gradle support for multi-project construction is excellent, and engineering relies on Gradle's first citizens.
    • Gradle support for local builds.
    • Support multi-mode dependency management: Includes jars or dirs from maven remote repositories, nexus, ivy warehouses, and local file systems
    • Gradle is the first built-in integration tool that has a good compatibility dependency with Ant, Maven, ivy, and integration.
    • Easy migration: Gradle works with any structure (Gradle can adapt to any structure you have.). You can build original and gradle projects in parallel on the same development platform. It is often required to write related tests to ensure the similarity of the plug-ins developed, and this migration can be less disruptive and as reliable as possible. This is also the best practice for refactoring.
    • The overall design of Gradle is a language-oriented rather than a rigid framework
what the Gradle offers
    • A switchable, contract-based build framework like Maven that never locks you (contract is better than configuration)
    • Powerful support for multi-project construction
    • Powerful dependency management (Apache Ivy based) for maximum convenience to build your project
    • Language for dependency based programming
    • Fully support existing MAVEN or Ivy warehouse infrastructure
    • Support for transitive dependency management, without the need for remote warehouses and pom.xml and Ivy profiles
    • Built on groovy scripting with a build script written in the groovy language
    • With a wide range of domain models support your build a rich domain model for describing your build.

2, the installation of Gradle

Gradle's official website download: https://gradle.org/downloads

If the above download is slow or inaccessible, you can download it here: http://www.androiddevtools.cn/

Androidstudio the words, the direct decompression, and then the directory in the guide is OK.

Eclipse to, after decompression to configure environment variables, the specific Baidu is good, online a lot of information.

3. Analysis

 Here's an example: This is the syntax before gradle2.0 (2.0 syntax changed)

1Apply plugin: ' Android '2 3 Buildscript {4 Repositories {5 mavencentral ()6     }7 Dependencies {8Classpath ' com.android.tools.build:gradle:0.7.3 '9Classpath ' net.sf.proguard:proguard-gradle:4.10 'Ten     } One } A  - Dependencies { -Compile Filetree (dir: ' Libs ', include: [' *.jar ']) the } -  - Android { -Compilesdkversion 21 +Buildtoolsversion "21.1.2" -  + Defaultconfig { AMinsdkversion 9 at     } - } -  - //type: Can be used as what tool to use, DependsOn: Perform the action after Compilereleasejava execution -Task Copyassets (type:copy, dependsOn: ' Compilereleasejava ') { -From ' Assets ' inInto ' build/classes/release/assets ' - } to  +Task Genjar (Type:jar, dependsOn: ' Copyassets ') { -From Filetree (dir: ' Build/classes/release ') theArchivename = ' Gp_android_sdk_unproguard.jar ' * } $ Panax Notoginseng Task Progjar (Type:proguard.gradle.ProGuardTask, Dependson:genjar) { -Outjars ' Build/gp_android_sdk.jar ' theLibraryjars system.getenv ("android_home") + "/platforms/android-19/android.jar" +  A     //specific need to keep live class theConfiguration ' Sdk_proguard.pro ' +}

There are a lot of type:gradle syntax in line 26: What copy, Delete, Jar are. Specific open source documents to see Gradle: https://gradle.org/docs/current/javadoc/index.html

such as copy: Copy from Src/main/doc directory to Build/target/doc

task Copydocs (type:copy) {     ' src/main/doc '     build/target/doc ' }

is not very simple. Other basic Baidu look at other people's documents, here is a recommendation: http://www.stormzhang.com/android/2014/02/28/android-gradle/

The following is a mix of words:

buildtypes {        release {            false            proguardfiles getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.txt '        }    }

The official writes the confusion in the location of the above code, but if you confuse and export the jar package at the same time, it is available: (There is no other way to try it)

Task Genjar (Type:jar, dependsOn: ' Copyassets ') {    ' build/classes/release ')    = ' gp_android_ Sdk_unproguard.jar '}task progjar (Type:proguard.gradle.ProGuardTask, Dependson:genjar) {    ' build/gp_ Android_sdk.jar '    libraryjars system.getenv ("android_home") + "/platforms/android-19/android.jar"    //  Keep class    configuration ' Sdk_proguard.pro ' is required

In fact, Gradle compile will generate the jar package: in the build directory under the Exploded-aar (different versions of the Gradle will not be the same) back to build: Classes.jar is the project's jar package, so you put the module Gradle compile generated Classes.jar is It's the jar bag you want.

Here's a gradle2.0 example:
1 //The script should be compiled with gradle2.0 above2 3Apply plugin: ' Com.android.application '4 5 Buildscript {6 Repositories {7 Jcenter ()8     }9 Dependencies {TenClasspath ' com.android.tools.build:gradle:1.0.0 ' OneClasspath ' net.sf.proguard:proguard-gradle:4.10 ' A     } - } -  the Dependencies { -Compile Filetree (dir: ' Libs ', include: [' *.jar ']) - } -  + Android { -Compilesdkversion 21 +Buildtoolsversion "21.1.2" A  at Defaultconfig { -Minsdkversion 9 -     } - } -  -Task Copyassets (type:copy, dependsOn: ' Compilereleasejava ') { inFrom ' Src/main/assets ' -Into ' build/intermediates/classes/release/assets ' to } +  -Task Genjar (Type:jar, dependsOn: ' Copyassets ') { theFrom Filetree (dir: ' Build/intermediates/classes/release ') *Archivename = ' Gp_android_sdk_unproguard.jar ' $ }Panax Notoginseng  - Task Progjar (Type:proguard.gradle.ProGuardTask, Dependson:genjar) { theOutjars ' Build/gp_android_sdk.jar ' +Libraryjars system.getenv ("android_home") + "/platforms/android-19/android.jar" A  the     //specific need to keep live class +Configuration ' Sdk_proguard.pro ' - } $  $Task Deleteoldjar (dependsOn: ' Progjar ')) { -Delete '.. /app/libs/gp_android_sdk.jar ' - } the  -Task Copyjar (type:copy, dependsOn: ' Deleteoldjar ') {WuyiFrom ' Build/gp_android_sdk.jar ' theInto '. /app/libs ' - } Wu  -Task SDK (dependsOn: ' Copyjar ') { About}

Android under Gradle compilation code and confusion

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.