动态创建 task
def createFetchRnTask() {
def configJson = new JsonSlurper().parse(file("$rootDir/config.json"))
def configs = configJson.libConfig
for (def module : configs) {
def moduleName = module.name
task "fetch-$moduleName"(type: Copy) {
exclude '**/.gradle'
exclude '**/build'
exclude '**/gradle'
exclude '**/**.properties'
exclude '**/gradlew'
exclude '**/gradlew.bat'
exclude '**/**.iml'
exclude '**/.**'
from "$rootDir/../node_modules/$moduleName/android"
into "$rootDir/node_modules/$moduleName/android"
}
}
}
createFetchRnTask()
打包源码
// 打包源码
task sourcesZip(type: Zip) {
group = 'uploadMaven'
archiveClassifier = "sources"
from android.sourceSets.main.java.srcDirs
}
创建/打包项目 doc
请参考 创建/打包项目 doc
打包项目所有文件
// 打包项目所有文件
task packZip(type: Zip) {
group = 'uploadMaven'
archiveBaseName = project.name
archiveAppendix = VERSION
archiveClassifier = "all"
archiveExtension = "zip"
// 打包根路径
from project.projectDir.absolutePath
// 输出路径
destinationDir = project.projectDir.absoluteFile
exclude '**.zip'
exclude '**.iml'
exclude '**/**.iml'
exclude 'build/**'
exclude '.idea/**'
exclude '.gradle/**'
exclude 'gradle/**'
exclude '**/build/**'
exclude 'repo/**'
}
发布 aar
请参考Maven 发布 aar
文件拷贝
// ** 为通用通配符,某类文件:**.aar,某个路径下的 build 文件夹:**/build
// from "$rootDir/repo": 要复制的文件所在文件夹路径
// include 'xxx.aar' : 要复制的文件名
// exclude 'xxx.aar' : 要排除的文件名
// into "$rootDir/temp" : 复制到 "$rootDir/temp"
task copyTest(type: Copy){
from "$rootDir/repo"
// include '**/**.aar'
// exclude '**/**.zip**'
into "$rootDir/temp"
}
混淆加密(待测试)
task proguard(type: ProGuardTask, dependsOn: makeJar) {
// 输入路径
injars "build/libs/zxing.jar"
// 输出路径
outjars 'build/libs/zxing-proguard.jar'
// 添加混淆配置
configuration 'proguard-rules.pro'
}
执行 cmd 命令
// 第一种写法:
task test(type: Exec){
// 单个命令
commandLine "java", "-version"
// 多个命令
commandLine "java", "-version"
commandLine "java", "-version"
}
// 第二种写法:
task test(){
doLast {
exec {
// 单个命令
commandLine "java", "-version"
// 多个命令
commandLine "java", "-version"
commandLine "java", "-version"
}
}
}
// 第三种写法:
task test(){
doLast {
// 单个命令
def cmd = "git --version"
println cmd.execute().text
// 多个命令
def cmd1 = "git --version"
println cmd1.execute().text
def cmd2 = "git --version"
println cmd2.execute().text
}
}
小技巧
./gradlew xxx —profile
输出 xxx 任务的html编译报告,可在浏览器查看,并进行针对性优化
./gradlew xxx —offline
离线编译 xxx 任务
./gradlew xxx —rerun-tasks
输出 xxx 任务相关联的所有任务
./gradlew :app:dependencies —configuration releaseCompileClasspath
查看app模块release版本下的所有依赖
./gradlew :app:dependencyInsight —dependency support-annotations —configuration releaseCompileClasspath
查看app模块release版本下的support-annotations依赖引用 可以帮助跟踪和解决库版本冲突的任何问题
./gradlew —profile —rerun-tasks assembleDebug
查看gradle编译各任务详细耗时 完成后,打开/build/reports/profile目录下类型如下命名的文件:
profile-2020-08-12-20-22-44.html https://www.jianshu.com/p/7cea87f28868