Android difference between Module vs Flavor

假装没事ソ 提交于 2020-06-14 15:10:28

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/47749810/android-difference-between-module-vs-flavor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!