AndroidStudio腳本命令指定AAR生成目錄與版本號

gradle_logo.png

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文件中配置以下:
  1. 生成所須要的目錄
  • //生成對應目錄
      task makeLibDir() {
      def outputPath = ROOT_PATH + '/aar_release'
      //腳本指定路徑
      if(project.hasProperty('OUT_PUT_DIR_PATH')){
          outputPath = "${OUT_PUT_DIR_PATH}"
      }
      //建立aar所輸出的目錄
      createDir(outputPath)
    }
    複製代碼
  1. 代碼參數塊
  • def createDir = {
        path ->
            File dir = new File(path)
            if (!dir.exists()) {
                dir.mkdirs()
            }
            println "Generate the corresponding directory path is" + dir.toString()
    }
    複製代碼
  1. 拷貝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 <-----------"
    }
    複製代碼
  1. 執行copyAAR
  • tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn(copyAAR)
    }
    複製代碼
  1. 使用命令執行**(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靈活打對應需求的版本則就略差一些。)

####先來張最近奧迪漏油事件皮一波,免費打打廣告 android

ad_publicity.jpg

好的,開始進入正題,首先靈活指定版本號,經過協定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來控制版本號與路徑。git

    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}"
    }
    
    //廣告的SDK
    def adPath
    if (!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 " + aarOutPath
    delete aarOutPath
    //循環刪除已存在的aar
    for (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 <----------"
    }
    複製代碼
相關文章
相關標籤/搜索