Can I use Firebase remote config for updating strings.xml of Android app?

坚强是说给别人听的谎言 提交于 2020-02-22 19:08:54

问题


Well known that Firebase Remote Config is a cloud service that lets you change the behavior and appearance of your app without requiring users to download an app update.

I was wondering that can I use Firebase Remote Config for updating strings.xml?

Sometime I need to update file strings.xml (for example: correct the translation of locale languages). And to do this I have to update my app.

It's great if we can store the strings in server like Firebase.


回答1:


I think you cannot do it through Firebase, just need to update apk.




回答2:


Dynamically updating strings.xml is not possible but there's a solution to this. I ran into the same problem when I wanted to do A/B testing with Firebase Remote Config. So I created FString library for android and it provides a smart solution. It currently powers Mouve Android App on production.

Installation

  • In your project's module build.gradle add this dependency
implementation 'com.ravindrabarthwal.fstring:fstring:1.0.0'
  • In your Android Application class add the following code
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

To access the strings

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

To update the FString values

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

Check the library documentation for more info. If you find the answer helpful upvote this and mark it as the answer.



来源:https://stackoverflow.com/questions/39523919/can-i-use-firebase-remote-config-for-updating-strings-xml-of-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!