Andrew Kemp
Andrew Kemp

Reputation: 153

Modules vs Package Android Studio

I am very new to Android Studio and Java.

I am in the process of developing an app. This app will basically follow a layered architecture, with different layers, for instance, the UI, Data Access Layer, Service layer etc.

I am not clear on the differences between packages and modules.

My question is, where would one put all these different layers, in modules, or packages?

Pointing to @Angel reponse to this question, the only difference between the two is \, modules define a more strict rule by who can access them, by having to import the namespaces for the modules.

Upvotes: 15

Views: 11956

Answers (1)

Bryan
Bryan

Reputation: 15155

A module is the container for the source code within a project. A single project can have multiple modules, but each module is a separate set of code and resources.

For instance, when you create a new project with the default settings, Android Studio generates a module called app. This module holds all of the source code, resource files and app-level settings for your application.

But, if you create a new project with a Phone/Tablet application as well as an Android Wear application, you will see two modules; mobile and wear. Each of these modules contain the source code, resource files and app-level settings for their respective application.

You can also create another module to be shared between multiple modules; this module would be considered a library module.

A package is essentially the directory (folder) to which source code belongs. Normally, this is a directory structure that uniquely identifies your application; such as com.example.app. Then you can create packages within your application package that separates your code; such as com.example.app.ui or com.example.app.data.


Therefore, to answer your question, the package for each application resides within the src/main/java directory of the application module. You could place a separate package within the application pacakge to separate each "layer" of your application architechture.

Just for a visual example, this is the basic structure for one of my projects:

project
|-- build.gradle
|-- settings.gradle
~
|-- common // a common library module for both mobile and wear
|   |-- build.gradle
|   |-- proguard-rules.pro
|   +-- src
|       +-- main
|           |-- AndroidManifest.xml
|           |-- assets
|           |-- java
|           |   +-- com
|           |       +-- example
|           |           +-- library // common module library package
|           |               |-- data
|           |               +-- util
|           +-- res
|
|-- mobile // mobile application module
|   |-- build.gradle
|   |-- proguard-rules.pro
|   +-- src
|       +-- main
|           |-- AndroidManifest.xml
|           |-- assets
|           |-- java
|           |   +-- com
|           |       +-- example
|           |           +-- app // mobile module application package
|           |               |-- data
|           |               |-- ui
|           |               +-- util
|           +-- res
|
+-- wear // wear application module
    |-- build.gradle
    |-- proguard-rules.pro
    +-- src
        +-- main
            |-- AndroidManifest.xml
            |-- assets
            |-- java
            |   +-- com
            |       +-- example
            |           +-- app // wear module application package
            |               |-- data
            |               |-- ui
            |               +-- util
            +-- res

Upvotes: 34

Related Questions