6

Is it possible to do something like:

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

in Gradle/Android Studio?

Possible advantages:

  1. Always get the latest version (You don't always have the latest version if you have to download and copy it manually)
  2. Even works when the library is not published to the maven repository

Or do I have to download and copy it every time?

user3593022
  • 127
  • 1
  • 10

2 Answers2

9

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

Jan
  • 1,359
  • 1
  • 13
  • 18
0

I do not know if you can compile files directly from an URL.

A workaround will be to create your own "maven" repository (not very convenient because you will always need to add JAR in your new repo, but with this solution its "even works when the library is not published to the maven repository").

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

dependencies {
    compile 'MyLibrary'
}

But as far as I know, it is not a viable choice for downloading from URLs.

Also, have a look at this code (not tested):

dependencies {
    compile ('my-custom-library:1.0') {
        artifact {
            name = 'my-custom-library'
            extension = 'jar'
            type = 'jar'
            url = 'http://....'
        }
    }
}
Guicara
  • 1,668
  • 2
  • 20
  • 34