[Windows] 下載Gradle到指定目錄

前言

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

一、軟件簡(jiǎn)介

自動(dòng)下載指定gradle版本到本地gradle的目標(biāo)目錄

二、下載地址

見(jiàn)評(píng)論區(qū)

三、截圖

image.png

四、代碼片段分享

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

  1. 點(diǎn)擊按鈕時(shí)啟動(dòng)下載線程
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ò)二進(jìn)制文件
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)建目標(biāo)文件夾失敗 $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()
    )
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容