You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于Android 11 ResourcesProvider与ResourcesLoader类动态加载资源的用法、Lokalise集成及低版本支持的技术问询

Answers to Your ResourcesLoader/ResourcesProvider Questions

1. Can these classes really be used to dynamically provide strings, themes, and other resources?

Absolutely! As you found in the cached article, Android 11 (API 30) introduced ResourcesLoader and ResourcesProvider specifically to let developers customize resource loading logic. They aren't limited to just strings—you can dynamically load Drawables, layouts, themes, and most other resource types too.

The core idea is that you can define custom resource sources (like a local directory, a downloaded APK, or even cloud-hosted resources if you can expose them as a file system path) and prioritize those sources over the app's built-in resources. The sample code you shared (loading resources from a directory extracted from an APK) is a perfect example of how this works: you're telling the app's Resources instance to check this custom provider first when resolving resources.

2. Can this be integrated with the Lokalise SDK using its provided Resources instance?

This is a bit trickier, but it's possible with some extra work. The ResourcesProvider API currently only offers two static methods to create instances: loadFromDirectory() and loadFromApk(). Since Lokalise provides its own Resources instance, you'll need to bridge that to a format ResourcesProvider can use.

Here's a potential approach:

  • Check if the Lokalise SDK lets you export its downloaded resources to a local directory (like your app's cache or files directory).
  • Once those resources are saved locally, use ResourcesProvider.loadFromDirectory() to create a provider pointing to that path.
  • Add this provider to a ResourcesLoader and attach it to your app's main Resources instance, just like in your sample code.

Keep in mind that Lokalise might not have built-in support for this API yet (since it's relatively new), so you may need to handle the resource extraction yourself. If Lokalise's Resources instance uses in-memory resources without exposing a file path, this approach won't work—you'd need a way to serialize those resources to disk first.

3. Is there a support library to use this feature on Android versions below API 30?

Unfortunately, no. ResourcesLoader and ResourcesProvider are system-level APIs introduced in Android 11, and they rely on changes to the platform's underlying resource loading system. There's no official support library (like AndroidX) that backports this functionality to older versions.

For apps targeting API levels below 30, you'll need to stick with traditional dynamic resource solutions—like the built-in functionality provided by Lokalise, which typically uses resource override techniques compatible with older Android versions.


Example Code Recap

You shared a solid example of loading resources from an extracted APK. Here's that code formatted for clarity:

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        val file = File(cacheDir, "overrideResources")
        unzip(ZipInputStream(assets.open("override.apk")), file)
        val rl = ResourcesLoader()
        rl.addProvider(ResourcesProvider.loadFromDirectory(file.path, null))
        resources.addLoaders(rl)
    }
}

This works by extracting the override APK's resources to a local directory, creating a provider for that directory, and telling the app's Resources to use this loader first.

内容的提问来源于stack exchange,提问作者android developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 12:17:34