Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access graphics from drawable folder of a dynamic feature module from main module

I'm getting my feet wet with dynamic module split API delivery in order to break up my game app into Instant and Installable versions. I've been following the Codelabs tutorial here https://codelabs.developers.google.com/codelabs/on-demand-dynamic-delivery/index.html#0. Unfortunately it uses Kotlin for the MainActivity code, which is less specific than Java, but still fairly followable if you've done a Kotlin tutorial. The example includes accessing a text tile in an 'assets' folder in an 'assets' feature module with the following:

private const val packageName = "com.google.android.samples.dynamicfeatures.ondemand"

val assetManager = createPackageContext(packageName, 0).assets
// Now treat it like any other asset file.
val assets = assetManager.open("assets.txt")
val assetContent = assets.bufferedReader()
           .use {
               it.readText()
           }

For now I just want to access graphic files in a drawable folder of my dynamic feature module. I'll only be using my dynamic feature module to store large graphics that would take me over the 10 MG limit for an Instant app download. What would be the cleanest way to do this?

Main 'app' module:

enter image description here

Java code in 'app':

loadTexture(R.drawable.aaa_image);

Bitmap bitmap;
public void loadTexture(final int resourceId){
    bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);
    ***

Dynamically delivered 'installationassets' module:

enter image description here

Still java code in 'app', won't reach:

 loadTexture(R.drawable.testgraphic);
 cannot resolve symbol 'testgraphic'
like image 939
Androidcoder Avatar asked Aug 26 '19 18:08

Androidcoder


People also ask

Why can’t I add dynamic feature modules to my App?

If your dynamic feature modules include activities or services which interact with system components, such as launcher shortcut icons, dynamic shortcuts, or notifications, you may have a hard time customising your app to support these. This is because these system components are unable to access resources like icons from dynamic modules.

What are dynamic feature modules (DFMs)?

We’re going to call them dynamic feature modules, or DFMs. When you distribute your app as an Android App Bundle, Play Store will deliver these modules to user’s devices as separate APKs at the appropriate time.

What is a feature module in angular?

A Feature Module in Angular is a module other than our main application root module that is decorated by @ngModule () decorator and hence share the same property configurations like application root module.

What is the storage module in Salesforce?

The storage module is a dynamic feature module configured for on-demand delivery. What we’ll be looking at in this article is how you can access a class that lives in the storage module from the base app module.


1 Answers

I found a working solution to load drawables from a feature module resources. The trick is to use the correct package name for the module.

Example:

  • base module package name is "com.project"
  • feature module name defined in build.gradle "FeatureModule"
  • feature module package name "com.project.FeatureModule"

build.gradle

dynamicFeatures = [":FeatureModule"]

Java

int drawableResId = context.getResources().getIdentifier("your_drawable_name", "drawable", "com.project.FeatureModule");
context.getDrawable(drawableResId);

Reference article

like image 61
pumnao Avatar answered Sep 30 '22 19:09

pumnao