Projects Overview: Modules
Projects Overview: Modules
Projects Overview: Modules
Table of contents
Modules
Project files
o The Android project view
Project structure settings
o Developer services
A project in Android Studio contains everything that defines your workspace for an app,
from source code and assets, to test code and build configurations. When you start a
new project, Android Studio creates the necessary structure for all your files and makes
them visible in the Project window on the left side of the IDE (click View > Tool
Windows > Project). This page provides an overview of the key components inside
your project.
Modules
A module is a collection of source files and build settings that allow you to divide your
project into discrete units of functionality. Your project can have one or many modules
and one module may use another module as a dependency. Each module can be
independently built, tested, and debugged.
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.
You can add a new module to your project by clicking File > New > New Module.
Provides 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. When you
create a new project, the default module name is "app".
They each provide essential files and some code templates that are appropriate
for the corresponding app or device type.
For more information on adding a module, read Add a Module for a New Device.
Feature module
Represents a modularized feature of your app that can take advantage of Play
Feature Delivery. For example, with feature modules, you can provide your users
with certain features of your app on-demand or as instant experiences
through Google Play Instant.
Library module
Provides a container for your reusable code, which you can use as a
dependency in other app modules or import into other projects. Structurally, a
library module is the same as an app module, but when built, it creates a code
archive file instead of an APK, so it can't be installed on a device.
Android Library: This type of library can contain all file types supported in an
Android project, including source code, resources, and manifest files. The build
result is an Android Archive (AAR) file that you can add as a dependency for your
Android app modules.
Java Library: This type of library can contain only Java source files. The build
result is an Java Archive (JAR) file that you can add as a dependency for your
Android app modules or other Java projects.
Provides a container for your Google Cloud backend code. This module has the
required code and dependencies for a Java App Engine backend that uses
simple HTTP, Cloud Endpoints, and Cloud Messaging to connect to your app.
You can develop your backend to provide cloud services your app needs.
Using Android Studio to develop your Google Cloud module lets you manage
app code and backend code in the same project. You can also run and test your
backend code locally, and use Android Studio to deploy your Google Cloud
module.
For more information on running and deploying a Google Cloud module,
see Running, Testing, and Deploying the Backend.
Some people also refer to modules as sub-projects and that's okay, because Gradle
also refers to modules as projects. For example, when you create a library module and
want to add it as a dependency to your Android app module, you must declare it as
follows:
dependencies {
compile project(':my-library-module')
}
Project files
By default, Android Studio displays your project files in the Android view. This view
does not reflect the actual file hierarchy on disk, but is organized by modules and file
types to simplify navigation between key source files of your project, hiding certain files
or directories that are not commonly used. Some of the structural changes compared to
the structure on disk include the following:
Within each Android app module, files are shown in the following groups:
manifests
Contains the AndroidManifest.xml file.
java
Contains the Java source code files, separated by package names, including
JUnit test code.
res
Contains all non-code resources, such as XML layouts, UI strings, and bitmap
images, divided into corresponding sub-directories. For more information about
all possible resource types, see Providing Resources.
When you select Project view, you can see a lot more files and directories. The most
important of which are the following:
module-name/
build/
libs/
src/
Contains all code and resource files for the module in the following
subdirectories:
androidTest/
Contains code for instrumentation tests that run on an Android device. For more
information, see the Android Test documentation.
main/
Contains the "main" sourceset files: the Android code and resources shared by
all build variants (files for other build variants reside in sibling directories, such
as src/debug/ for the debug build type).
AndroidManifest.xml
Describes the nature of the application and each of its components. For more
information, see the AndroidManifest.xml documentation.
java/
jni/
Contains native code using the Java Native Interface (JNI). For more information,
see the Android NDK documentation.
gen/
Contains application resources, such as drawable files, layout files, and UI string.
See Application Resources for more information.
assets/
Contains file that should be compiled into an .apk file as-is. You can navigate
this directory in the same way as a typical file system using URIs and read files
as a stream of bytes using the AssetManager . For example, this is a good
location for textures and game data.
test/
Contains code for local tests that run on your host JVM.
build.gradle (module)
build.gradle (project)
This defines your build configuration that apply to all modules. This file is integral
to the project, so you should maintain them in revision control with all other
source code.
SDK Location: Sets the location of the JDK, Android SDK, and Android NDK
that your project uses.
Project: Sets the version for Gradle and the Android plugin for Gradle, and the
repository location name.
Developer Services: Contains settings for Android Studio add-in components
from Google or other third parties. See Developer Services, below.
Modules: Allows you to edit module-specific build configurations, including the
target and minimum SDK, the app signature, and library dependencies. See Modules,
below.
Developer services
Turning on any of these services may cause Android Studio to add necessary
dependencies and permissions to your app. Each configuration page lists these and
other actions that Android Studio takes if you enable the associated service.
Modules
The Modules settings section lets you change configuration options for each of your
project's modules. Each module's settings page is divided into the following tabs:
Properties: Specifies the versions of the SDK and build tools to use to compile
the module.
Signing: Specifies the certificate to use to sign your APK.
Flavors: Lets you create multiple build flavors, where each flavor specifies a set
of configuration settings, such as the module's minimum and target SDK version, and
the version code and version name. For example, you might define one flavor that has a
minimum SDK of 15 and a target SDK of 21, and another flavor that has a minimum
SDK of 19 and a target SDK of 23.
Build Types: Lets you create and modify build configurations, as described
in Configuring Gradle Builds. By default, every module has debug and release build
types, but you can define more as needed.
Dependencies: Lists the library, file, and module dependencies for this module.
You can add, modify, and delete dependencies from this pane. For more information
about module dependencies, see Configuring Gradle Builds.
Styles and Themes
Styles and themes on Android allow you to separate the details of your app
design from the UI structure and behavior, similar to stylesheets in web design.
A style is a collection of attributes that specify the appearance for a single View.
A style can specify attributes such as font color, font size, background color, and
much more.
A theme is a collection of attributes that's applied to an entire app, activity, or
view hierarchy—not just an individual view. When you apply a theme, every view
in the app or activity applies each of the theme's attributes that it supports.
Themes can also apply styles to non-view elements, such as the status bar and
window background.
Styles and themes are declared in a style resource file in res/values/, usually
named styles.xml.
A style specifies attributes for a particular type of view. For example, one style might
specify a button's attributes. Every attribute you specify in a style is an attribute you
could set in the layout file. By extracting all the attributes to a style, it's easy to use and
maintain them across multiple widgets.
Styles and themes are meant to work together. For example, you might have a style
that specifies that one part of a button should be colorPrimary, and another part
should be colorSecondary. The actual definitions of those colors is provided in the
theme. When the device goes into night mode, your app can switch from its "light"
theme to its "dark" theme, changing the values for all those resource names. You don't
need to change the styles, since the styles are using the semantic names and not
specific color definitions.
For more information about how themes and styles work together, see the blog
post Android Styling: Themes vs Styles.
<TextView
style="@style/GreenText"
... />
Each attribute specified in the style is applied to that view if the view accepts it. The
view simply ignores any attributes that it does not accept.
Note: Only the element to which you add the style attribute receives those style attributes—any
child views do not apply the styles. If you want child views to inherit styles, instead apply the style
with the android:theme attribute.
For example, you can inherit the Android platform's default text appearance and modify
it as follows:
However, you should always inherit your core app styles from the Android Support
Library. The styles in the support library provide compatibility with Android 4.0 (API level
14) and higher by optimizing each style for the UI attributes available in each version.
The support library styles often have a name similar to the style from the platform, but
with AppCompat included.
To inherit styles from a library or your own project, declare the parent style
name without the @android:style/ part shown above. For example, the following
example inherits text appearance styles from the support library:
You can also inherit styles (except those from the platform) by extending a style's name
with a dot notation, instead of using the parent attribute. That is, prefix the name of
your style with the name of the style you want to inherit, separated by a period. You
should usually do this only when extending your own styles, not styles from other
libraries. For example, the following style inherits all styles from the GreenText style
above and then increases the text size:
<style name="GreenText.Large">
<item name="android:textSize">22dp</item>
</style>
You can continue inheriting styles like this as many times as you'd like by chaining on
more names.
Note: If you use the dot notation to extend a style, and you also include the parent attribute, then
the parent styles override any styles inheritted through the dot notation.
To find which attributes you can declare with an <item> tag, refer to the "XML
attributes" table in the various class references. All views support XML attributes from
the base View class, and many views add their own special attributes. For example,
the TextView XML attributes includes the android:inputType attribute that you can
apply to a text view that receives input, such as an EditText widget.
For example, here's how to apply the Android Support Library's material design "dark"
theme to the whole app:
And here's how to apply the "light" theme to just one activity:
Beginning with Android 5.0 (API level 21) and Android Support Library v22.1, you can
also specify the android:theme attribute to a view in your layout file. This modifies the
theme for that view and any child views, which is useful for altering theme color palettes
in a specific portion of your interface.
Style hierarchy
Android provides a variety of ways to set attributes throughout your Android app. For
example, you can set attributes directly in a layout, you can apply a style to a view, you
can apply a theme to a layout, and you can even set attributes programmatically.
When choosing how to style your app, be mindful of Android's style hierarchy. In
general, you should use themes and styles as much as possible for consistency. If
you've specified the same attributes in multiple places, the list below determines which
attributes are ultimately applied. The list is ordered from highest precedence to lowest:
If you’re trying to style your app and not seeing the results you expect, it's likely that other styling is
overriding your changes. For example, if you apply a theme to your app, along with a style to an
individual View, the style attributes would override any matching theme attributes for that View. Note,
however, that any theme attributes that aren't overridden by the style are still used.
TextAppearance
One limitation with styles is that you can apply only one style to a View. In a TextView,
however, you can also specify a TextAppearance attribute which functions similarly to a
style, as shown in the following example:
<TextView
...
android:textAppearance="@android:style/TextAppearance.Material.Headline"
android:text="This text is styled via textAppearance!" />
Notice that the style values are actually references to other color resources, defined in
the project's res/values/colors.xml file. So that's the file you should edit to change
the colors. But before you start changing these colors, preview your colors with
the Material Color Tool. This tool helps you pick colors from the material palette and
preview how they'll look in an app.
<!-- a secondary color for controls like checkboxes and text fields
-->
<color name="colorAccent">#FF4081</color>
</resources>
And then you can override whatever other styles you want. For example, you can
change the activity background color as follows:
Most attributes are applied to specific types of views, and some apply to all views.
However, some theme attributes listed at R.styleable.Theme apply to the activity
window, not the views in the layout. For example, windowBackground changes the
window background and windowEnterTransition defines a transition animation to use
when the activity starts (for details, see Start an Activity with an Animation).
The Android Support Library also provides other attributes you can use to customize
your theme extended from Theme.AppCompat (such as the colorPrimary attribute
shown above). These are best viewed in the library's attrs.xml file
Note: Attribute names from the support library do not use the android: prefix. That's used only for
attributes from the Android framework.
There are also different themes available from the support library that you might want to
extend instead of the ones shown above. The best place to see the available themes is
the library's themes.xml file.
If a new version of Android adds theme attributes that you want to use, you can add
them to your theme while still being compatible with old versions. All you need is
another styles.xml file saved in a values directory that includes the resource version
qualifier. For example:
Because the styles in the values/styles.xml file are available for all versions, your
themes in values-v21/styles.xml can inherit them. As such, you can avoid
duplicating styles by beginning with a "base" theme and then extending it in your
version-specific styles.
For example, to declare window transitions for Android 5.0 (API level 21) and higher,
you need to use some new attributes. So your base theme
in res/values/styles.xml could look like this:
<resources>
<!-- base set of styles that apply to all versions -->
<style name="BaseAppTheme"
parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorPrimaryDark">@color/primaryTextColor</item>
<item name="colorAccent">@color/secondaryColor</item>
</style>
<!-- declare the theme name that's actually applied in the manifest
file -->
<style name="AppTheme" parent="BaseAppTheme" />
</resources>
<resources>
<!-- extend the base theme to add styles available only with API level
21+ -->
<style name="AppTheme" parent="BaseAppTheme">
<item name="android:windowActivityTransitions">true</item>
<item
name="android:windowEnterTransition">@android:transition/slide_right</item
>
<item
name="android:windowExitTransition">@android:transition/slide_left</item>
</style>
</resources>
Now you can apply AppTheme in your manifest file and the system selects the styles
available for each system version.
For more information about using alternative resources for different devices,
read Providing Resources.
<Button
style="@style/Widget.AppCompat.Button.Borderless"
... />
And if you want to apply this style to all buttons, you can declare it in your
theme's buttonStyle as follows:
You can also extend widget styles, just like extending any other style, and then apply
your custom widget style in your layout or in your theme.
These files include Java class files, resource files, and a file containing
compiled resources.
An APK contains the following directories:
META-INF/: Contains the CERT.SF and CERT.RSA signature files, as well as
the MANIFEST.MF manifest file.
assets/: Contains the app's assets, which the app can retrieve using an
AssetManager object.
res/: Contains resources that aren't compiled into resources.arsc.
lib/: Contains the compiled code that is specific to the software layer of a
processor. This directory contains a subdirectory for each platform type, like
armeabi, armeabi-v7a, arm64-v8a, x86, x86_64, and mips.
An APK also contains the following files. Among them, only
AndroidManifest.xml is mandatory.
resources.arsc: Contains compiled resources. This file contains the XML
content from all configurations of the res/values/ folder.
The packaging tool extracts this XML content, compiles it to binary form, and
archives the content. This content includes language strings and styles, as
well as paths to content that is not included directly in the resources.arsc file,
such as layout files and images.
Note: Do not compress this file in your APK. classes.dex: Contains the
classes compiled in the DEX file format understood by the Dalvik/ART virtual
machine.
AndroidManifest.xml: Contains the core Android manifest file. This file lists the
name, version, access rights, and referenced library files of the app. The file
uses Android's binary XML format.