Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Layouts in Android Studio depending on Flavor

I have a project in Android Studio that uses different flavors for different colors (some other things as well, but they don't matter).

What I would like is to see how a certain UI element (all defined by xml files) looks with non-default flavor?

Let's say I have to flavors, flavor A which is default and flavor B which is not. Lets say flavor A main color is red, and flavor B main color is green.

When I open layouts, they will always show as red, no matter what flavor in build variants is selected.

I'm sure there is a way to do this, but I don't know how.

Edit1: Seems I didn't explain it (thought it was implicit). My resource files for flavors are already in different folders. Colors in resource files are of the same name. That is not the problem, the problem is that I can't see colors in layouts inside of Android Studio.

Colors are in separate folders and it works as it should. When I build and install flavors, they use their colors. But when I open a layout, it always shows with default colors in Android Studio

like image 296
Invader Zim Avatar asked May 22 '19 11:05

Invader Zim


People also ask

What is Flavour dimension android?

The flavor dimensions define the cartesian product that will be used to produce variants. Example: flavorDimensions("dimA", "dimB") productFlavors { row1 { ... dimension = "dimA" } row2 { ... dimension = "dimA" } row3 { ... dimension = "dimA" } col1 { ...

What are layouts in Android Studio?

When you start a new project, by default Android Studio will make your app equip itself with a relative layout [ Android Studio version 2.3 and above has the default layout set to Constraint Layout ]. You can very well build an app without using other layouts in Android Studio.

How to change the product flavor in Android Studio?

Change product flavor from Android Studio side menu. Check here You have to select build variant to run the respective flavor. To find the build variants tab take a look at below screenshot. Not the answer you're looking for? Browse other questions tagged android android-studio android-studio-3.0 or ask your own question.

How to implement constraint layout in Android Studio?

Constraint Layout is a new addition to Android Studio. That’s why to implement it, we have to add a dependency in our project. In your Gradle Scripts, find a file ‘ build.gradle (Module: app).’ In that file, you can find all the dependencies. Add the following code to it. Remember, the ‘dependencies’ block should already exist.

How do I create and configure build types in Android Studio?

You can create and configure build types in the module-level build.gradle file inside the android block. When you create a new module, Android Studio automatically creates the debug and release build types for you. Although the debug build type doesn't appear in the build configuration file, Android Studio configures it with debuggable true.


1 Answers

Let me blow out some dusts first :

You're getting default colors in your layout files is because, your layouts resides under default folder (main package in this case).

What happens here is that, when you create build of particular flavor (let's say flavorA), packaging system takes files specific to that flavors that are duplicated from default folder and replace it to provide you flavor-specific build.

So, your generated apk file contains flavor specific files (colors.xml in your case) + default files from main (res folder of main like all layouts and other resources & your class files too) that are not the part of your flavor specific folder.

That's why everything works perfectly.


Now back to point : (Hack)

What should you do to see layouts with flavor specific colors?

"Although I would not recommend this way" unless you're having different layout logic for flavor specific build, all you can do is place particular layout files (just to see rendered output) in your layout folder under your flavorA directory.

So, It'll now take colors from flavorA folder and render you layout in IDE with that colors in your layout file.

Note: Again, this is just a hacky way to see different things in flavor specific way, not a solution. Even though I don't think there's even solution for this problem because, this is not a problem & this is how multi-flavor gradle build system works and that's not a bug in this system.

Edit:


So, after some research, I find out how you can achieve such thing that 'you get rendered output based on your flavor colors'.

Let's say you're having different colors based on your flavors like below :

enter image description here

Now, contents of flavorB & flavorW are different for colors.xml file but having same color attributes like:

flavorB contains :

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#757575</color>
    <color name="colorPrimaryDark">#212121</color>
    <color name="colorAccent">#607d8b</color>
</resources>

flavorW contains :

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6d4c41</color>
    <color name="colorPrimaryDark">#3e2723</color>
    <color name="colorAccent">#33691e</color>
</resources>

As you can find out that number of color elements are the same but just values are different.

TL;DR

Solution : In such case, you can simply delete colors.xml from your main/res/values folder. It won't generate any error unless you're missing any flavor specific color attr for your "current build variant".

check out main dir for my project :

enter image description here

(As you can see here that, main resource directory doesn't contains any colors.xml file)

So, if you'll try changing your build variant and look at any layout file from main it'll opt out with that specific product flavor color for you.

Note : Beware that, If you add or remove one color from one flavor remember to also add/remove it to other flavors too or else your project might get error while building. (you can also do the same trick with other resources too like drawables, layouts, dimensions etc. just make sure every flavor contains it all with the same resource name)

like image 109
Jeel Vankhede Avatar answered Sep 19 '22 14:09

Jeel Vankhede