Android-如何从gradle build的bitbucket下载文本文件并将其添加到项目中

Android-如何从gradle build的bitbucket下载文本文件并将其添加到项目中,android,gradle,android-gradle-plugin,bitbucket,Android,Gradle,Android Gradle Plugin,Bitbucket,我在Bitbucket repo中有一些.txt文件(只有.txt文件而不是android lib),我想在通过android studio(Gradle)构建项目时将其添加到我的android项目中 要实现的目标:随时修改远程文件内容,并在构建项目时添加更新的文件 我做了很多研究,但没有找到任何解决办法。请提供帮助。您可以使用预生成任务在生成之前下载文件,并使用执行下载。以下内容将把文件下载到应用程序模块的资产目录中 android { preBuild << {

我在Bitbucket repo中有一些.txt文件(只有.txt文件而不是android lib),我想在通过android studio(Gradle)构建项目时将其添加到我的android项目中

要实现的目标:随时修改远程文件内容,并在构建项目时添加更新的文件


我做了很多研究,但没有找到任何解决办法。请提供帮助。

您可以使用
预生成任务在生成之前下载文件,并使用执行下载。以下内容将把文件下载到
应用程序
模块的
资产
目录中

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 }}
    }
}
在这种情况下,您可以将它们放入
local.properties
文件中(用于不提交凭据):

阅读
预构建任务中的属性:

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

preBuild文件。withOutputStream{可能是我拥有私有bitbucket存储库的副本,如何传递bitbucket凭据?请参阅我更新的帖子,了解如何使用凭据和读取本地属性的值我在“ext_url”中添加了用户名和密码正如您所提到的,但这总是重定向到登录页面,并下载登录页面的html内容。请记住这是私人回购。转到Bitbucket上的回购,转到您的文件选择原始按钮,复制地址并使用
https://username:password@org
代替
https://bitbucket.org
。我的私人回购确实对我起了作用
file_path=app/src/main/assets/index.html
ext_url=https://username:password@bitbucket.org/bertrandmartel/test/raw/c489ae46c3de9ad7089f53660a8de616af08265d/youtube.html
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")
    }
}