task makeJar(type:org.gradle.api.tasks.bundling.Jar) {
//指定生成的jar名
baseName 'drisk'
//从哪里打包class文件
from('build/intermediates/classes/debug/com/ijm/drisk')
//打包到jar后的目录结构
into('com/ijm/drisk')
//去掉不需要打包的目录和文件
//exclude('ui', 'uuid')
//过滤不需要打入jar包的文件(以ui,uuid,M开头的目录和文件)
exclude('ui', 'uuid', 'M')
//指定打包的class
//include "com/xxx/drisk/*.class"
}
task copyFile(type: org.gradle.api.tasks.Copy){
from('build/libs/drisk.jar', 'libs/xxx.jar', 'src/main/libs')
into('build/sdk')
}
task deleteSdk(type: org.gradle.api.tasks.Delete){
delete('build/sdk')
}
makeJar.dependsOn(build)
copyFile.dependsOn(deleteSdk)
dependsOn
表示在自己之前先执行这个方法
uiTest.mustRunAfter unitTest
执行``gradle uiTest unitTest -q```会输出如下结果,说明unitTest是在uiTest之前运行的。
Compiled
Unit test
UI test
https://www.jianshu.com/p/d6a3ea60ccf3
repositories {
flatDir {
dirs 'libs'
}
}
compile(name: 'mergedsdk-release', ext: 'aar')
举个例子
task makeJar(type: org.gradle.api.tasks.bundling.Jar) {
from('build/intermediates/classes/release', 'cacheclass')
destinationDir = file('libs')
baseName 'mysdk'
}
build.dependsOn(makeJar)
干涉arr生成流程.
task copyFile(type: org.gradle.api.tasks.Copy) {
from('cacheclass')
into('build/intermediates/classes/release')
// from('cacheclass')
// into('build/intermediates/classes/release')
}
task deleteSdk(type: org.gradle.api.tasks.Delete) {
delete('build/intermediates/classes/release')
}
//makeJar.dependsOn(build)
//copyFile.dependsOn(deleteSdk)
//copyFile.dependsOn(build)//通过执行copyFile任务触发 cooyFile在 build之后执行,无法拦截 arr的生成。
//build.dependsOn(copyFile)// copyFile先与build执行。 通过执行build触发
同时执行复制和删除
task deleteSdk(type: org.gradle.api.tasks.Delete) {
delete('build/intermediates/classes/release')
}
task publishSDK2Demo3() {
copy {//不根据根目录定位,容易找不到路径
from("${project.rootDir}/mergeincludearr/build/outputs/aar/mergeincludearr-release.aar")
into("${publishDir}/sdklib/libs")
}
delete("d:/1.txt")
}
task publishSDK2Demo(type: org.gradle.api.tasks.Copy) {
copy {//不根据根目录定位,容易找不到路径
from("${project.rootDir}/mergeincludearr/build/outputs/aar/mergeincludearr-release.aar")
into("${publishDir}/sdklib/libs")
}
delete("${publishDir}/app/build")
delete("${publishDir}/.gradle")
delete("${publishDir}/sdk/build")
delete("${publishDir}/app/app.iml")
delete("${publishDir}/sdklib/sdklib.iml")
print "删除缓存完成 ${publishDir}"
// copy {
// print "\n copy被执行!"
// from 'build/outputs/arr/mergeincludearr-release.aar'
// into "${publishDir}/sdklib/libs"
//
//
// rename {
// 'xsdk.arr'
// }
// }
}
task copy(type: Copy) {
// end::no-description[]
description 'Copies the resource directory to the target directory.'
// tag::no-description[]
from 'resources'
into 'target'
include('**/*.txt', '**/*.xml', '**/*.properties')
}
表示从resources复制以txt, xml .properties结尾的文件到target目录
依赖的另外一种追加方式
依赖到assembleRelease的办法
如果直接在build.gradle里面写xxx.dependsOn(assembleRelease)就会报错,could not get unknown property 'assembleDebug' for project
方法1:
tasks.whenTaskAdded { task ->
// print " task ${task.name} add \n"
if (task.name == 'assemble') {
// if (task.name == 'assembleRelease') {
task.dependsOn 'deleteDemoTask'
}
}
方法2:
task A_BuildDex(dependsOn: "assembleDebug") {
// F:\src\git_project\fuck\testapp\build\intermediates\dex\debug\mergeDexDebug\out
doLast{
println("copy debug...")
copy {
// from "build\\intermediates\\external_libs_dex\\debug\\mergeExtDexDebug\\out"//纯模块里面再引用的dex 显然不符合需求
from "build\\intermediates\\dex\\debug\\mergeDexDebug\\out"
into "../assets/"
}
}
}
方法3
task renameApk(type:Copy){
//something
dependsOn 'assembleRelease'
}
无法依赖到build的错误
![](https://i-blog.csdnimg.cn/blog_migrate/65b234ce9b2f354b75b3427904296d7b.png)
xxx
这是bug,我直接不依赖到build依赖到assembleDebug了。