Android Studio: New Project VS New Module

Android studio: new project vs new module

From the documentation (Android Studio is based on Intellij IDEA) :

Whatever you do in IntelliJ IDEA, you do that in the context of a
project. A project is an organizational unit that represents a
complete software solution.

Your finished product may be decomposed into a series of discrete,
isolated modules, but it's a project definition that brings them
together and ties them into a greater whole.

For Android, it means one project per app, and one module per library and per test app.

There are multiple issues if you try to build multiple apps within the same project. It's possible, but if you try (like I did), you will see that almost everything is designed to work with a single app per project.

For example, there is an option to "rebuild the project", which makes no sense with multiple apps, many other project settings would be useless, and the built-in VCS system isn't great when you have multiple repositories.

Modules vs Package Android Studio

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

(Dis-) Advantage of having multiple modules in an Android Studio Project?

It is of great advantage to have multiple modules rather than to create a single large app-module. Following are the key points:

  1. If you find the compile time is taking longer then you can disable the module from gradle you are not working upon temporarily and compile it faster.
  2. A module helps us to divide project into discrete units of functionality also. You can create one data module which contains all pure java beans and can be used by multiple app if you are in same domain. Eg. Finance domain can have two applications one for viewing policies for customer and other can be for an insurance agent for viewing the same data. But the data module can be shared across all apps and even the data module can be borrowed from server or API team. Data module can be tested individually without any android dependencies and any one knows about java can write test cases.
  3. Each module can be independently build, tested, and debugged.
  4. Additional modules are often useful when creating code libraries within your own project or when you want to create different sets of code and resources for different device types, such as phones and wearables, but keep all the files scoped within the same project and share some code.
  5. Also Android app module and Library module are different.
  6. You can keep two different versions of module based on the API releases as from ASOP.

You can have a look for more on android developer resource

How modularization can speed up your Android app’s built time

App modularization and module lazy loading at Instagram and beyond

Modularizing Android Applications by Mauin

Survey on how Android developers were modularising their apps

Difference between build.gradle (Project) and build.gradle (Module)

build.gradle (Project:My-app)

Top-level build file where you can add configuration options common to
all sub-projects/modules.

Each project contains a top-level Gradle file. It usually contains common configurations for all modules. Whatever is included in this top-level Gradle gile, it will affect all modules.

Example:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha3'

//Maven plugin
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

build.gradle (Module:app)

Build file of your specific module (where you add your dependencies, signing configurations, build types, flavors, etc.)

All modules have a specific Gradle file. Whatever is included in this gradle file, it will only affect the module that is included on.

Example:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.hrskrs.gesturefun"
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
zipAlignEnabled true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
zipAlignEnabled true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':gesture-fun')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.jakewharton:butterknife:7.0.1'
}

How to create Android Studio project with ONLY a library module

For anyone who is having this same problem, it appears that you truly cannot delete the app module from an Android Studio project. All projects must have an app module, even if the main purpose is to create a library. I ended up making a small example app showing how to use the library, which appears to be the convention.

Android Studio: Why does my New project contain 2 modules

When creating new projects, we always create a top level project with a single module in it.
Studio or IntelliJ will show both in the module list but the top one ("testing") doesn't output anyway.

We do this instead of a flat structure in the root project, so that it will allow you later to easily add new modules (like libraries) without having to convert the structure of your project.

what matters is the "testing-testing" module (we'll probably rename this "testing-main" at some point). the other module doesn't do anything. If you look at its build.gradle it's empty, while the other one declares an Android project.



Related Topics



Leave a reply



Submit