RE:从零开始学gradle(二)

接着上一章的继续。  上一章传送门:RE:从零开始学gradle(一)

运行基本构建任务

在Terminal终端下输入以下命令:

 gradlew tasks
会列出所有的task列表

------------------------------------------------------------
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 'MyView'.
components - Displays the components produced by root project 'MyView'. [incubating]
dependencies - Displays all dependencies declared in root project 'MyView'.
dependencyInsight - Displays the insight into a specific dependency in root project 'MyView'.
help - Displays a help message.
model - Displays the configuration model of root project 'MyView'. [incubating]
projects - Displays the sub-projects of root project 'MyView'.
properties - Displays the properties of root project 'MyView'.
tasks - Displays the tasks runnable from root project 'MyView' (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.
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
jarDebugClasses
jarReleaseClasses
newTask
printProperties
transformResourcesWithMergeJavaResForDebugUnitTest
transformResourcesWithMergeJavaResForReleaseUnitTest

To see all tasks and more detail, run gradlew tasks --all  //查看更多细节

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

BUILD SUCCESSFUL
gradle打包命令:

 gradlew assembleDebug
该任务会将项目打包成apk和调试版本的应用程序。默认输出路径是:MyApp/app/build/outputs/apk。

注意事项:Gradle针对一些任务命名规则采用了驼峰命名法,并缩写了任务名称。eg.可以用assDeb来替代assembleDebug,甚至用aD来替代。但是不能有相同的缩写名称。
基本任务
gradle引用的android插件提供了四个最基本的任务:assemblecleancheckbuild.分别用来实现汇编、清除、检查和构建这四个基本功能。

默认情况下,assemble会调用assembleDebug和assembleRelease两种模式,如果添加其他模式,当指定特定模式后,该命令就会执行该模式下的对应命令。
当然,对于这些基本命令,AS中不一定非要通过命令行来执行。还可以通过UI视图来执行:


在这个窗口中,可以通过双击任务名称来运行任何任务。并且可以在Gradle Console窗口中跟踪任何正在运行的任务进度。

项目属性

有三种常用的设置属性方法:

1.ext块

ext {
test1 = 'test1122'
}
2.直接在gradle.properties文件中声明

test=test2222
3.使用 -P命令

gradlew printProperties -Pcmd='test78787'
默认任务

在顶层的build.gradle文件中添加以下代码:

defaultTasks 'clean', 'assembleDebug'
如果你没有添加额外命令的话,你运行GW,那么就会默认运行clean assembleDebug这两个task.

执行以下命令,可以查看默认任务:

$ gradlew tasks | grep "Default tasks"

依赖管理

依赖管理是Gradle的核心领域之一。在极端情况下,依赖一个包、model等只需要一行代码就可以实现。


存储库

通常讨论的依赖指的是外部依赖,比如说友盟推送SDK、okhttpJar包等等。使用中央代码库就可以解决这样的问题。Gradle支持三种不同的存储库:Maven、lvy和静态文件目录。在执行阶段时从存储库获取依赖关系。Gradle还会保留一份本地缓存,所以它至少需要下载一次。

依赖关系有三个元素标识:组、名称和版本。如下:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //添加所有lib包下面的jar包到项目中
    compile 'com.android.support:appcompat-v7:23.1.1'
    //这种方式是基于groovy语法的,所以其完整的表述应该是这样的:compile group: 'com.android.support', name: 'appcompat-v7', version:'23.1.1'
}
依赖关系中必填的是名称,组和版本是选填元素。尽量全部填写,因为版本差异有可能会导致构建项目直接失败。

依赖model如下:

        compile project(':UMengPushSDK')
        compile project(':Danmaku') //依赖模块
还可以采用aar文件形式依赖。aar是Gradle将library中所有代码资源进行打包的结果。生成在model/build/outputs/aar/文件夹下面,一般会生成release版和debug版。看个人需要而定。然后可以在app目录下新建一个叫aars的文件夹,将aar拷入其中。再到根build.gradle中添加以下代码:

allprojects {
    repositories {
        jcenter()

        flatDir{//定义aar路径
            dirs 'aars'
        }
    }
}
然后重新写依赖:

dependencies {
    //    compile project(':UMengPushSDK')
    //    compile project(':Danmaku') //依赖模块
    compile(name: 'Danmaku-debug', ext: 'aar')//在aars目录下,添加一个叫做Danmaku-release的文件,且其后缀是aar的作为依赖
    compile(name: 'UMengPushSDK-debug', ext: 'aar')
}
针对jar包的依赖,AS默认已经写好,如下:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //添加所有lib包下面的jar包到项目中
    //    compile files('libs/butterknife-7.0.1.jar')//单独添加jar包到项目中
}
动态版本
在某些情况下,开发人员需要获取到最新版本的依赖关系,那么依赖方式可以用以下形式来实现:

dependencies {
  compile 'com.android.support:support-v4:22.2.+'
  compile 'com.android.support:appcompat-v7:22.2+'
  compile 'com.android.support:recyclerview-v7:+'
}
第一行是告诉Gradle获取最新的补丁版本;

第二行是告诉Gradle获取每个新的次要版本;

第三行是告诉Gradle获取最新的版本。

注:动态版本是不提倡的,因为新的版本的未知性会给程序带来一定的不稳定性。


以上是简述了Gradle中的任务和依赖的基本用法。具体实践的时候,还是要多多积累。

未完待续~~





猜你喜欢

转载自blog.csdn.net/shirakawakanaki/article/details/52921711