Dynamic feature: Access library drawable from dynamic feature module

595 views Asked by At

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)
1

There are 1 answers

0
VIN On BEST ANSWER

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)