In Buck, Why Module A cannot refer Module B's resources, like R.string.. R.id..
does anyone knows why?
As far as I can tell the reason comes down to Buck not merging the resources of the dependency modules with the resources of the dependant module. So if:
We have:
- Library module A, with package "com.hello.A"
- Library module B, with package "com.hello.B"
- A is a dependency of B
- A has a string resource "my_title"
Then:
- We'll have a "com.hello.A.R" resource class
- We'll have a "com.hello.B.R" resource class
- "com.hello.B.R" will not contain references to any resource from A. So we won't be able to reference our string from B's code by just saying "com.hello.B.R.string.my_title". We'll need to say "com.hello.A.R.string.my_title".
This is problematic for my use case too, as I'd like to have the same codebase built with Gradle and Buck. As things are currently, we refer to resources in dependency modules either the Buck way (com.hello.A.R.string.my_title in module B's codebase), or the Gradle way (com.hello.B.R.string.my_title in module B's codebase.
The only solution I managed to come up with is generating "merging" resource rules for modules that have resources and have other modules depending on them. So in the example above, we'd have a special android_resource rule for module B in module A's BUCK config, that builds all of A's resource with B's package. Module B would then also have this resource rule as a dependency next to it's own resource rule.
According to my tests, this actually works, but the number of build rules can blow up depending on the project structure, as this needs to be done to all transitive module dependencies.
@rzsombor There is no need for two codebases, in your example, com.hello.A.R.string.my_title will work both for BUCK and gradle. And that's what exactly our team do for our production code; we run BUCK build for development and gradle build for production release.
@Piasy I forgot to mention one important detail from my previous example: the modules are all Android Library modules. The reference com.hello.A.R.string.my_title seems to be ok if it's placed in an Android Application module. This same code fails for me as soon as it's being used in a library, the error being:
Error:(8, 51) error: package com.hello.A.R does not exist
Unfortunately our project is structured into ~a dozen inter-dependent android library modules on multiple layers, which are being assembled by a single Android Application module containing minimal code. This results in two things: ridiculous Gradle build times, and the need to solve the issue this thread is about.
@rzsombor Got it.
Seems like this is possible using an undocumented option that was added in https://github.com/facebook/buck/pull/653
I will add it to our plugin
Thanks for resolving this, much appreciated! This was one of the biggest blockers for using Buck / OkBuck for our project, we'll definitely reevaluate them now.