Thursday, March 15, 2018

Understanding Android Studio project structure

Unlike xcode or visual studio, there is no workspace file or project file for Android studio project. So what consists of an android studio workspace or project?

First, android studio uses the term "project" to represents an usual workspace, and uses "module" to represent an usual project. Modules for executable apk file is called "app module", and modules for reusable libraries (aar or jar files) are called "library module". The android term will be used below.

An android studio project is any directory which contains the files of
settings.gradle
build.gradle
gradle.properties
and one or more directory for app modules and libraries modules. The above files are called project level files under android view, as they are in the project folder.

Settings.gradle
The most important file is settings.grade, which indicates which modules are include in the project,  the below sample settings.gradle shows two modules are included in the project "app" and "mylib3":
include ':app', ':mylib3'

Please refer to https://docs.gradle.org/current/dsl/org.gradle.api.initialization.Settings.html for details of settings.gradle. By default, the module name is also the folder name under the project's folder

settings.gradle can also be used to indicate the path if a module is not a subfolder under the current project folder

build.gradle
Project level build.gradle usually indicates repository used to load the dependent libraries for both build time and runtime. As project does not contains any source files so the build.gradle does not contains any android SDK version info, or build type info

gradle.properties
gradle.properties file can be used to configure the environment settings for the build

The under each module folder, the only required file is build.gradle, which starts with either 'com.android.application' for android executable
or "com.android.library" for android library
or "java-library" for jar library
The module's build.gradle also include the information about how java source file and other dependent libraries are used by the current module.


Note, any build.gradle file can be used as a gradle script plugin by its container gradle file through "apply from".  So if a dependent library is not shown in the module's dependency section, the library may be applied from a sub gradle file "applied to" to the parent gradle file.



No comments:

Post a Comment