Compile Jar from Url in Gradle

android, android-studio, gradle

Solution

This works for me:

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')
}

It will download a fresh copy after deleting the build dir

Problem

Is it possible to do something like: ``` compile files('http://ho.st/jar/MyLibrary.jar') ``` in Gradle/Android Studio? Possible advantages: - Always get the latest version (You don't always have the latest version if you have to download and copy it manually) - Even works when the library is not published to the maven repository Or do I have to download and copy it every time?

Original source