我可以使用 Firebase 远程配置来更新 Android 应用程序的 strings.xml 吗?

2024-04-22

众所周知Firebase Remote Config 是一项云服务,可让您更改应用的行为和外观,而无需用户下载应用更新。

我想知道我可以使用Firebase Remote Config用于更新strings.xml?

有时我需要更新文件strings.xml(例如:更正语言环境的翻译)。为此,我必须更新我的应用程序。

如果我们可以存储的话那就太好了strings在服务器中像Firebase.


动态更新 strings.xml 是不可能的,但有一个解决方案。当我想使用 Firebase Remote Config 进行 A/B 测试时,我遇到了同样的问题。所以我创建了FString https://github.com/ravindrabarthwal/FString/Android 库,它提供了一个智能解决方案。目前它的权力移动 Android 应用程序 https://play.google.com/store/apps/details?id=app.mouve.androidbase1关于生产。

安装

  • 在您的项目模块中构建.gradle添加这个依赖
implementation 'com.ravindrabarthwal.fstring:fstring:1.0.0'
  • 在你的安卓设备中应用类中添加以下代码
import com.example.myapp.R.string as RString // Import your string 

class MyApp: Application() {

    override fun onCreate() {
        super.onCreate()
        FString.init(RString::class.java) // Initializes the FString
    }

}

Usage

访问字符串

  // This is how you get strings without FString
  context.getString(R.string.my_app_name) // This is how you normally do

  // To get the string using FString use
  FString.getString(context, R.string.my_app_name)

  // If you prefer extension function use
  context.getFString(R.string.my_app_name) // This is how FString works ;)

更新字符串值

  /*
   * Assume this JSON is coming from server. The JSON string 
   * must be parseable to a JSON object with key and value pairs
   */
  var jsonFromServer = """
      {
        "my_app_name": "MyApp v2.0",
        "some_other_string": "this is so cool",
      }
  """.trimIndent()

  // This will update the SharedPreference using keys from
  // above JSON and corresponding value. The SharedPreference will
  // not save any key that is not is strings.xml
  FString.update(context, jsonFromServer) 

查看库文档以获取更多信息。如果您发现答案有帮助,请投票并将其标记为答案。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我可以使用 Firebase 远程配置来更新 Android 应用程序的 strings.xml 吗? 的相关文章

随机推荐