首页 > 解决方案 > 在构建时下载 Android 字符串资源

问题描述

我在一个应用程序上工作,在该应用程序中,我们将在线的 strings.xml 文件翻译成众包翻译。翻译工具提供了一个 API,用于访问每个 URL 的 xml 文件。

例如:https://localise.biz/api/export/locale/en.xml?format=android&key=7qUo-LUKd4VIHSwRYB5005T7QQbaFCGw

gradle构建开始时是否可以下载并包含这些文件?

期待您的回答!

标签: androidstringandroid-resources

解决方案


IamAshKS 为我提供了答案。我是这样解决的:

task downloadTranslations {
   group 'pre-build tasks'
   description 'Downloads all translation files when building the app.'

   ext.apiKey = '7qUo-LUKd4VIHSwRYB5005T7QQbaFCGw'

   //English
   doLast {
      def f = new File("${project.projectDir}/src/main/res/values/strings.xml")
      new URL("https://localise.biz/api/export/locale/en.xml?format=android&key=${apiKey}").withInputStream{ i -> f.withOutputStream{ it << i }}
   }
}

非常感谢你的帮助!现在,我只需要在实际构建之前运行它。


推荐阅读