英文:
Android Studio: unable to merge module resources into main app
问题
我有几个Android项目,它们共享相同的帮助函数(用Kotlin编写)和相同的资源,例如button_save、toast_press_back_again_to_exit。将所有这些函数和资源提取到一个共同的库中,所有应用项目只需包含这个库,而不是再次定义所有这些内容,这是非常自然的。
一切都很顺利,直到昨天我启动了一个新项目,我仍然可以在布局XML中引用这些通用资源,比如android:text="R.string.button_save"
,但我不能再在Kotlin中直接引用它们,而必须使用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.string.xxx
,而必须使用com.example.myapplication.mylibrary.R.string.xxx
。
为什么?任何线索都将不胜感激!
英文:
I have several android projects and they share same helper functions (in Kotlin) and same resources such as button_save, toast_press_back_again_to_exit. it is quite natural to extract all those functions and resources into a common library, and all the app projects just need to include this library, instead of define all those things again.
Everything works well until yesterday I started a new project, I can still refer to those common resources in the layout xml, like, andoird:text="R.string.button_save", but I can no longer directly refer to them in Kotlin by R.string.button_save, I have to use "com.example.app.common.R.string.button_save".
It looks like Android Studio fails to merge library/module resources into the main app's R.java. I thought that it might because of the latest Gradle version (8.0.2 + com.android.tools.build:gradle:7.4.2), I tried to downgrade Gradle to the same version that the old projects use (7.5.1 + com.android.tools.build:gradle:7.2.2), but it still does not work.
I googled couple hours but most are talking about how to solve the conflict during the resource merging, but the issue I am facing is they do not merge at all.
I even tried to create an app project from scratch, defines nothing in it to make sure no conflict, then create a new module library, defines nothing but one simple string resource with a 32bits random string as its name, also to make sure no conflict. but, in app's side, I can not use this string resource by R.string.xxx, but have to use it by com.example.myapplication.mylibrary.R.string.xxx.
Why? Any clue would be appreciated!
答案1
得分: 5
Urrrr... 没关系,找到了根本原因。
在最新的Android Studio中,自动生成的 gradle.properties 文件包含了这个:
# 启用每个库的 R 类的命名空间,以便其 R 类仅包括库本身声明的资源,而不包括来自库依赖关系的资源,从而减小该库的 R 类的大小
android.nonTransitiveRClass=true
英文:
Urrrr... never mind, found the root cause.
In the latest Android Studio, the auto generated gradle.properties file contains this:
# 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论