1

My Android Studio project has two product flavors - A and B. Both have in their resource folder a layout named my_layout.xml.

I want also when I build the project for flavor A to have the layout named a_my_layout.xml and b_my_layout.xml when building flavor B. Then from the source code I will dynamically get the resource id with getResources().getIdentifier("a_my_layout", "layout"...)

I want the name of the layout to remain my_layout.xml while I am working in the editor(Android studio) i.e. I don't want to have a bunch of prefixes to my layouts in the res/ folder. Is this possile with Gradle?

gop
  • 2,150
  • 5
  • 26
  • 54
  • Why do you want to change the file's name? – shhp Jun 23 '16 at 09:47
  • 1
    shhp, This is part of a complex system where we have dynamic loading of resources. There are types of flavors where we have layouts with the same names coming from different directories and we need these prefixes. I don't want to go into details in order to keep the focus of the question. – gop Jun 23 '16 at 09:55
  • @gosho_ot_pochivka any luck with this problem? basically I need the same thing - create alias for existing resource.. – Max Komarychev Jun 25 '19 at 09:24

1 Answers1

1

To be honest, I don't get it why you want to rename them, doesn't matter if you load the resources dynamically, if you have the layouts in both flavours, when you load them, it will load the one specific to your flavour, but it's your choice :).

If you really want to rename them, I think you can achieve this by creating a rename script and link it with a predefined prebuild task.

Something like this (in app level gradle):

task renameLayoutsForVariants {
   // Do the renaming here with the prefixes
}

task renameFilesBack {
  // Rename files back to the initial name
}

afterEvaluate {
     android.applicationVariants.all { variant ->
         variant.javaCompiler.dependsOn(renameLayouts)
}

renameFilesBack.dependsOn build, deploy

For accessing resources file from gradle, there are tons of posts on SO, you just have to check it.

In conclusion, it's possible 100%, you just have to tweak some gradle scripts and link them to some already defined tasks that run when you build/run an android project.

danypata
  • 9,895
  • 1
  • 31
  • 44