Is it possible to copy/replace files only at build time?

Hi, I have the following scenario:

I have a main Project, which will have ‘branding’ versions. So for a different country, for example Greece we will have little modified code and different resources. For Netherlands we will have again different code changes and resource changes.

We don’t want to make a new project for each country, so the Solution could be:

  • Make a folder ‘branding/greece’ and ‘branding/netherlands’, etc. In it we put ONLY the stuff that is different/changed - code, layouts, assets, etc.

  • Then make a Gradle script which copies the stuff from ‘branding/greece’ and the copies+replaces it into the main project directory. So contents of ‘branded’ ‘res’ and ‘src’ folders would be copied/replaced into the main project for example.

So this would make the main project appear to be as the ‘brand’ project and according .apk files would be generated.

  • Since the main Project structure should not change permanently, there are two options:
  1. Make a backup of the main project, and after building a ‘brand’ version delete the project with all the copied/replaced files - then restore the original project structure from the backup.

  2. Somehow undo the copy/replace operation after building, but not sure if this is possible in Gradle.

I might be totally wrong about this, so any suggestions are welcome.

Sounds like what you want is (many) different flavors. Have you looked into flavors in the Android Gradle plugin docs? (Copying things around is undesirable, and should only be done as a last resort.)

Thanks for the info, but this is not a final solution in this case, because the main project contains 90% of code that doesn’t change. The ‘brand’ versions maybe add 2 or 3 new files for code, so only these files should be there. If we change the ‘src’ directory to the ‘brand’ project it will obviously not compile, because it has only 3 files in that folder. So with your suggested approach we would have to copy the 90% into the ‘branding/country’ folder and that is a lot of duplicated code.

Are you certain about this? I’d expect that by using flavors, nothing at all would have to be copied. (The standard example of flavors is a shared main codebase and two application variants with variant-specific code - e.g. free and premium. How is your case fundamentally different?)

I did take a look at some examples and I have some doubts - what if we want to keep the same package name for both flavors. Seems like flavors are only supported when you make different new packages.

Not sure. Flavors are likely your best shot, so I recommend to give them a try.

It seems to be working, even with the same package name. Thanks for your help!