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

标签 android gradle android-gradle-plugin bitbucket

我在 Bitbucket 存储库中有一些 .txt 文件(只有 .txt 文件不是 android lib),我想在通过 Android Studio(Gradle)构建项目时将其添加到我的 android 项目中。

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

我研究了很多,但找不到任何解决方案。请帮忙。

最佳答案

您可以使用 preBuild在构建之前下载文件并使用 this method 执行下载的任务.下面将文件下载到assets您的 app 的目录模块

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

如果使用私有(private)存储库,请将您的凭据与基本身份验证方案 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 }}
    }
}

在这种情况下,您可以将它们放入 local.properties文件(用于不提交您的凭据):
file_path=app/src/main/assets/index.html
ext_url=https://username:password@bitbucket.org/bertrandmartel/test/raw/c489ae46c3de9ad7089f53660a8de616af08265d/youtube.html

阅读 preBuild 中的属性任务 :
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")
    }
}

关于Android - 如何在 gradle build 时从 bitbucket 下载文本文件并将其添加到项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44676682/

相关文章:

android - com.android.builder.merge.DuplicateRelativeFileException:使用操作系统独立路径 'META-INF/INDEX.LIST'找到了多个文件

java - 如何在jar中重新放置软件包以获取gradle依赖

android - Gradle重复输入

安卓工作室 : “Execution failed for task ' :app:mergeDebugResources'” if project is created on drive C:

android-gradle-plugin - 在最新检查期间无法捕获任务 ':app:bundleReleaseJsAndAssets' 属性 '$1' 的输入文件的快照

java.lang.NoClassDefFoundError : android/os/Build$VERSION 错误

android - 获取图像输入流的大小

java - 如何创建带有阴影和垂直项目的 CardView

android - 在 View 上使用 Canvas 绘制,但未绘制任何内容

android - 将 Gradle 与现有的 Android 项目结合使用