0

I have the following project setup:

MyProjectApp/app/src/main/res/layout/main_activity.xml (layout1)
MyProjectApp/module1/src/main/res/layout/main_activity.xml (layout2)

Now in my module1, when I try to do setContentView(R.layout.main_activity);, it loads the layout from the app. To fix this, I could rename my layout2 so that there is no name collision.

My problem is, when I release my module1 in AAR format, there is still the possibility of name collision because another project setup may name their own layout1 the same as my layout in the AAR.

Well, I could rename my layout2 to a very random name but I don't want that. I want a simple layout name such as main_activity.

So the question is, how can I make sure that when I do setContentView(R.layout.main_activity); inside module1, it loads the layout2 file?

user1506104
  • 6,554
  • 4
  • 71
  • 89
  • 2
    Have you tried using a FQCN (fully qualified class name) to refer to the `R` file? Unless the `app` and `module1` have the exact same packagename, you could do `myproject.app.R.layout.main_activity` and `myproject.module1.R.layout.main_activity1` and that would avoid the problem. Basically, `module1` should import it's own `R` file, and not the `app` module's `R` file. – Shark Jun 13 '23 at 13:55
  • Yes, I tried this: setContentView(com.mycompany.module1.R.layout.main_activity). Still loads the `layout1` instead of `layout2`. @Shark – user1506104 Jun 13 '23 at 14:12
  • ... Are you absolutely sure that the two files are not identical? I mean, if module1.R has, for example, textview named `@+id/text1` and app.R has textview named `@+id/text2`, can your module1 code actually reference `text2` even though it's nonexistant in it's R file? – Shark Jun 13 '23 at 14:16
  • @Shark Yes I am sure. module1.R has `text1` while app.R has none. Thus, my module crashes because it could not find `text1`. – user1506104 Jun 13 '23 at 14:20
  • Interesting. what about gradle settings, can you post those? Gradle version and plugin version might be important here, but - is the same problem present if you rename `module1.R.main_activity` to something like `module1.R.main_activity_test` ? I'm thinking that maybe, if `app` has `module1` as a dependency, the dependency resources aren't packaged because android isn't producing a fat AAR. "Similar" problem [here](https://stackoverflow.com/questions/39633799/compiling-one-aar-library-file-from-multiple-modules#39633911) – Shark Jun 13 '23 at 14:28
  • 1
    Also see [this](https://stackoverflow.com/questions/27890037/packaging-android-aar-that-has-dependencies) but it kinda sounds like AAR packaging is causing problems. Which... shouldn't be a case, as it implies a bug in the packaging itself. So probably something else is wrong. – Shark Jun 13 '23 at 14:29

1 Answers1

0

I did not find any resolution. I even tried turning off Non Transitive R Classes but did not work.

I ended up renaming my module1's layout file with something like below as suggested in this blog post:

MyProjectApp/module1/src/main/res/module1_main_activity.xml
user1506104
  • 6,554
  • 4
  • 71
  • 89