我的项目是多个模块。本质上,会有跨多个模块使用的资源。我们将此资源命名为 logo_image.png
目前,在每个模块的 res 文件夹中,都存在一个 logo_image.png 的副本。这可能是一个问题,因为它会占用大量空间。
我想做的是创建一个公共(public)模块,它存储整个项目使用的所有资源,并且所有引用都指向该公共(public)模块中的资源。这样的事情可能吗?这是我的尝试(失败了)
Activity_main.xml
<ImageView
android:id="@+id/imageView"
android:layout_width="300dp"
android:layout_height="150dp"
android:src="@common/drawable/logo_image" /> <-- "Unknown reference
最佳答案
您可以在项目中创建一个名为 assets
或您想要的任何其他名称的模块。然后,在每个需要访问公共(public)资源的模块中,将该模块作为依赖项包含在模块 build.gradle
implementation project(':assets')
然后,您将像在每个模块中本地定义 Assets 一样访问 Assets 。
<ImageView
android:id="@+id/imageView"
android:layout_width="300dp"
android:layout_height="150dp"
android:src="@drawable/logo_image" />
关于android - 从 XML 文件中查找另一个模块中的资源 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61873271/