Introduction

That’s not a common subject and few people might run into this topic.
However, here is an example : you need to update some texts like a help component or add languages or even fix some localization mistakes… all of this without updating your APK !
Sadly, you can’t download a need « strings.xml » and replace your old one ( or even create values-fr/strings.xml )
So I actually made a proof of concept of a system to work this constraint. I guess the idea isn’t new but it still uses some tricks to make it efficient on Android.

The system

StringServerSide

In practice

  1. Starting the application, the program will read the string.xml matching the  language selected on the device and then import it to the SQL Lite Database.
  2. Then using a version system, we’ll request the server for an updated version of our strings if necessary !
  3. Depending on the version,we’ll update the database as well as the « string version » in the preferences.

How to capture all strings automatically ?

That’s quite an interesting question when you’re new to Android !
Actually, you know how to use myTextView.setText(R.string.myString);
You don’t need to do add (getString(R.string.myString), « R.string.myString ») line by line :

We use Java Reflect !

Java Reflect can be used to retrieve fields from a Class as well as their values… at runtime.
So the trick is to call :

Class androidString = R.string.class;
Field[] fields = androidString.getFields();

Then with a simple loop you put your insert requests.

So this is my sample project as a demonstration.
NB : I didn’t made the server connection part. I assume that’s not really tricky to do ( I use something similar in most of my apps )

>> Sample code ( Eclipse project ) <<

Conclusion

To be honest, this method isn’t optimal. However, there aren’t much better solutions out there (or else I didn’t found them 😉 ). You might use an external files (xml, json… ) to store your texts. However, I guess it will way more complicated to get decent performances as well as a elegant update management. Finally, if you can avoid it you should but you need … it’s possible. 🙂

Hopefully it might give ideas for some others features !

Publicités