It seems to me like this will only work if your application project references resources defined in your library project. While my project does that, I also reference resources in my library project that are defined in my library project. I am pretty sure these will not work using the method provided. For example, lets take the following setup:
you have application
com.example.app and library of
com.example.lib.
Each of these projects will have its own android generated R class (com.example.lib.R). However, the application project will have an R class and the resource definition ID's won't line up with the corresponding one that is generated for the library project. It looks like android system generates all of the resource identifiers from the library projects into your application project, however the values aren't the same.
As an example, in my lib project i have a String resource named
error_file_not_found. In the Library project's R class, it has an id of 0x7f040058. The value does not exist in the xml resources of my application project, but yet its definition does exist in the com.example.app.R class, and its value is 0x7f050058. I'm not sure if it's a coincidence but all the ID's in my com.example.app.R class are exactly 0x10000 (65536 decimal) greater than the corresponding definition in the com.example.lib.R class. When I debug the code being run from Robolectric's jUnit test runner, the value defined in com.example.lib.R is the one passed to resources.getString(R.string.something). However, when debugging the actual application running, the value of the Resource ID is the one that gets defined in the application project's R class as opposed to the one in the library project (which is what I would expect).
The Robolectric resource loader fails to find this definition though and returns null. Even if the resources had been loaded it wouldn't have worked though because the values wouldn't line up (the ID wouldn't map to the name).
This seems like a pretty tough problem to solve. If we at least pull in the library resources as mentioned that would help. Unfortunately the android manifest is not where library project references are defined (maybe in the past).. What i've seen is that the library projects are defined in the project.properties file in the application root folder so we would need to look there.
Once we have the resources it's just a matter of getting the mapping right. I'm not sure that this 0x1000 increment is useful but perhaps if no definition is found we could check for a resource definition 0x1000 greater. Seems like quite a hack and it may be specific to the version of the android sdk tools installed though (or may break in the future).
An alternate solution may be to simply not use the standard resource loader from the library project and instead use another that we pass in a string value rather than an ID. This however has the downside that we lose out on static type analysis done here and could end up trying to reference resources that weren't defined (if we don't test thoroughly enough!).