Android Studio:无法将模块资源合并到主应用中



我有几个android项目,它们共享相同的helper函数(在Kotlin中)和相同的资源,如button_save, toast_press_back_again_to_exit。将所有这些函数和资源提取到一个公共库中是很自然的,所有的应用项目只需要包含这个库,而不是重新定义所有这些东西。

一切都工作得很好,直到昨天我开始了一个新的项目,我仍然可以在布局xml中引用那些常见的资源,比如,anddoird:text=" r.t rstring .button_save",但我不能再直接在Kotlin中通过r.t rstring引用它们。button_save,我必须使用"com.example。app.common。r.string。button_save"

看起来Android Studio未能将库/模块资源合并到主应用程序的R.java中。我认为这可能是因为最新的Gradle版本(8.0.2 + com.android.tools.build: Gradle:7.4.2),我试图将Gradle降级到旧项目使用的相同版本(7.5.1 + com.android.tools.build: Gradle:7.2.2),但它仍然不起作用。

我搜索了几个小时,但大多数人都在谈论如何解决资源合并期间的冲突,但我面临的问题是它们根本没有合并。

我甚至试图从头开始创建一个应用程序项目,在其中没有定义任何内容以确保没有冲突,然后创建一个新的模块库,只定义一个简单的字符串资源,以32位随机字符串作为其名称,也确保没有冲突。但是,在应用程序方面,我不能通过r.s string使用这个字符串资源。.xxx,但是必须通过com.example.myapplication.mylibrary.R.string.xxx.

为什么?任何线索将不胜感激!

…没关系,找到根本原因了。

在最新的Android Studio中,自动生成gradle。属性文件包含:

# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true

最新更新