动态特征:从动态特征模块访问可绘制的库

Dynamic feature: Access library drawable from dynamic feature module

我正在尝试从我的 :wrapped 模块访问位于 :library 模块(常规 android 库模块)中的资源,也是一个常规 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)

想出了如何从 :wrapped 中的 Compose 代码访问 :library 中的资源:

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