<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          [源碼] Gradle下載器-解決你gradle下載速度慢的問題

          共 2507字,需瀏覽 6分鐘

           ·

          2024-04-10 17:37

          文章 目錄

          • 前言

          • 一、簡要的代碼片段

          • 二、小工具獲取


          前言

          最近使用gradle編譯安卓的時候又出現(xiàn)了gradle下載超時的問題,經(jīng)常性地手動下載再復(fù)制到目錄,確實每次都做,每次都很繁瑣。

          于是有了今天這個軟件地誕生,先放個截圖。
          678efbc7dff7908c9fdc85cef2ddf890.webp

          該軟件會自動從騰訊鏡像下載gradle并自動放到該放的位置

          一、簡要的代碼片段

          小工具是使用jetpack compose for desktop編寫的,實際上也沒有什么技術(shù)含量,簡單地分享幾個代碼片段。

          1. 點擊按鈕時啟動下載線程

              Button({
          for (fName in gradleFileNames.split("\n")) {
          val fileName = fName.trim().removeSuffix(".zip")
          if (fileName.isNotEmpty()) {
          gradleFileDirName = getFolderName("https://services.gradle.org/distributions/${fileName}.zip")
          val src = "${gradleSrcUrl}/${fileName}.zip"
          val tarFilePath = "${gradleDir}\\wrapper\\dists\\${fileName}\\${gradleFileDirName}\\$fileName.zip"
          thread {
          Downloader.download(src, tarFilePath) { status ->
          println("callback status $status")
          appendTaskStatus(status)
          }
          }
          }
          }
          }) {
          Text("下載")
          }
          1. 讀取服務(wù)器文件的內(nèi)容

                  fun fileContent(fileUrl: String): String?{
          try {
          val conn = URI(fileUrl).toURL().openConnection()
          if(conn.contentType.startsWith("text/plain;")) { //text/plain; charset=utf-8
          val contentStream = conn.content as InputStream
          val content = contentStream.readAllBytes().decodeToString()
          return content
          }
          }catch (e: Exception){
          println("exception $e")
          }
          return null
          }
          1. 下載網(wǎng)絡(luò)二進制文件

              fun download(fileURL: String, savePath: String, callback: (DownloadStatus)->Unit){
          callback(DownloadStatus("新的任務(wù) from $fileURL, to $savePath", fileURL, savePath))
          try {
          val conn = URI(fileURL).toURL().openConnection()
          val inputStream = conn.getInputStream()
          val tarDir = File(File(savePath).parent)
          if(!tarDir.exists()){
          if(!tarDir.mkdirs()){
          callback(DownloadStatus("創(chuàng)建目標文件夾失敗 $tarDir", fileURL, savePath))
          return
          }
          }
          callback(DownloadStatus("開始下載 $fileURL", fileURL, savePath))
          val tmpPath = "$savePath.tmp"
          val tarFile = File(tmpPath)
          tarFile.createNewFile()
          val buff = inputStream.readAllBytes()
          val fos = FileOutputStream(tarFile)
          fos.write(buff)
          fos.close()
          inputStream.close()
          callback(DownloadStatus("下載完成 $savePath", fileURL, savePath))
          File(tmpPath).renameTo(File(savePath))
          } catch (e: Exception) {
          println("catch a exception $e")
          callback(DownloadStatus("下載失敗:$e", fileURL, savePath))
          }
          println("download finish $fileURL, to $savePath")
          }
          1. 其中DownloadStatus的定義(用于整合下載狀態(tài))

                  class DownloadStatus(
          val message: String,
          val fromUrl: String="",
          val toPath: String="",
          val error: String="",
          val date: String= Date().toString(),
          val attachedList: MutableList<DownloadStatus> = mutableListOf()
          )
          二、小工具獲取

          回復(fù)關(guān)鍵詞 “gradle下載器" 獲取。

          點擊左下角閱讀原文查看Gitee開源倉庫。

          瀏覽 35
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲三级久久 | 天天综合干 | 六月丁香深深爱 | 日韩成人AV一区二区 | 小h片|