Android Studio Gradle Plugin User Guide Selective translation

Source: Internet
Author: User
Tags maven central

Original

Http://tools.android.com/tech-docs/new-build-system/user-guide

Build file build.gradle

Basic Project1. The simplest configuration:
Apply plugin: ' java '

Use your own J-Ava inserts to compile Java with Gradle

2. The most common Android configuration:
Buildscript {    repositories {        mavencentral ()    }    dependencies {        classpath ' Com.android.tools.build:gradle:0.11.1'    }}apply plugin: ' Android ' Android {         "19.0.0"}


Repositories using MAVEN central repository
Dependencies relies on Android plugin

Apply plugin using Android plugin
Android {} config android plugin parameter, this thing DSL entrance

Tip: You need to local.properties set the sdk.diR attribute that represents the ANDROID SDK path, or use the ANDROID_HOME environment variable

Project structure1. Default directory structure:
    • src/main/Release Source
    • src/androidtest/put the test code
2.java and Android plugin common source directory:
    • java/
    • resources/
3.android Plugin Source Directory
    • Androidmanifest.xml
    • res/
    • assets/
    • aidl/
    • rs/
    • jni/
Configuration Path

Two ways to modify the Java plugin default path

Sourcesets {    Main {        java {            srcdir ' Src/java '        }        Resources {            srcdir ' src/resources '        }    }}
sourcesets {    [' Src/java ']    [' src/ Resources ']}
Change the path of Android plugin to the old form
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 ')}}
Common tasks for building tasks
    • Assemble Merge output
    • Check Run all checks
    • build run assemble and check
    • Clean Empty output

These tasks do not do anything themselves, specific things to plug in, similar to the strategy model

Java Tasks
    • Assemble
      • Jar
        This task creates the output.
    • Check
      • Test
        This task runs the tests.

Jars are directly or indirectly dependent on other tasks, such as classes compiling Java code

Concrete Look http://gradle.org/docs/current/userguide/java_plugin.html

Android Task
    • Assemble
      The task to assemble the output (s) of the project
    • Check
      The task to run all the checks.
    • Connectedcheck
      Runs checks that requires a connected device or emulator. They'll run on all connected devices in parallel.
    • Devicecheck
      Runs checks using APIs to connect to remote devices. This is used on CI servers.
    • Build
      This task does both assemble and check
    • Clean
      This task cleans the output of the project

More Connectedcheck and Devicecheck check the device connection

tip:gradle Support Task Name shorthand: For example, use Gradle AR instead of Gradle assemblerelease

Customizing the build processManifest entries
Android {        "19.0.0" Defaultconfig            {        "2.0"         +}    }         
Default value
Property Name Default value in DSL object Default value
Versioncode -1 Value from manifest if present
Versionname Null Value from manifest if present
Minsdkversion -1 Value from manifest if present
Targetsdkversion -1 Value from manifest if present
ApplicationID Null Value from manifest if present
Testapplicationid Null ApplicationID + ". Test"
Testinstrumentationrunner Null Android.test.InstrumentationTestRunner
Signingconfig Null Null
Proguardfile N/A (set only) N/A (set only)
Proguardfiles N/A (set only) N/A (set only)
Dependency management relies on local packages
dependencies {    compile files (' Libs/foo.jar ')}android {    ...}

Compile files will be added to the classpath and packaged into the APK

Optional compile configuration

    • Compile:main Application
    • Androidtestcompile:test Application
    • Debugcompile:debug Build Type
    • Releasecompile:release Build Type.
dependent on the remote package
repositories {    mavencentral ()}dependencies {    compile ' Com.google.guava:guava:11.0.2'}android {    ...}

First add the repository, and then dependencies in the form of Maven or Ivy to declare artifacts, you can

Configure additional MAVEN Libraries
Repositories {    maven {        "http://repo.mycompany.com/maven2"    }}

Http://gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html

Multi-Project Construction

Example:

myproject/+ app/+ libraries/+ lib1/+ lib2/

APP,LIB1,LIB2 is a gradle project.
Gradle quote them this way: app:libraries:lib1:libraries:lib2
Each project has a build.gradle file
root directory has Settings.gradle
The structure is as follows:

myproject/| Settings.gradle + app/| Build.gradle + libraries/+ lib1/| Build.gradle + lib2/| Build.gradle

In order to indicate that the current directory is a gradle directory, you need to have a Settings.gradle file with the following contents:

Include ': App ', ': Libraries:lib1 ', ': Libraries:lib2 '

Apps depend on other projects, so the dependencies are written

dependencies {    Compile project (': Libraries:lib1 ')}
Library project

Above: Libraries:lib1:libraries:lib2 can be a Java project: The app uses their jar package,

But if these libraries are going to access Android APIs or other Android resources, they can't be normal Java projects, they have to be Android library Projects

Create Android Library Projects
Buildscript {    repositories {        mavencentral ()    }    dependencies {        classpath ' Com.android.tools.build:gradle:0.5.6'    }}apply plugin: ' Android-library ' Android {    $} 

Use Android-libraray plugin other similar to normal Android projects

Output. aar files, including compiled code (. jar/.so) and resource files (manifest, res, assets)

Reference Library
dependencies {    Compile project (': Libraries:lib1 ')    Compile project (': Libraries:lib2 ')}
Publish Library
Android {    "debug"}

The release version is only released by default and can be modified by the code above

If there is a variant (which will be mentioned later), add the variant name

Android {    "Flavor1debug"}  
Publish all Versions
Android {    Publishnondefault true}
Referencing other publications for artifact
Dependencies {    Flavor1compile project (path: ': Lib1 ', configuration: ' Flavor1release ')    Flavor2compile Project (path: ': Lib1 ', configuration: ' Flavor2release ')}  
Build variants

Variants: similar to regular and premium editions

Android {    ...    .. productflavors {        Flavor1 {...        }        Flavor2 {            ...}}    }

Build Type + Product Flavor = Build Variant

For example

    • Flavor1-debug
    • Flavor1-release
    • Flavor2-debug
    • Flavor2-release

Android Studio Gradle Plugin User Guide Selective translation

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.