A build.gradle全局常量:
-
//根路径 def ROOT_PATH = rootProject.rootDir.path def GROUP = "com.genialsir.mobileads" def MOB_SDK_VERSION_NAME = "1.1.2"复制代码
B 在当前库项目的build.gradle文件中android{}中配置如下:
-
//自定义aar名称 android.libraryVariants.all { variant -> variant.outputs.all { def fileName = GROUP + "." + MOB_SDK_VERSION_NAME + ".aar" outputFileName = fileName } }复制代码
C 借助命令生成arr到指定的目录:
-
使用gradle -b **(代表当前Project或Module目录名)/build.gradle aarRelease复制代码
在对应build.gradle文件中配置如下:
- 生成所需要的目录
-
//生成对应目录 task makeLibDir() { def outputPath = ROOT_PATH + '/aar_release' //脚本指定路径 if(project.hasProperty('OUT_PUT_DIR_PATH')){ outputPath = "${OUT_PUT_DIR_PATH}" } //创建aar所输出的目录 createDir(outputPath)}复制代码
- 代码参数块
-
def createDir = { path -> File dir = new File(path) if (!dir.exists()) { dir.mkdirs() } println "Generate the corresponding directory path is" + dir.toString()}复制代码
- 拷贝AAR
-
task copyAAR(type: Copy) { println "----------> ARR copy start <-----------" def buildPath = ROOT_PATH + '/containerad/build/outputs/aar' //AAR所在的编译路径 from buildPath //AAR所拷贝的目录 def outputPath = ROOT_PATH + '/aar_release' println "outputPath = " + outputPath into outputPath println "----------> ARR copy succeeded <-----------"}复制代码
- 执行copyAAR
-
tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn(copyAAR)}复制代码
- 使用命令执行**(gradle -b 目录名/build.gradle aarRelease)**并发布AAR
-
task aarRelease(type: Copy) { //任务开始 println "----------> AAR Release Task Start <----------" //AAR所在的编译路径 def buildPath = ROOT_PATH + '/containerad/build/outputs/aar' //AAR输出路径 def aarOutPath = ROOT_PATH + "/aar_release" //广告的SDK def adPath = buildPath + GROUP + "." + MOB_SDK_VERSION_NAME + ".aar" //封装数组 def modulePaths = [adPath] //删除AAR输出目录 println "Delete -> " + aarOutPath delete aarOutPath //循环删除各个AAR编译目录 for (path in modulePaths) { println "Delete -> " + path delete path } //执行编译AAR脚本(原生脚本) dependsOn 'assembleRelease' //循环拷贝AAR到ARR输出目录 for (path in modulePaths) { //显示开始拷贝 println "-> Start of current copy <-" //当前目录 println "From : " + path from path //拷贝至 aarOutPath println "Into : " + aarOutPath into aarOutPath; //显示当前拷贝结束 println "-> End of current copy <-" } //任务结束 println "----------> AAR Release Task End <----------"}复制代码
(上面内容写的有些繁琐与重复,灵活性相对差些,如果需要外部指定对应版本与目录,以及通过git灵活打对应需求的版本则就略差一些。)
####先来张最近奥迪漏油事件皮一波,免费打打广告
好的,开始进入正题,首先灵活指定版本号,通过协定OUT_PUT_AAR_VERSION_SUFFIX来指定所需版本。
//自定义aar名称android.libraryVariants.all { variant -> variant.outputs.all { def fileName if (!project.hasProperty('OUT_PUT_AAR_VERSION_SUFFIX')) { fileName = GROUP + "." + MOB_SDK_VERSION_NAME + ".aar" }else{ fileName = GROUP + "." + "${OUT_PUT_AAR_VERSION_SUFFIX}.aar" } outputFileName = fileName }}复制代码
指定对应的生成目录以及版本号
使用默认路径命令执行(gradle -b containerad/build.gradle aarRelease) 脚本指定版本号与对应目录(gradle assemble -P OUT_PUT_AAR_VERSION_SUFFIX=1.1.2 -P OUT_PUT_DIR_PATH=H:\AndroidProject\ADPlugIn\OnlyLadyAD\ad_release_path aarRelease)复制代码
-
通过协定好的OUT_PUT_DIR_PATH与OUT_PUT_AAR_VERSION_SUFFIX来控制版本号与路径。
task aarRelease(type: Copy) {//任务开始println "----------> AAR Release Task Start <----------"//执行编译AAR脚本(原生脚本)dependsOn 'assembleRelease'//AAR所在的编译路径def buildPath = ROOT_PATH + '/containerad/build/outputs/aar/'//AAR输出默认路径def aarOutPath = ROOT_PATH + '/aar_release'//脚本指定路径if(project.hasProperty('OUT_PUT_DIR_PATH')){ aarOutPath = "${OUT_PUT_DIR_PATH}"}//广告的SDKdef adPathif (!project.hasProperty('OUT_PUT_AAR_VERSION_SUFFIX')) { //默认版本 adPath = buildPath + GROUP + "." + MOB_SDK_VERSION_NAME + ".aar"}else{ //脚本指定版本 adPath = buildPath + GROUP + "." + "${OUT_PUT_AAR_VERSION_SUFFIX}.aar"}//封装数组def modulePaths = [adPath]//当前AAR输出目录println "The current aar output directory is " + aarOutPathdelete aarOutPath//循环删除已存在的aarfor (path in modulePaths) { println "Delete ---> " + path delete path}//循环拷贝AAR到ARR输出目录for (path in modulePaths) { //显示开始拷贝 println "-> Start of current copy <-" //当前目录 println "From : " + path from path //拷贝至 aarOutPath println "Into : " + aarOutPath into aarOutPath; //显示当前拷贝结束 println "-> End of current copy <-"}dependsOn 'assembleRelease'//任务结束println "----------> AAR Release Task End <----------"}复制代码