一、环境配置
这里直接使用Android Studio,会在project的build.gradle中写相关内容,且在terminal中输入命令。
注:1. 如果用gradle发现不认这个命令,可以使用./gradlew,这里使用./gradlew。
二、task的基本使用
1. 使用
task hello {
doFirst {
println 'I want to say '
}
doLast {
println 'hello world'
}
}
用<<代替doLast
task hello <<{
println 'hello world'
}
hello.doFirst {
println 'I want to say '
}
运行task,-q是quiet,不会生成gradle的日志信息。
./gradlew -q hello
I want to say
hello world
给task添加描述、分组
task hello(group:"custom",description:"i am just a test")<<{
println("shuo")
}
使用命令./gradlew -q tasks查看所有task信息,我们可以看到

2. 任务依赖:dependsOn
task hello << {
println 'hello world'
}
task hello2(dependsOn: hello) << {
println 'hello world2'
}
还可以这样写:
task hello2<<{
println 'hello world2'
}
task hello <<{
println 'hello world'
}
hello2.dependsOn hello
运行hello2
./gradlew -q hello2
//hello world
//hello world2
那么不先定义hello,怎么办呢?加个单引号就行~
task hello2(dependsOn:'hello') <<{
println 'hello world2'
}
task hello <<{
println 'hello world'
}
task hello2<<{
println 'hello world2'
}
hello2.dependsOn 'hello'
task hello <<{
println 'hello world'
}
3. 终结者任务:finalizedby
task hello<<{
println("hello world")
}
task end<<{
println("the end")
}
hello.finalizedBy end
./gradlew -q hello
hello world
the end
4. 动态创建任务
一次创建hello0 hello1 hello2这三个任务,这样写:
3.times { number ->
task "hello$number" << {
println "hello world$number"
}
}
运行hello2
./gradlew -q hello2
hello world2
5.添加自定义属性->ext
task hello {
ext.data='hello world'
}
task hello2<<{
println hello.data
}
./gradlew -q hello2
hello world
6. 文件读取
task hello {
def files = file('./app/src/main').listFiles().sort()
files.each {
if (it.isFile()) {
ant.loadfile(srcFile: it, property: it.name)
println "*** $it.name ***"
println "${ant.properties[it.name]}"
}
}
}
结果
./gradlew -q hello
*** AndroidManifest.xml ***
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.qianqian.aa">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
7.方法的使用
task hello {
files('./app/src/main').each {
if (it.isFile()) {
ant.loadfile(srcFile: it, property: it.name)
println "*** $it.name ***"
println "${ant.properties[it.name]}"
}
}
}
File[] files(String path) {
file(path).listFiles().sort()
}
8. 默认任务
在上述代码上边添加defaultTasks即可,使用./gradlew -q命令,即可默认运行hello任务。
defaultTasks 'hello'
...
二、多任务调用
1.任务只能被调用一次
task base << {
println 'base'
}
task release(dependsOn: base) <<{
println 'release'
}
task debug(dependsOn: base) <<{
println 'debug'
}
./gradlew -q release debug
base
release
debug
2. 排除某个任务
这里排除base
./gradlew -q release -x base
release
3. 简化任务名
根据驼峰命名,每个单词的首字母,但这个命名要唯一区分这一个任务才可以,不然就要多打几个字母啦。
./gradlew -q dA r
base
debug
release
参考网站
https://dongchuan.gitbooks.io/gradle-user-guide-/build_script_basics/using_methods.html
网友评论