Android SDK

Integrate

Android SDK

Over-the-air strings in a Kotlin Android app, with the APK’s strings as the fallback.

On this page

The lantern-android library loads a project’s published OTA release into a native Android app, and falls back to the app’s bundled strings.xml for anything the release doesn’t have. It has no dependencies and needs minSdk 24.

Install#

JitPack builds the library from a git tag. Add the repository, then the dependency:

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

// app/build.gradle.kts
dependencies {
    implementation("com.github.Rabzizz:lantern-android:v0.1.0")
}

Set up#

First create a distribution key and publish a release in the project’s Releases tab (how it works). Then initialise the library once and wrap every Activity:

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        // The project's distribution key, from its Releases tab.
        Lantern.init(this, "https://lantern.abyss-inn.ch", "ld_…")
    }
}

// Every Activity: a base class is easiest.
abstract class BaseActivity : AppCompatActivity() {
    override fun attachBaseContext(newBase: Context) =
        super.attachBaseContext(Lantern.wrap(newBase))
}

From then on getString, android:text and android:hint in XML layouts, and Compose’s stringResource return the release’s value when it has one.

Strings match by Android resource name, exactly as the strings.xml export names them: home.title becomes R.string.home_title. A project imported from strings.xml already matches.

When new strings show#

init fetches the current language in the background and never blocks startup. New strings apply to screens created after they arrive, normally on the next launch. To apply them sooner:

// Runs on the main thread after a refresh brought new strings.
Lantern.onUpdated = { recreate() }
// Fetch again, e.g. in onResume or on pull-to-refresh.
Lantern.refresh()

Lantern.refreshNow() is the blocking variant for a worker thread, such as WorkManager. Outside an Activity, use Lantern.getString(…).

Fallbacks#

SituationResult
The key is in the releaseThe release’s value. b, i and u tags become styling, as in strings.xml.
The key isn’t in the releaseThe bundled resource.
Offline, or a server errorThe last release the app cached.
No release for this key, language or app version (404)The cache is cleared and the bundled strings are used.
A corrupt cache, or one written by another app versionIgnored; the bundled strings are used.
The device language is pt-BRTries pt-BR, then pt, as Android does.

The app version sent to Lantern is versionName cut to its leading numbers (1.4.2-beta becomes 1.4.2), so a release’s app-version range applies.

Plurals#

Plurals stored in ICU form are selected with the device’s CLDR plural rules, so in French 0 picks the one branch. A branch may use Android’s %d or ICU’s #. A plural with =0 or offset: has no Android form, so the app keeps its bundled plural.

Limits#

  • In XML layouts, only android:text and android:hint on TextViews are covered, including EditText, Button and the AppCompat and Material widgets. Menu titles and Toolbar titles keep the bundled text: set them in code with getString.
  • A custom TextView subclass written with its full class name in XML is covered on API 29 and up.