android / user-interface-samples

Multiple samples showing the best practices in the user interface on Android.
Apache License 2.0
4.34k stars 1.63k forks source link

Per-app languages don't work in release builds #408

Open DimitarStoyanoff opened 1 year ago

DimitarStoyanoff commented 1 year ago

Reproduction steps:

  1. Open either compose_app or views_app examples from PerAppLanguages folder in Android Studio.
  2. Create a keystore and build release app bundle.
  3. Use bundletool to run the app on a device.

Expected result: Text language changes upon selection.

Actual result: App stays in the default language, state change is not triggered. Killing the app and restarting it doesn't change the app strings language. The compose_app example even gets an ANR. On SDK 33 in device settings the correct language is set for the app. AppLocalesStorageHelper logs show the selected locale was persisted correctly, but the app does not reflect those changes.

Tested on Pixel 6 emulators with SDK 31 and 33, as well as on physical OnePlus Nord. Tested with AGP versions 7.2.2, 7.3.1 and 8.0.0-alpha09.

ashnohe commented 1 year ago

Thanks for flagging this issue @DimitarStoyanoff! I can replicate. This look to be an issue with split apks.

As a workaround you might disable language splits, see Handling language changes. The below works as a workaround:

bundle {
  language {
    enableSplit = false
  }
}

Another option may be to use the Play Core library to download languages on demand, see Download additional language resources or this blog post. I've not yet tested this.

The last option is to use --mode=universal instead of --connected-device as a bundletool parameter to create a single apk instead of a split apk. That will not work in production however, as user's will run into the same issue with languages not downloading on request.

More info: The language is I believe not switching to one of the other languages because the language is not set as a system language preference and therefore not included in the initial split apk. For example, if I set my system language preferences to English and Japanese, the compose in-app language picker (on a release build with a split apk) works for those two languages, but not for the other languages.

I'm connecting with the team internally on this and will circle back, but hopefully the above workarounds unblock you in the meantime.

DimitarStoyanoff commented 1 year ago

Thank you @ashnohe for the analysis and for the workarounds! I will proceed with alpha testing and using them for the time being.

I had also opened an issue in Issue Tracker for this case (though it got some spam comments for whatever reason). Will post a link to this update there.