-1

I have some .txt file (Only .txt files not android lib) in Bitbucket repo which I want to add into my android project when project building through Android studio (Gradle).

Goal to achieve: Modify remote files content any time and add updated file when building project.

I research a lot but couldn't find any solution. Please help.

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
Vishesh Chandra
  • 6,951
  • 6
  • 35
  • 38
  • Possible duplicate of [how to download external files in gradle?](https://stackoverflow.com/questions/17123606/how-to-download-external-files-in-gradle) – MSpeed Jun 21 '17 at 14:58

1 Answers1

1

You can use the preBuild task to download the file before the build and perform the download with this method. The following will download a file into assets directory of your app module

android {

    preBuild << {
        def url = "https://bitbucket.org/HellGate/jquery-slider/raw/5ab0c31aaa57fb7d321076194f462b472f5f031e/index.html"
        def file = new File('app/src/main/assets/index.html')
        new URL(url).withInputStream{ i -> file.withOutputStream{ it << i }}
    }
}

If using private repositories, put your credentials with basic auth scheme username:password :

android {

    preBuild << {
        def url = "https://username:password@bitbucket.org/HellGate/jquery-slider/raw/5ab0c31aaa57fb7d321076194f462b472f5f031e/index.html"
        def file = new File('app/src/main/assets/index.html')
        new URL(url).withInputStream{ i -> file.withOutputStream{ it << i }}
    }
}

In this case, you could put them in local.properties file (for not committing your credentials) :

file_path=app/src/main/assets/index.html
ext_url=https://username:password@bitbucket.org/bertrandmartel/test/raw/c489ae46c3de9ad7089f53660a8de616af08265d/youtube.html

Read the properties in your preBuild task :

preBuild << {

    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())

    if (properties.containsKey("file_path") && properties.containsKey("ext_url")) {
        def file = new File(properties.getProperty("file_path"))
        def url = properties.getProperty("ext_url")
        new URL(url).withInputStream{ i -> file.withOutputStream{ it << i }}
    }
    else{
        println("no properties found")
    }
}
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • I have private bitbucket repository, How to pass bitbucket credentials? – Vishesh Chandra Jun 22 '17 at 05:45
  • Please see my updated post for using credentials and reading the values from local properties – Bertrand Martel Jun 22 '17 at 08:48
  • I have added username and password in "ext_url" as you have mention but this is always redirected to login page and also downloading the html content of login page. Please keep in mind this is private repo. – Vishesh Chandra Jun 22 '17 at 11:41
  • 1
    Go to your repo on Bitbucket, go to your file select the raw button, copy address and use `https://username:password@bitbucket.org` in place of `https://bitbucket.org`. It did work for me with my private repo – Bertrand Martel Jun 22 '17 at 11:57