我能否使用Firebase远程配置来更新Android应用程序的strings.xml文件?

4
众所周知,Firebase Remote Config 是一项云服务,它可以让您更改应用程序的行为和外观,而无需要求用户下载应用程序更新。
我想知道是否可以使用 Firebase Remote Config 来更新 strings.xml?
有时候我需要更新 strings.xml 文件(例如:更正本地化语言的翻译)。为了做到这一点,我必须更新我的应用程序。
如果我们可以像 Firebase 一样将 strings 存储在服务器上,那就太棒了。

3
strings.xml 无法在运行时更改。 - Atif Farrukh
@AtifFarrukh 谢谢。 - TOP
虽然您无法动态更新strings.xml,但是有一个简单的解决方案。请查看下面的答案。 - Ravindra Barthwal
你可以查看这个Firebase解决方案 https://dev59.com/UZzha4cB1Zd3GeqPKMfi#69600481 - Ahmed na
2个回答

3
动态更新strings.xml是不可能的,但有解决方案。当我想要使用Firebase远程配置进行A / B测试时,我也遇到了同样的问题。因此,我为Android创建了FString库,它提供了一个智能解决方案。它目前在Mouve Android App上运行。

安装

  • 在项目模块的build.gradle中添加此依赖项
implementation 'com.ravindrabarthwal.fstring:fstring:1.0.0'

在您的Android应用程序类中添加以下代码。
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
    }

}

使用

访问字符串

  // 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 ;)

更新 FString 值

  /*
   * 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) 

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


2
我认为你无法通过Firebase完成此操作,只需更新apk即可。

真令人难过得知这个消息。 - TOP

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接