【问题标题】:Download Android string resources at build time在构建时下载 Android 字符串资源
【发布时间】:2020-03-30 06:30:49
【问题描述】:

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

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

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

期待您的回答!

【问题讨论】:

标签: android string android-resources


【解决方案1】:

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

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

【讨论】:

  • 它为您工作真是太好了。我不确定您是否放置了实际的 API 密钥,但最好不要将 API 密钥放在 Stack Overflow 或任何公共页面或网站上。谢谢!
  • 这里没有安全问题。它只是一个导出密钥。该密钥没有写入权限。
  • 哦,那样的话,太好了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
相关资源
最近更新 更多