在 Gradle 中从 Url 编译 Jar

use*_*022 3 android gradle android-studio

是否可以执行以下操作:

compile files('http://ho.st/jar/MyLibrary.jar')
Run Code Online (Sandbox Code Playgroud)

在 Gradle/Android Studio 中?

可能的优势:

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

还是我每次都必须下载并复制它?

Jan*_*Jan 6

这对我有用:

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')
}
Run Code Online (Sandbox Code Playgroud)

删除构建目录后,它将下载一个新副本


Gui*_*ara 0

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

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

repositories {
    maven {
          url "http://..."
    }
}

dependencies {
    compile 'MyLibrary'
}
Run Code Online (Sandbox Code Playgroud)

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

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

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