从主模块访问动态特征模块的可绘制文件夹中的图形 [英] Access graphics from drawable folder of a dynamic feature module from main module

查看:119
本文介绍了从主模块访问动态特征模块的可绘制文件夹中的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了让我的游戏应用分为即时版和可安装版,我不停地使用动态模块拆分API交付.我一直在这里关注Codelabs教程 https ://codelabs.developers.google.com/codelabs/on-demand-dynamic-delivery/index.html#0 .不幸的是,它使用Kotlin编写MainActivity代码,该代码不像Java那样具体,但是如果您完成了Kotlin教程,它仍然是可以理解的.该示例包括使用以下命令访问资产"功能模块中资产"文件夹中的文本磁贴:

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()
           }

现在,我只想访问动态功能模块的可绘制文件夹中的图形文件.我将仅使用动态功能模块来存储大型图形,这些图形将使我超过10 MG的即时应用程序下载限制.这样做最干净的方法是什么?

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?

应用"主模块:

应用"中的Java代码:

Java code in 'app':

loadTexture(R.drawable.aaa_image);

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

动态交付的"installationassets"模块:

Dynamically delivered 'installationassets' module:

应用"中的Java代码仍然无法访问:

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

 loadTexture(R.drawable.testgraphic);
 cannot resolve symbol 'testgraphic'

推荐答案

我找到了一个可行的解决方案,可以从功能模块资源中加载可绘制对象.诀窍是为模块使用正确的软件包名称.

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.

示例:

  • 基本模块软件包名称为" com.project "
  • 在build.gradle" FeatureModule " 中定义的
  • 功能模块名称
  • 功能模块程序包名称" com.project.FeatureModule "
  • 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);

参考文章

这篇关于从主模块访问动态特征模块的可绘制文件夹中的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆