【问题标题】:Compile Jar from Url in Gradle从 Gradle 中的 Url 编译 Jar
【发布时间】:2014-06-20 04:56:36
【问题描述】:

是否可以这样做:

compile files('http://ho.st/jar/MyLibrary.jar')

在 Gradle/Android Studio 中?

可能的优势:

  1. 始终获取最新版本(如果您必须手动下载和复制,您并不总是拥有最新版本)
  2. 即使在库未发布到 maven 存储库时也可以工作

还是每次都要下载复制?

【问题讨论】:

    标签: android gradle android-studio


    【解决方案1】:

    这对我有用:

    def urlFile = { url, name ->
        File file = new File("$buildDir/download/${name}.jar")
        file.parentFile.mkdirs()
        if (!file.exists()) {
            new URL(url).withInputStream { downloadStream ->
                file.withOutputStream { fileOut ->
                    fileOut << downloadStream
                }
            }
        }
        files(file.absolutePath)
    }
    dependencies { //example
        compile urlFile('https://github.com/java-native-access/jna/blob/4.2.2/lib/native/android-arm.jar?raw=true', 'jna-android-arm')
    }
    

    删除构建目录后会下载一个新的副本

    【讨论】:

      【解决方案2】:

      我不知道你是否可以直接从 URL 编译文件。

      一种解决方法是创建您自己的“maven”存储库(不是很方便,因为您总是需要在新存储库中添加 JAR,但是使用此解决方案,它“即使在库未发布到 maven 存储库时也可以工作")。

      repositories {
          maven {
                url "http://..."
          }
      }
      
      dependencies {
          compile 'MyLibrary'
      }
      

      但据我所知,从 URL 下载不是一个可行的选择。

      另外,看看这段代码(未测试):

      dependencies {
          compile ('my-custom-library:1.0') {
              artifact {
                  name = 'my-custom-library'
                  extension = 'jar'
                  type = 'jar'
                  url = 'http://....'
              }
          }
      }
      

      【讨论】:

      • 谢谢,稍后再测试
      猜你喜欢
      • 2020-04-15
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多