1.3 通过命令行执行 Gradle任务

“Gradle wrapper”指的是Android应用根目录下的gradlew.bat脚本文件,这里的”w”指的就是”wrapper”.
Gradle wrapper可以使我们不必安装就可以运行Gradle.通过应用根目录下gradle/wrapper文件夹里的gradle-wapper.jargradle-wrapper.properties来启动程序.
gradle-wrapper.properties里的内容如下:

#Mon Dec 28 10:00:20 PST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

distributionUrl这句是说wrapper将会下载安装2.14.1版本的Gradle.第一次运行之后,Gradle会被缓存在zipStoreBase路径下的zipStorePath文件夹中,可供今后再执行Gradle任务的时候使用.

要想使用wrapper,在Android Studio的Terminal命令行输入gradlew.bat,会下载上面给出的distributionUrl地址里的Gradle版本[1].

万事俱备,现在可以在命令行执行gradle任务了.
在命令行输入gradlew tasks可以就查看所有可执行的任务:

D:\Androidworkspace\MobileSafe>gradlew tasks
Incremental java compilation is an incubating feature.                           
:tasks                                                                              

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Android tasks         
-------------         
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for each variant.
sourceSets - Prints out all the source sets defined in this project.

Build tasks
-----------
assemble - Assembles all variants of all applications and secondary packages.
assembleAndroidTest - Assembles all the Test applications.
assembleDebug - Assembles all Debug builds.
assembleRelease - Assembles all Release builds.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
clean - Deletes the build directory.
compileDebugAndroidTestSources
compileDebugSources
compileDebugUnitTestSources
compileReleaseSources
compileReleaseUnitTestSources
mockableAndroidJar - Creates a version of android.jar that's suitable for unit tests.

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'MobileSafe'.
components - Displays the components produced by root project 'MobileSafe'. [incubating]
dependencies - Displays all dependencies declared in root project 'MobileSafe'.
dependencyInsight - Displays the insight into a specific dependency in root project 'MobileSafe'.
help - Displays a help message.
model - Displays the configuration model of root project 'MobileSafe'. [incubating]
projects - Displays the sub-projects of root project 'MobileSafe'.
properties - Displays the properties of root project 'MobileSafe'.
tasks - Displays the tasks runnable from root project 'MobileSafe' (some of the displayed tasks may belong to subprojects).

Install tasks
-------------
installDebug - Installs the Debug build.
installDebugAndroidTest - Installs the android (on device) tests for the Debug build.
installRelease - Installs the Release build.
uninstallAll - Uninstall all applications.
uninstallDebug - Uninstalls the Debug build.
uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build.
uninstallRelease - Uninstalls the Release build.

Verification tasks
------------------
check - Runs all checks.
connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
connectedCheck - Runs all device checks on currently connected devices.
connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices.
deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers.
deviceCheck - Runs all device checks using Device Providers and Test Servers.
lint - Runs lint on all variants.
lintDebug - Runs lint on the Debug build.
lintRelease - Runs lint on the Release build.
test - Run unit tests for all variants.
testDebugUnitTest - Run unit tests for the debug build.
testReleaseUnitTest - Run unit tests for the release build.

Other tasks
-----------
clean
extractProguardFiles
jarDebugClasses
jarReleaseClasses
transformResourcesWithMergeJavaResForDebugUnitTest
transformResourcesWithMergeJavaResForReleaseUnitTest

To see all tasks and more detail, run gradlew tasks --all

To see more detail about a task, run gradlew help --task <task>

BUILD SUCCESSFUL

执行多个任务用空格(space)将他们分隔开就行[2]:

gradlew lint assembleDebug

想排除一个任务可以添加一个-x flag:

gradlew assembleDebug -x lintDebug

在命令行按照驼峰规则(camel-case)输入任务名的缩写,只要缩写是唯一的,就可以执行该任务,比如:

gradlew androidDependencies

gradlew anDe

执行效果是一样的!

如果构建文件没有命名为build.gradle,使用-b flag来明确构建文件名:

gradlew -b app.gradle

1.5 Adding Java Library Dependencies会讨论构建文件里的依赖库
4.3 Excluding Tasks讨论从构建过程中排除任务

  1. 在命令行运行gradle命令可能需要开启gradle的守护进程,我是在开启了守护进程之后才好用的,开启守护进程也会改善gradle的启动和执行时间,具体方法如下:
    C:\Users<username>\.gradle文件夹下gradle.properties文件(如果没有就用记事本自行创建一个)中写上org.gradle.daemon=true就可以开启gradle的守护进程.

  2. 相同的任务名写两遍只会执行一次

    原文作者:GuoYiheng
    原文地址: https://www.jianshu.com/p/682af083d754
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞