动态功能:从动态功能模块访问可绘制库

发布于 2025-01-19 18:39:35 字数 1347 浏览 0 评论 0原文

我正在尝试从我的 :wrapped 模块(也是常规的 android 库模块)访问 :library 模块(常规的 android 库模块)中的资源。

复杂的因素是我创建了一个 :dynamicFeatureWrapper 模块,它导入 plugin id: 'com.android.dynamic-feature',因此充当动态功能模块,但实际上,我的动态功能的实际内容位于 :wrapped 中,

:dynamicFeatureWrapper (imports the plugin id: 'com.android.dynamic-feature')
    |            |
    | api        |impl
    v            v
:wrapped        :baseApp 
    |
    |api
    v
:library 

我首先像安装任何动态功能模块一样安装 :dynamicFeatureWrapper ,然后从我的:wrapped 模块,我尝试访问 :library 中的可绘制资源。但是,当我尝试像这样访问它时,我得到 ResourceNotFound

// in :wrapped module

LocalContext.current.applicationContext.resources.getIdentifier(
    "my_icon",
    "drawable",
    "com.example.myapp.library"

这会导致以下崩溃:

2022-04-07 14:13:41.516 2114-2114/com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapp, PID: 2114
    java.lang.NoSuchFieldError: No static field my_icon of type I in class 
Lcom/example/myapp/library/R$drawable; or its superclasses (declaration of 
'com.example.myapp.library.R$drawable' appears in /data/user/0/com.example.myapp/files/splitcompat/6057000/verified-splits/dynamicFeatureWrapper.apk!classes4.dex)

I am trying to access a resource that lives in :library module (a regular android library module), from my :wrapped module, also a regular android library module.

The complicating factor is that I created a :dynamicFeatureWrapper module, which imports the plugin id: 'com.android.dynamic-feature', and therefore serves as the dynamic feature module, but really, the actual content of my dynamic feature is in :wrapped

:dynamicFeatureWrapper (imports the plugin id: 'com.android.dynamic-feature')
    |            |
    | api        |impl
    v            v
:wrapped        :baseApp 
    |
    |api
    v
:library 

I first install the :dynamicFeatureWrapper as I would any dynamic feature module, and then from my :wrapped module, I try to access a drawable resource in :library. However, when I try to access it like this, I get ResourceNotFound:

// in :wrapped module

LocalContext.current.applicationContext.resources.getIdentifier(
    "my_icon",
    "drawable",
    "com.example.myapp.library"

This results in the following crash:

2022-04-07 14:13:41.516 2114-2114/com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapp, PID: 2114
    java.lang.NoSuchFieldError: No static field my_icon of type I in class 
Lcom/example/myapp/library/R$drawable; or its superclasses (declaration of 
'com.example.myapp.library.R$drawable' appears in /data/user/0/com.example.myapp/files/splitcompat/6057000/verified-splits/dynamicFeatureWrapper.apk!classes4.dex)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2025-01-26 18:39:35

弄清楚如何从位于 :wrapped 中的 Compose 代码访问位于 :library 中的资源:

val context = LocalContext.current
val resources = context.resources
val packageName = context.packageName
val drawableId = resources.getIdentifier("ic_my_icon", "drawable", packageName)

Figured out how to access my resource that lives in :library from my Compose code that lives in :wrapped:

val context = LocalContext.current
val resources = context.resources
val packageName = context.packageName
val drawableId = resources.getIdentifier("ic_my_icon", "drawable", packageName)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文