简体   繁体   English

通过API响应动态创建strings.xml文件

[英]Dynamic strings.xml file creation from API response

I would like to create my strings.xml file dynamically every time I build my project from Android Studio. 每次从Android Studio构建项目时,我都希望动态创建自己的strings.xml文件。 Right now I'm using Loco to share strings across Android and IOS. 现在,我正在使用Loco在Android和IOS之间共享字符串。 This tool exposes an API which response is my strings.xml or .strings file (IOS). 该工具公开了一个API,其响应是我的strings.xml或.strings文件(IOS)。 I would like to know if there is any way to call the API from gradle in order to update the strings files each time the project is build. 我想知道是否有任何方法可以从gradle调用API,以便在每次构建项目时更新字符串文件。 Maybe Fastlane can help me with that issue? 也许Fastlane可以帮助我解决这个问题?

Finally, I found a solution. 最后,我找到了解决方案。 Basically, with a groovy script in your gradle you can call an http endpoint via URL class from java.net , get the response of the GET call as string and write the response into your string.xml file. 基本上,使用groovy脚本,您可以通过java.net URL类调用http端点,以字符串形式GET调用的响应,并将响应写入到string.xml文件中。

In my build.gradle (Project level) I defined the " syncStrings " task as below: 在build.gradle(项目级别)中,我定义了“ syncStrings ”任务,如下所示:

task syncStrings {
    def strings = new URL( 'https://localise.biz/api/export/locale/it-IT.xml?format=android&key=<api_key>' ).text
    File file = new File("app/src/main/res/values/strings.xml")
    file.write(strings)
}

Hope it helps somebody else ;) 希望它可以帮助别人;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM