5

what are the main differences between android modules and flavors?

From module definition https://developer.android.com/studio/projects/add-app-module.html

Modules provide a container for your app's source code, resource files, and app level settings, such as the module-level build file and Android manifest file. Each module can be independently built, tested, and debugged.

But I could say the same thing about flavors.

Can someone point out the key differences between these two?

2 Answers2

3

A module is a part of a project whereas flavors are more or less configurations/implementations

A concrete example for modules: if you are making a library you can have several module, e.g. the library itself and a demo project. Each of them are modules

Note: each module has its own code

A concrete example for flavors: Your are making an application with some online features. For this you have several environments (on server env. for test, one for live version). You can make a build flavor for every environment with its own configuration (and its own implementations if needed)

Note: flavors can share their code (using main folder)

This is just a short conclusion. Modules and Flavors are much more powerful than this short description but I think it will point the most important differences

EDIT: Key difference is the structure of your project, especially regarding gradle.

modules always have their own gradle file, so you get following structure:

projectName.gradle
    module1.gradle
    module2.gradle
    ...

flavors are defined in a module's gradle file:

e.g. module1.gradle could look like this:

// some other gradle stuff before

buildTypes {
    dev {
        // your config
    }
    debug {
        // your config
    }
    release {
        // your config
    }
}

As you can see, you can mix up flavors and modules. So now you would have a project with 2 modules and different flavors in module1.

In your project folder, you should have following structure in your file explorer:

/project
  /module1
    /src
      /main
      /dev
      /debug
      /release
  /module2
beal
  • 435
  • 4
  • 15
  • I didn't ask about concrete examples, but I asked for key differences. If I make app/src/flavorName directory, it will be on same "part of project level" as module. – Tomáš Havlíček Dec 11 '17 at 10:15
  • I only made this examples to make differences more clear. So what questions do you still have? – beal Dec 11 '17 at 11:17
  • The way flavors, and moduls are manifested is only valid point you are making in your post. It is difference, but I wouldn't call that **KEY** difference between these two. What is the functionality I can do with one, but cannot do with other? – Tomáš Havlíček Dec 11 '17 at 12:10
  • 2
    I cannot image a scenario which would require a module or a flavor. I think in most cases you can implement nearly everything using flavors or modules (sure there will be some cases a flavor or module will not work, but I cannot image some at this moment). The more interesting point is when you should use a flavor and when you should use a module to keep your project clean and simple – beal Dec 11 '17 at 15:12
  • Useful discussion. You mentioned that, flavors can share their code (using main folder) My point: Even modules can share the code with the main module/project (using main folder) – Perry May 31 '19 at 10:59
0

Modules are completely different sets of files. Flavors can share most of the code or configs and have only minor differences.

Pavel Kuznetsov
  • 344
  • 2
  • 8
  • what are those minor differences. ? – Mayur Agarwal Dec 11 '17 at 09:48
  • I can define flavor as app/src/flavorName, and it would be completly different set of code too. Moduls can share code too, but I guess It wouldn't be good practice. Differences between flavors depends on my implementation. – Tomáš Havlíček Dec 11 '17 at 09:54
  • Yes, but you can't do opposite - make modules to share code. These two entities just intended for use in different cases, but you can do similar things with them, if you want. – Pavel Kuznetsov Dec 11 '17 at 09:57
  • @MayurAgarwal this depends on your config You can change gradle properties, like applicationId, etc. Or provide completely different sources, as TomášHavlíček mentioned above – Pavel Kuznetsov Dec 11 '17 at 10:00
  • @PavelKuznetsov I can add another module as dependency to original module? I can have BaseModule with shared code between others modules. – Tomáš Havlíček Dec 11 '17 at 10:06
  • @TomášHavlíček yes, of course. But it will cost you some additional time. Modules is all about dividing the app. Flavor - is for creating different versions of one app with minor differences. – Pavel Kuznetsov Dec 11 '17 at 10:31
  • @TomášHavlíček In fact, using flavors to make differences in app logic is not so convenient, because you need to keep file structule similar for all of them. Better you should use flavors to vary some resources and gradle configs, and try to not change the code (or make only simple changes like constant values etc) – Pavel Kuznetsov Dec 11 '17 at 10:36