Stringalize
← All articles

Updating Android string resources without a Play Store release

· 7 min read

An Android app keeps its text in res/values/strings.xml and one more file per language, values-de, values-it, values-pt-rBR. That works well until a translation turns out to be wrong after release. Then a one-word fix needs a new build, a Play Console upload, a staged rollout and users who install the update.

Our article on over-the-air updates covers why that matters and the rules that keep it safe. This one is about Android specifically: why its strings are harder to replace than they look, the paths text takes to the screen, and how the Stringalize library reaches each of them.

Strings are compiled into the app

strings.xml doesn't ship as XML. At build time it is compiled into resources.arsc, a binary table inside the APK, and every string gets an integer id: R.string.checkout_button is just a number. At run time Android looks that number up in the table, picking the language from the device or app settings.

That table is read-only. There is no supported way to add a file to an installed app's resources, or to point Android at a newer copy. On iOS a downloaded .lproj folder can be loaded as a bundle; on Android there is nothing equivalent. So instead of replacing the files, a live update has to step in at the moment the app asks for a string.

Three paths to the screen

Text reaches an Android screen in three different ways, and each needs its own handling.

From code

getString(R.string.greeting), getQuantityString and their formatting variants all go through the context's Resources object, and end up in two methods: getText and getQuantityText. Answer those two and every lookup from code follows.

From Compose

stringResource and pluralStringResource read the Resources of the current context too, the Activity the composition lives in. Once the Activity's resources answer with the new text, Compose does, without any change to composables.

From XML layouts

This is the tricky one. android:text="@string/farewell" in a layout is not resolved through getText: the view reads its attributes directly from the compiled layout. Wrapping Resources does nothing for it. The text has to be applied as the views are created, which means taking part in layout inflation.

How the library does it

The Stringalize Android library handles all three, using only public Android APIs.

  • A context wrapper. Stringalize.wrap(base) returns a ContextWrapper whose Resources override getText and getQuantityText. For each lookup it asks Android for the resource's name, checkout_button, and returns the released text when the release has that name. Every other resource, and every string the release doesn't contain, comes from the app as before.
  • A layout inflater. The same wrapper hands out a LayoutInflater that wraps the view factory AppCompat and Material install. After a TextView, Button or EditText is created, it checks whether android:text or android:hint pointed to a string resource, and sets the released text if there is one.
  • Plural rules from the platform. A release carries every plural form a language has. The library picks the right one with Android's own ICU plural rules, so "5 files" in Polish gets the many form, and 22 gets few.
  • The app's language, not just the device's. Before using a release, the library checks it against the locales of the resources it is answering for. If someone switches the app to Italian with per-app language settings, an English release stops applying and the Italian one is downloaded.

The release itself is a small JSON file per language, with the text exactly as getString would return it: escapes already resolved, placeholders in Android's %1$s syntax. The library downloads only the user's language, saves it in the app's private files, and from the next launch uses it.

Setting it up

The library is on JitPack. Add the repository in settings.gradle.kts:

dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
        maven("https://jitpack.io")
    }
}

Then the dependency in your app module:

implementation("com.github.cbarbera80:stringalize-android:0.1.0")

Start it in your Application, with the token from the project's Live updates dialog:

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        Stringalize.start(this, token = "ota_…")
    }
}

And wrap each Activity's context, most easily in a base class they share:

open class BaseActivity : AppCompatActivity() {
    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(Stringalize.wrap(base))
    }
}

That's the whole integration. Your strings stay in strings.xml, and your code keeps calling getString and stringResource. In Stringalize, import the same strings.xml files so the project has the same names as the app. When a translation needs fixing, correct it, mark it translated and press Publish release.

What it covers, and what it doesn't

Updated text reaches:

  • getString, getText and getQuantityString on a wrapped Activity, and in its fragments;
  • Compose stringResource and pluralStringResource;
  • android:text and android:hint in XML layouts, including View Binding and Data Binding.

It does not reach:

  • Strings with styling markup, such as <b> or <xliff:g>. Replacing them with plain text would lose the styling, so they keep the bundled version.
  • Menus, toolbar titles set in XML, preference screens and manifest labels. Android reads those through other paths. Set them from code if they need to be updatable.
  • Custom views declared by their full class name, in apps that don't use AppCompat or Material.

A key with dots in Stringalize, such as home.title, also answers for R.string.home_title, since Android turns dots into underscores in resource names. A string that exists in Stringalize but not in the app has no effect: live updates change text, never add it.

Is it allowed on Google Play?

Google Play's rules forbid apps from downloading executable code outside the store. A release contains text and nothing else: the same kind of content as a news feed or a product list. The library uses no hidden APIs and no reflection, and sends only the token, the release number and the language code. Nothing identifies the device or the person using it.

Same release, both platforms

If you ship on iOS too, the same project serves both. Each release is rendered once per platform: iOS apps get .strings and .stringsdict files with %@, Android apps get their text with %1$s, from the same translations. (Why the two syntaxes differ is the subject of another article.) Fix the German checkout button once, publish, and both apps show it from their next launch.