androidandroid-studiogradleaapt

Android Studio: unable to merge module resources into main app


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!


Solution

  • 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