How to overwrite shared default resource values android library projects

Hello Xamarin Community,

i am facing a problem while sharing resources across Android apps.
In Short: I would like to centralize most parts of the android UI across similar apps, except some in this case customer specific resources, like strings, colors, images, fonts etc...

In my scenario there is a Backend modeled as a PCL which is shared over two platform specific libraries, one is my so called AndroidLib. There are numerous customers, who will get their specifically branded app for each platform in this case i am in charge for the customer specific Android apps. Each Customer requires some customization, but not every customer wants the same amount of customization, there are sensible Defaults for strings, colors etc. which should be shared across multiple android projects for all customers. Sharing resouce-independent code causes no problem, but resource-dependent code does. The following structure outlines the given scenario:

Backend [PCL]

AndroidLib [AndroidLibrary] : Provides Code and Defaults for Android Apps -> Backend
C1Android [AndroidApp] : App for Customer 1 -> AndroidLib
C2Android [AndroidApp] : App for Customer 2 -> AndroidLib

Xamarin's Android Library Project type allows to merge resources scattered over several Android Library Projects including the Android App Projects, this is valuable solution to add new resources. However this method seems to disallow overwriting of resources, e.g. when a layout in the library project which depends on dummy resources being defined later in a customer specific project.

A first attempt of mine was to use the Android Library Project's resource merge mechanism to overwrite a previously defined resource, but without success. It seems that the first time a resource is bound to a value, this is going to be its final value, independent of whether another value is set to the resource afterwards. To clarify the approach take a look at the following:

AndroidLib contains:

  • strings.xml
    -- somestring = "asdf"
    -- anotherstring = "dummy value"
  • main.axml
    -- references anotherstring

C1Android contains:

  • strings.xml
    -- evenanotherstring = "42"
    -- anotherstring = "123"

This ends up with an application showing the string "dummy value" defined via the main.axml. Expected output would have been "123" to me, as it is defined within the Customer 1's Android Project. The same holds for images as well. Maybe there is some flag which can be set to activate such overwrite behavior within the msbuild tasks.

Does anybody have some thoughts or some valid workarounds about this ? Maybe i am undermining the usual way of solving this kind of resource sharing/overwriting task. I am new to xamarin just as a side not, and i am faced with an already existing code base so my possibilities are a little bit constrained, but i am always open minded :)

Thanks in advance


  • chandanchandan USMember

    More info on this topic is on youtube

  • cfraz89_cfraz89_ USMember

    Our team has a similar problem, with many apps needing high-level UI tweaks on top of a shared codebase.
    Resource overwriting of library projects should work (just as it does in vanilla android dev), we're depending on it heavily for the new version of the project.

    Basic layouts and drawables in hte library project, which allows ids to be generated. The top level projects reference the library projects, which should cause the generated resource.designer.cs to copy over all ids from the library projects, and overwrite ones which are also defined in the top project.

    Before this was supported in xamarin, we kept a giant dictionary which mapped enums to integers, with an enum defined for each corresponding layout/view widget. The top level project would fill out this dictionary at startup, mapping out the enums to the generated ids.

  • @cfraz89_ Thanks for your input, i already read about Androids capabilities, so i assumed it to work just the same in Xamarins build process. Maybe i encountered some bug or what i suspect being more probable is that i am doing something wrong. I have provided a minimal working example which shows the described behavior.

    It is a solution containing three projects:
    AndroidLib : defines a string value "predefined" with content "COMMON"
    Berlin -> AndroidLib : overwrites "predefined" with content "Berlin"
    Munich -> AndroidLib

    Both define a Main.axml which reference "predefined"'s string content. Munich app behaves as expected and shows "COMMON", but Berlin app doesn't and shows "COMMON" as well. I figured out that Xamarin Studio's axml editor is able to display the correct value "Berlin" on the large text view. However the deployed android package doesn't act as expected.

    Any clues ?
    Greetings and thanks in advance

  • CShipleyCShipley USMember ✭✭

    We have need to be able to customize an application using libraries and have had similar difficulties. Any luck figuring out a solution?

Sign In or Register to comment.